Make bot commands run in separate threads by default
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-11-22 21:59:58 +01:00
parent b681acdbca
commit 5f73c4069b
6 changed files with 95 additions and 106 deletions

View File

@ -34,8 +34,6 @@ public class BotInfoCommand implements MessageCommand
@Override @Override
public void runCommand(MessageReceivedEvent event, String label, String[] args) { public void runCommand(MessageReceivedEvent event, String label, String[] args) {
// run in a thread because we might use thread-locking things
new Thread(() -> {
// get a list of message commands // get a list of message commands
LinkedList<MessageCommand> messageCommands = Cache.getMessageCommandListener().getRegisteredCommands(); LinkedList<MessageCommand> messageCommands = Cache.getMessageCommandListener().getRegisteredCommands();
LinkedList<String> commandNames = new LinkedList<>(); LinkedList<String> commandNames = new LinkedList<>();
@ -46,6 +44,5 @@ public class BotInfoCommand implements MessageCommand
// send the list // send the list
MessageEmbed embed = BotInfo.generateEmbed(commandNames); MessageEmbed embed = BotInfo.generateEmbed(commandNames);
event.getMessage().replyEmbeds(embed).queue(); event.getMessage().replyEmbeds(embed).queue();
}).start();
} }
} }

View File

@ -30,9 +30,6 @@ public class ClearCommand implements MessageCommand
@Override @Override
public void runCommand(MessageReceivedEvent event, String label, String[] args) public void runCommand(MessageReceivedEvent event, String label, String[] args)
{
// start a new thread, because we are doing synchronous, thread-blocking operations!
new Thread(() ->
{ {
String senderId = event.getMessage().getAuthor().getId(); String senderId = event.getMessage().getAuthor().getId();
@ -75,7 +72,6 @@ public class ClearCommand implements MessageCommand
// add the message to database. // add the message to database.
Cache.getDatabaseSource().queueDisabling(botMessage); Cache.getDatabaseSource().queueDisabling(botMessage);
Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getAuthor()); Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getAuthor());
}).start();
} }

View File

@ -23,8 +23,6 @@ public class BotInfoCommand extends SlashCommandImpl
@Override @Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{ {
// run in a thread because we might use thread-locking things
new Thread(() -> {
// defer reply because this might take a moment // defer reply because this might take a moment
event.deferReply().queue(); event.deferReply().queue();
@ -40,6 +38,5 @@ public class BotInfoCommand extends SlashCommandImpl
// send the list // send the list
MessageEmbed embed = BotInfo.generateEmbed(registeredCommandNames); MessageEmbed embed = BotInfo.generateEmbed(registeredCommandNames);
event.getHook().editOriginalEmbeds(embed).queue(); event.getHook().editOriginalEmbeds(embed).queue();
}).start();
} }
} }

View File

@ -28,10 +28,7 @@ public class ClearCommand extends SlashCommandImpl
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{ {
// start a new thread, because we are doing synchronous, thread-blocking operations! event.deferReply().queue();
new Thread(() ->
{
event.deferReply().complete();
// check if user is trying to run command in dms. // check if user is trying to run command in dms.
String error = ClearChat.checkDMs(event.getChannel()); String error = ClearChat.checkDMs(event.getChannel());
@ -74,7 +71,5 @@ public class ClearCommand extends SlashCommandImpl
Cache.getDatabaseSource().queueDisabling(botMessage); Cache.getDatabaseSource().queueDisabling(botMessage);
Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getUser()); Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getUser());
}).start();
} }
} }

View File

@ -130,6 +130,8 @@ public class MessageCommandListener extends ListenerAdapter
// copy all split arguments to the array, except from the command label // copy all split arguments to the array, except from the command label
commandArgs = Arrays.copyOfRange(argsRaw, 1, argsRaw.length); commandArgs = Arrays.copyOfRange(argsRaw, 1, argsRaw.length);
} }
commandObject.runCommand(event, commandLabel, commandArgs);
// finally run the command, in a new thread to avoid locking.
new Thread(() -> commandObject.runCommand(event, commandLabel, commandArgs)).start();
} }
} }

View File

@ -33,6 +33,8 @@ public class SlashCommandListener extends ListenerAdapter
SlashCommand command = registeredCommands.get(commandName); SlashCommand command = registeredCommands.get(commandName);
if(command == null) return; if(command == null) return;
command.runSlashCommand(event);
// finally run the command, in a new thread to avoid locking.
new Thread(() -> command.runSlashCommand(event)).start();
} }
} }