Skip to content

Commit

Permalink
Merge pull request #7 from t0gepi/bugFix_indexOutOfBounds
Browse files Browse the repository at this point in the history
IndexOutOfBounds bugs fixed
  • Loading branch information
FrostedCA authored Jul 2, 2023
2 parents 74a3d20 + c8e5589 commit 87f5321
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +24,7 @@ public String getPrefix() {
}

public void setPrefix(String prefix) {
this.prefix = prefix;
PrefixCommands.prefix = prefix;
}

@Override
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ public String getDescription() {
return null;
}

public List<PrefixOption> getOptions() {
protected final List<PrefixOption> 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;
Expand All @@ -48,6 +56,6 @@ public List<Role> getAuthorizedRoles(JDA jda) {
return new ArrayList<>();
}

public void execute(MessageReceivedEvent event, MySQL mySQL) { }
abstract public void execute(MessageReceivedEvent event, MySQL mySQL);

}
3 changes: 2 additions & 1 deletion src/main/java/ca/tristan/testbot/BotMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
45 changes: 45 additions & 0 deletions src/main/java/ca/tristan/testbot/ExampleCommand.java
Original file line number Diff line number Diff line change
@@ -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<String> 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();
}


}

0 comments on commit 87f5321

Please sign in to comment.