fix stuff and add stuff yup

This commit is contained in:
ChomeNS 2023-03-19 10:47:15 +07:00
parent fce5527abe
commit 91ac26714d
12 changed files with 210 additions and 35 deletions

View file

@ -5,8 +5,10 @@ import net.kyori.adventure.text.Component;
import java.util.List;
public interface Command {
String name();
String description();
List<String> usage();
List<String> alias();
int trustLevel();
Component execute(CommandContext context, String[] args, String[] fullArgs) throws Exception;

View file

@ -9,17 +9,28 @@ import java.util.ArrayList;
import java.util.List;
public class CommandBlockCommand implements Command {
public String name() { return "cb"; }
public String description() {
return "Executes a command in the command core";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("<command>");
usages.add("<{command}>");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("cmd");
aliases.add("commandblock");
aliases.add("run");
return aliases;
}
public int trustLevel() {
return 0;
}

View file

@ -10,17 +10,26 @@ import java.util.Arrays;
import java.util.List;
public class CowsayCommand implements Command {
public String name() { return "cowsay"; }
public String description() {
return "Moo";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("<cow> <message>");
usages.add("<cow> <{message}>");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("");
return aliases;
}
public int trustLevel() {
return 0;
}

View file

@ -0,0 +1,46 @@
package me.chayapak1.chomensbot_mabe.commands;
import me.chayapak1.chomensbot_mabe.command.Command;
import me.chayapak1.chomensbot_mabe.command.CommandContext;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.List;
public class CreatorCommand implements Command {
public String name() { return "creator"; }
public String description() {
return "Shows the bot's creator";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("");
return aliases;
}
public int trustLevel() {
return 0;
}
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
context.sendOutput(
Component.empty()
.append(Component.text("ChomeNS Bot ").color(NamedTextColor.YELLOW))
.append(Component.text("was created by ").color(NamedTextColor.WHITE))
.append(Component.text("chayapak").color(NamedTextColor.GREEN))
);
return Component.text("success");
}
}

View file

@ -0,0 +1,52 @@
package me.chayapak1.chomensbot_mabe.commands;
import me.chayapak1.chomensbot_mabe.command.Command;
import me.chayapak1.chomensbot_mabe.command.CommandContext;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.List;
public class DiscordCommand implements Command {
public String name() { return "discord"; }
public String description() {
return "Shows the Discord invite";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("");
return aliases;
}
public int trustLevel() {
return 0;
}
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
final String link = "https://discord.gg/xdgCkUyaA4";
context.sendOutput(
Component.empty()
.append(Component.text("The Discord invite is ").color(NamedTextColor.WHITE))
.append(
Component
.text(link)
.clickEvent(ClickEvent.openUrl(link))
.color(NamedTextColor.BLUE)
)
);
return Component.text("success");
}
}

View file

@ -9,17 +9,26 @@ import java.util.ArrayList;
import java.util.List;
public class EchoCommand implements Command {
public String name() { return "echo"; }
public String description() {
return "Says a message";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("<message>");
usages.add("<{message}>");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("say");
return aliases;
}
public int trustLevel() {
return 0;
}

View file

@ -10,9 +10,10 @@ import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HelpCommand implements Command {
public String name() { return "help"; }
public String description() {
return "Shows the help";
}
@ -24,6 +25,15 @@ public class HelpCommand implements Command {
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("heko");
aliases.add("cmds");
aliases.add("commands");
return aliases;
}
public int trustLevel() {
return 0;
}
@ -62,9 +72,8 @@ public class HelpCommand implements Command {
public List<Component> getCommandListByTrustLevel (int trustLevel) {
final List<Component> list = new ArrayList<>();
for (Map.Entry<String, Command> entry : CommandHandlerPlugin.commands().entrySet()) {
final String name = entry.getKey();
final Command command = entry.getValue();
for (Command command : CommandHandlerPlugin.commands()) {
final String name = command.name();
if (command.trustLevel() != trustLevel) continue;
list.add(Component.text(name).color(getColorByTrustLevel(trustLevel)));
@ -89,13 +98,11 @@ public class HelpCommand implements Command {
final String commandName = args[0];
for (Map.Entry<String, Command> entry : CommandHandlerPlugin.commands().entrySet()) {
if (!entry.getKey().equals(commandName)) continue;
for (Command command : CommandHandlerPlugin.commands()) {
if (!command.name().equals(commandName)) continue;
final List<Component> usages = new ArrayList<>();
final Command command = entry.getValue();
usages.add(
Component.empty()
.append(Component.text(prefix + commandName).color(NamedTextColor.GOLD))
@ -105,7 +112,7 @@ public class HelpCommand implements Command {
usages.add(
Component.empty()
.append(Component.text("Trust level: ").color(NamedTextColor.GREEN))
.append(Component.text("TODO").color(NamedTextColor.YELLOW))
.append(Component.text(command.trustLevel()).color(NamedTextColor.YELLOW))
);
for (String usage : command.usage()) {

View file

@ -9,6 +9,8 @@ import java.util.ArrayList;
import java.util.List;
public class TestCommand implements Command {
public String name() { return "test"; }
public String description() {
return "Tests if the bot is working";
}
@ -20,6 +22,13 @@ public class TestCommand implements Command {
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("");
return aliases;
}
public int trustLevel() {
return 0;
}

View file

@ -8,17 +8,26 @@ import java.util.ArrayList;
import java.util.List;
public class ThrowCommand implements Command {
public String name() { return "throw"; }
public String description() {
return "A command to throw an error, kinda useless";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("[message]");
usages.add("[{message}]");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("throwerror");
return aliases;
}
public int trustLevel() {
return 0;
}

View file

@ -10,13 +10,12 @@ import java.util.ArrayList;
import java.util.List;
public class ValidateCommand implements Command {
public String name() { return "validate"; }
@Override
public String description() {
return "Validates a hash";
}
@Override
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("<hash|ownerHash>");
@ -24,12 +23,17 @@ public class ValidateCommand implements Command {
return usages;
}
@Override
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("checkhash");
return aliases;
}
public int trustLevel() {
return 1;
}
@Override
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
final Bot bot = context.bot();
final String hash = fullArgs[0];

View file

@ -4,31 +4,31 @@ import lombok.Getter;
import me.chayapak1.chomensbot_mabe.command.Command;
import me.chayapak1.chomensbot_mabe.command.CommandContext;
import me.chayapak1.chomensbot_mabe.commands.*;
import me.chayapak1.chomensbot_mabe.util.ElementUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang3.exception.ExceptionUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class CommandHandlerPlugin {
@Getter private static final Map<String, Command> commands = new HashMap<>();
@Getter private static final List<Command> commands = new ArrayList<>();
public CommandHandlerPlugin () {
registerCommand("cb", new CommandBlockCommand());
registerCommand("cowsay", new CowsayCommand());
registerCommand("echo", new EchoCommand());
registerCommand("help", new HelpCommand());
registerCommand("test", new TestCommand());
registerCommand("throw", new ThrowCommand());
registerCommand("validate", new ValidateCommand());
registerCommand(new CommandBlockCommand());
registerCommand(new CowsayCommand());
registerCommand(new EchoCommand());
registerCommand(new CreatorCommand());
registerCommand(new DiscordCommand());
registerCommand(new HelpCommand());
registerCommand(new TestCommand());
registerCommand(new ThrowCommand());
registerCommand(new ValidateCommand());
}
public void registerCommand (String commandName, Command command) {
commands.put(commandName, command);
public void registerCommand (Command command) {
commands.add(command);
}
public static Component executeCommand (String input, CommandContext context, String hash, String ownerHash) {
@ -36,7 +36,7 @@ public class CommandHandlerPlugin {
final String commandName = splitInput[0];
final Command command = commands.get(commandName);
final Command command = ElementUtilities.findCommand(commands, commandName);
// idea told this as "Condition 'command == null' is always 'false'" and its not true LMFAO
if (command == null) return Component.text("Unknown command: " + commandName).color(NamedTextColor.RED);
@ -45,10 +45,11 @@ public class CommandHandlerPlugin {
final String[] fullArgs = Arrays.copyOfRange(splitInput, 1, splitInput.length);
final int longestUsageIndex = getLongestUsageIndex(command.usage());
final int minimumArgs = getMinimumArgs(command.usage().get(longestUsageIndex));
final int maximumArgs = getMaximumArgs(command.usage().get(longestUsageIndex));
final String usage = command.usage().get(longestUsageIndex);
final int minimumArgs = getMinimumArgs(usage);
final int maximumArgs = getMaximumArgs(usage);
if (fullArgs.length < minimumArgs) return Component.text("Excepted minimum of " + minimumArgs + " argument(s), got " + fullArgs.length).color(NamedTextColor.RED);
if (fullArgs.length > maximumArgs) return Component.text("Too much arguments, expected " + maximumArgs).color(NamedTextColor.RED);
if (fullArgs.length > maximumArgs && !usage.contains("{")) return Component.text("Too much arguments, expected " + maximumArgs).color(NamedTextColor.RED);
String userHash = "";
if (trustLevel > 0 && splitInput.length >= 2) userHash = splitInput[1];

View file

@ -0,0 +1,16 @@
package me.chayapak1.chomensbot_mabe.util;
import me.chayapak1.chomensbot_mabe.command.Command;
import java.util.List;
public class ElementUtilities {
public static Command findCommand(List<Command> commands, String searchTerm) {
for (Command command : commands) {
if (command.name().equals(searchTerm) || command.alias().contains(searchTerm)) {
return command;
}
}
return null;
}
}