diff --git a/src/main/java/ca/tristan/easycommands/commands/prefix/PrefixCommands.java b/src/main/java/ca/tristan/easycommands/commands/prefix/PrefixCommands.java index ac16426..7f85bd1 100644 --- a/src/main/java/ca/tristan/easycommands/commands/prefix/PrefixCommands.java +++ b/src/main/java/ca/tristan/easycommands/commands/prefix/PrefixCommands.java @@ -2,18 +2,18 @@ import ca.tristan.easycommands.EasyCommands; import ca.tristan.easycommands.utils.LogType; -import ca.tristan.easycommands.utils.Logger; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; import java.util.Objects; +import java.util.concurrent.TimeUnit; public class PrefixCommands extends ListenerAdapter { private final EasyCommands easyCommands; - private String prefix = "!"; + public static String prefix = "!"; public PrefixCommands(EasyCommands easyCommands) { this.easyCommands = easyCommands; @@ -24,7 +24,7 @@ public String getPrefix() { } public void setPrefix(String prefix) { - this.prefix = prefix; + PrefixCommands.prefix = prefix; } @Override @@ -44,12 +44,16 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { if(easyCommands.getExecutors().containsKey(cmdName) && easyCommands.getExecutors().get(cmdName) instanceof PrefixExecutor) { PrefixExecutor executor = (PrefixExecutor) easyCommands.getExecutors().get(cmdName); String[] options = event.getMessage().getContentRaw().replace(prefix + cmdName + " ", "").split(" "); - if(options.length > 0 && executor.getOptions().size() > 0) { - for (int i = 0; i < options.length; i++) { + + if(options.length != executor.getOptions().size()){ + event.getChannel().sendMessage(executor.usage()).queue(message -> message.delete().queueAfter(10,TimeUnit.SECONDS)); + return; + } + else{ + for (int i = 0; i < executor.getOptions().size(); i++) { executor.getOptions().get(i).setStringValue(options[i]); } } - easyCommands.getLogger().logBoth(LogType.PREFIXCMD, "'" + cmdName + "' has been triggered.", event.getMember()); if(!executor.getAuthorizedChannels(easyCommands.jda).isEmpty() && !executor.getAuthorizedChannels(easyCommands.jda).contains(event.getChannel())) { easyCommands.getLogger().logBoth(LogType.WARNING, "PrefixCommand: '" + cmdName + "' has been triggered but the channel it was executed in isn't authorized.", event.getMember()); diff --git a/src/main/java/ca/tristan/easycommands/commands/prefix/PrefixExecutor.java b/src/main/java/ca/tristan/easycommands/commands/prefix/PrefixExecutor.java index 173a912..3f49c20 100644 --- a/src/main/java/ca/tristan/easycommands/commands/prefix/PrefixExecutor.java +++ b/src/main/java/ca/tristan/easycommands/commands/prefix/PrefixExecutor.java @@ -29,10 +29,18 @@ public String getDescription() { return null; } - public List getOptions() { + protected final List getOptions(){ return options; } + public String usage(){ + StringBuilder usage = new StringBuilder("Usage: " + PrefixCommands.prefix + getName()); + for(PrefixOption option : options){ + usage.append(" <").append(option.getName().replace("<", "").replace(">", "")).append(">"); + } + return usage.toString(); + } + @Override public boolean isOwnerOnly() { return false; @@ -48,6 +56,6 @@ public List getAuthorizedRoles(JDA jda) { return new ArrayList<>(); } - public void execute(MessageReceivedEvent event, MySQL mySQL) { } + abstract public void execute(MessageReceivedEvent event, MySQL mySQL); } diff --git a/src/main/java/ca/tristan/testbot/BotMain.java b/src/main/java/ca/tristan/testbot/BotMain.java index c69ab60..159e094 100644 --- a/src/main/java/ca/tristan/testbot/BotMain.java +++ b/src/main/java/ca/tristan/testbot/BotMain.java @@ -27,7 +27,8 @@ private JDA setupJDA(EasyCommands easyCommands) throws InterruptedException { ).addExecutor( new HelpCmd(easyCommands), - new ReloadConfigCmd(easyCommands) + new ReloadConfigCmd(easyCommands), + new ExampleCommand() ).addEnabledCacheFlags().addGatewayIntents().buildJDA(); } diff --git a/src/main/java/ca/tristan/testbot/ExampleCommand.java b/src/main/java/ca/tristan/testbot/ExampleCommand.java new file mode 100644 index 0000000..6e5b1ed --- /dev/null +++ b/src/main/java/ca/tristan/testbot/ExampleCommand.java @@ -0,0 +1,45 @@ +package ca.tristan.testbot; + +import ca.tristan.easycommands.commands.prefix.PrefixExecutor; +import ca.tristan.easycommands.commands.prefix.PrefixOption; +import ca.tristan.easycommands.database.MySQL; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +import java.util.List; + +public class ExampleCommand extends PrefixExecutor { + + public ExampleCommand() { + super(); + options.add(new PrefixOption("someOption", "first option")); + } + + @Override + public String getName() { + return "example"; + } + + @Override + public List getAliases() { + return List.of("s"); + } + + @Override + public String getDescription() { + return "!example [options] - The bot will say all the options."; + } + + @Override + public boolean isOwnerOnly() { + return false; + } + + + public void execute(MessageReceivedEvent event, MySQL mySQL) { + // the bot simply prints all the options of the !example command + String reply = options.stream().map(PrefixOption::getStringValue).reduce("", (r,u) -> r + " " + u); + event.getChannel().sendMessage(reply).queue(); + } + + +}