fix stuff and add stuff yup
This commit is contained in:
parent
fce5527abe
commit
91ac26714d
12 changed files with 210 additions and 35 deletions
|
@ -5,8 +5,10 @@ import net.kyori.adventure.text.Component;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface Command {
|
public interface Command {
|
||||||
|
String name();
|
||||||
String description();
|
String description();
|
||||||
List<String> usage();
|
List<String> usage();
|
||||||
|
List<String> alias();
|
||||||
int trustLevel();
|
int trustLevel();
|
||||||
|
|
||||||
Component execute(CommandContext context, String[] args, String[] fullArgs) throws Exception;
|
Component execute(CommandContext context, String[] args, String[] fullArgs) throws Exception;
|
||||||
|
|
|
@ -9,17 +9,28 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CommandBlockCommand implements Command {
|
public class CommandBlockCommand implements Command {
|
||||||
|
public String name() { return "cb"; }
|
||||||
|
|
||||||
public String description() {
|
public String description() {
|
||||||
return "Executes a command in the command core";
|
return "Executes a command in the command core";
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> usage() {
|
public List<String> usage() {
|
||||||
final List<String> usages = new ArrayList<>();
|
final List<String> usages = new ArrayList<>();
|
||||||
usages.add("<command>");
|
usages.add("<{command}>");
|
||||||
|
|
||||||
return usages;
|
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() {
|
public int trustLevel() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,26 @@ import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CowsayCommand implements Command {
|
public class CowsayCommand implements Command {
|
||||||
|
public String name() { return "cowsay"; }
|
||||||
|
|
||||||
public String description() {
|
public String description() {
|
||||||
return "Moo";
|
return "Moo";
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> usage() {
|
public List<String> usage() {
|
||||||
final List<String> usages = new ArrayList<>();
|
final List<String> usages = new ArrayList<>();
|
||||||
usages.add("<cow> <message>");
|
usages.add("<cow> <{message}>");
|
||||||
|
|
||||||
return usages;
|
return usages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> alias() {
|
||||||
|
final List<String> aliases = new ArrayList<>();
|
||||||
|
aliases.add("");
|
||||||
|
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
public int trustLevel() {
|
public int trustLevel() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,17 +9,26 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class EchoCommand implements Command {
|
public class EchoCommand implements Command {
|
||||||
|
public String name() { return "echo"; }
|
||||||
|
|
||||||
public String description() {
|
public String description() {
|
||||||
return "Says a message";
|
return "Says a message";
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> usage() {
|
public List<String> usage() {
|
||||||
final List<String> usages = new ArrayList<>();
|
final List<String> usages = new ArrayList<>();
|
||||||
usages.add("<message>");
|
usages.add("<{message}>");
|
||||||
|
|
||||||
return usages;
|
return usages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> alias() {
|
||||||
|
final List<String> aliases = new ArrayList<>();
|
||||||
|
aliases.add("say");
|
||||||
|
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
public int trustLevel() {
|
public int trustLevel() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,10 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class HelpCommand implements Command {
|
public class HelpCommand implements Command {
|
||||||
|
public String name() { return "help"; }
|
||||||
|
|
||||||
public String description() {
|
public String description() {
|
||||||
return "Shows the help";
|
return "Shows the help";
|
||||||
}
|
}
|
||||||
|
@ -24,6 +25,15 @@ public class HelpCommand implements Command {
|
||||||
return usages;
|
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() {
|
public int trustLevel() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -62,9 +72,8 @@ public class HelpCommand implements Command {
|
||||||
public List<Component> getCommandListByTrustLevel (int trustLevel) {
|
public List<Component> getCommandListByTrustLevel (int trustLevel) {
|
||||||
final List<Component> list = new ArrayList<>();
|
final List<Component> list = new ArrayList<>();
|
||||||
|
|
||||||
for (Map.Entry<String, Command> entry : CommandHandlerPlugin.commands().entrySet()) {
|
for (Command command : CommandHandlerPlugin.commands()) {
|
||||||
final String name = entry.getKey();
|
final String name = command.name();
|
||||||
final Command command = entry.getValue();
|
|
||||||
|
|
||||||
if (command.trustLevel() != trustLevel) continue;
|
if (command.trustLevel() != trustLevel) continue;
|
||||||
list.add(Component.text(name).color(getColorByTrustLevel(trustLevel)));
|
list.add(Component.text(name).color(getColorByTrustLevel(trustLevel)));
|
||||||
|
@ -89,13 +98,11 @@ public class HelpCommand implements Command {
|
||||||
|
|
||||||
final String commandName = args[0];
|
final String commandName = args[0];
|
||||||
|
|
||||||
for (Map.Entry<String, Command> entry : CommandHandlerPlugin.commands().entrySet()) {
|
for (Command command : CommandHandlerPlugin.commands()) {
|
||||||
if (!entry.getKey().equals(commandName)) continue;
|
if (!command.name().equals(commandName)) continue;
|
||||||
|
|
||||||
final List<Component> usages = new ArrayList<>();
|
final List<Component> usages = new ArrayList<>();
|
||||||
|
|
||||||
final Command command = entry.getValue();
|
|
||||||
|
|
||||||
usages.add(
|
usages.add(
|
||||||
Component.empty()
|
Component.empty()
|
||||||
.append(Component.text(prefix + commandName).color(NamedTextColor.GOLD))
|
.append(Component.text(prefix + commandName).color(NamedTextColor.GOLD))
|
||||||
|
@ -105,7 +112,7 @@ public class HelpCommand implements Command {
|
||||||
usages.add(
|
usages.add(
|
||||||
Component.empty()
|
Component.empty()
|
||||||
.append(Component.text("Trust level: ").color(NamedTextColor.GREEN))
|
.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()) {
|
for (String usage : command.usage()) {
|
||||||
|
|
|
@ -9,6 +9,8 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class TestCommand implements Command {
|
public class TestCommand implements Command {
|
||||||
|
public String name() { return "test"; }
|
||||||
|
|
||||||
public String description() {
|
public String description() {
|
||||||
return "Tests if the bot is working";
|
return "Tests if the bot is working";
|
||||||
}
|
}
|
||||||
|
@ -20,6 +22,13 @@ public class TestCommand implements Command {
|
||||||
return usages;
|
return usages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> alias() {
|
||||||
|
final List<String> aliases = new ArrayList<>();
|
||||||
|
aliases.add("");
|
||||||
|
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
public int trustLevel() {
|
public int trustLevel() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,17 +8,26 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ThrowCommand implements Command {
|
public class ThrowCommand implements Command {
|
||||||
|
public String name() { return "throw"; }
|
||||||
|
|
||||||
public String description() {
|
public String description() {
|
||||||
return "A command to throw an error, kinda useless";
|
return "A command to throw an error, kinda useless";
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> usage() {
|
public List<String> usage() {
|
||||||
final List<String> usages = new ArrayList<>();
|
final List<String> usages = new ArrayList<>();
|
||||||
usages.add("[message]");
|
usages.add("[{message}]");
|
||||||
|
|
||||||
return usages;
|
return usages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> alias() {
|
||||||
|
final List<String> aliases = new ArrayList<>();
|
||||||
|
aliases.add("throwerror");
|
||||||
|
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
public int trustLevel() {
|
public int trustLevel() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,13 +10,12 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ValidateCommand implements Command {
|
public class ValidateCommand implements Command {
|
||||||
|
public String name() { return "validate"; }
|
||||||
|
|
||||||
@Override
|
|
||||||
public String description() {
|
public String description() {
|
||||||
return "Validates a hash";
|
return "Validates a hash";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> usage() {
|
public List<String> usage() {
|
||||||
final List<String> usages = new ArrayList<>();
|
final List<String> usages = new ArrayList<>();
|
||||||
usages.add("<hash|ownerHash>");
|
usages.add("<hash|ownerHash>");
|
||||||
|
@ -24,12 +23,17 @@ public class ValidateCommand implements Command {
|
||||||
return usages;
|
return usages;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public List<String> alias() {
|
||||||
|
final List<String> aliases = new ArrayList<>();
|
||||||
|
aliases.add("checkhash");
|
||||||
|
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
public int trustLevel() {
|
public int trustLevel() {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
|
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
|
||||||
final Bot bot = context.bot();
|
final Bot bot = context.bot();
|
||||||
final String hash = fullArgs[0];
|
final String hash = fullArgs[0];
|
||||||
|
|
|
@ -4,31 +4,31 @@ import lombok.Getter;
|
||||||
import me.chayapak1.chomensbot_mabe.command.Command;
|
import me.chayapak1.chomensbot_mabe.command.Command;
|
||||||
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
import me.chayapak1.chomensbot_mabe.command.CommandContext;
|
||||||
import me.chayapak1.chomensbot_mabe.commands.*;
|
import me.chayapak1.chomensbot_mabe.commands.*;
|
||||||
|
import me.chayapak1.chomensbot_mabe.util.ElementUtilities;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.event.HoverEvent;
|
import net.kyori.adventure.text.event.HoverEvent;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class CommandHandlerPlugin {
|
public class CommandHandlerPlugin {
|
||||||
@Getter private static final Map<String, Command> commands = new HashMap<>();
|
@Getter private static final List<Command> commands = new ArrayList<>();
|
||||||
|
|
||||||
public CommandHandlerPlugin () {
|
public CommandHandlerPlugin () {
|
||||||
registerCommand("cb", new CommandBlockCommand());
|
registerCommand(new CommandBlockCommand());
|
||||||
registerCommand("cowsay", new CowsayCommand());
|
registerCommand(new CowsayCommand());
|
||||||
registerCommand("echo", new EchoCommand());
|
registerCommand(new EchoCommand());
|
||||||
registerCommand("help", new HelpCommand());
|
registerCommand(new CreatorCommand());
|
||||||
registerCommand("test", new TestCommand());
|
registerCommand(new DiscordCommand());
|
||||||
registerCommand("throw", new ThrowCommand());
|
registerCommand(new HelpCommand());
|
||||||
registerCommand("validate", new ValidateCommand());
|
registerCommand(new TestCommand());
|
||||||
|
registerCommand(new ThrowCommand());
|
||||||
|
registerCommand(new ValidateCommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerCommand (String commandName, Command command) {
|
public void registerCommand (Command command) {
|
||||||
commands.put(commandName, command);
|
commands.add(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Component executeCommand (String input, CommandContext context, String hash, String ownerHash) {
|
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 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
|
// 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);
|
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 String[] fullArgs = Arrays.copyOfRange(splitInput, 1, splitInput.length);
|
||||||
final int longestUsageIndex = getLongestUsageIndex(command.usage());
|
final int longestUsageIndex = getLongestUsageIndex(command.usage());
|
||||||
final int minimumArgs = getMinimumArgs(command.usage().get(longestUsageIndex));
|
final String usage = command.usage().get(longestUsageIndex);
|
||||||
final int maximumArgs = getMaximumArgs(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 < 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 = "";
|
String userHash = "";
|
||||||
if (trustLevel > 0 && splitInput.length >= 2) userHash = splitInput[1];
|
if (trustLevel > 0 && splitInput.length >= 2) userHash = splitInput[1];
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue