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,18 +34,15 @@ 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 // get a list of message commands
new Thread(() -> { LinkedList<MessageCommand> messageCommands = Cache.getMessageCommandListener().getRegisteredCommands();
// get a list of message commands LinkedList<String> commandNames = new LinkedList<>();
LinkedList<MessageCommand> messageCommands = Cache.getMessageCommandListener().getRegisteredCommands(); for (MessageCommand command : messageCommands) {
LinkedList<String> commandNames = new LinkedList<>(); commandNames.add(command.getCommandLabels().get(0));
for (MessageCommand command : messageCommands) { }
commandNames.add(command.getCommandLabels().get(0));
}
// 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

@ -31,51 +31,47 @@ 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! String senderId = event.getMessage().getAuthor().getId();
new Thread(() ->
{
String senderId = event.getMessage().getAuthor().getId();
// 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());
if (error != null) { if (error != null) {
event.getMessage().reply(error).queue(); event.getMessage().reply(error).queue();
return; return;
} }
// get the amount from the command args. // get the amount from the command args.
Integer toDeleteAmount; Integer toDeleteAmount;
if (args.length == 0) toDeleteAmount = 1; if (args.length == 0) toDeleteAmount = 1;
else toDeleteAmount = Integer.parseInt(args[0]); else toDeleteAmount = Integer.parseInt(args[0]);
error = ClearChat.checkDeleteAmount(toDeleteAmount); error = ClearChat.checkDeleteAmount(toDeleteAmount);
if (error != null) { if (error != null) {
event.getMessage().reply(error).queue(); event.getMessage().reply(error).queue();
return; return;
} }
// answer by saying that the operation has begun. // answer by saying that the operation has begun.
String content = "\uD83D\uDEA7 Clearing..."; String content = "\uD83D\uDEA7 Clearing...";
Message botMessage = event.getMessage().reply(content).complete(); Message botMessage = event.getMessage().reply(content).complete();
int deleted = ClearChat.delete(toDeleteAmount, int deleted = ClearChat.delete(toDeleteAmount,
event.getMessageIdLong(), event.getMessageIdLong(),
event.getChannel()); event.getChannel());
// get a nicely formatted message that logs the deletion of messages. // get a nicely formatted message that logs the deletion of messages.
content = ClearChat.parseAmount(deleted); content = ClearChat.parseAmount(deleted);
// edit the message text and attach a button. // edit the message text and attach a button.
Button dismiss = ClearChat.getDismissButton(); Button dismiss = ClearChat.getDismissButton();
// ^ todo: maybe the dismiss button should also delete the original message sent by the user? // ^ todo: maybe the dismiss button should also delete the original message sent by the user?
// todo: but then, we need to differentiate between command type in the database, and store // todo: but then, we need to differentiate between command type in the database, and store
// todo: that message's id too. // todo: that message's id too.
botMessage = botMessage.editMessage(content).setActionRow(dismiss).complete(); botMessage = botMessage.editMessage(content).setActionRow(dismiss).complete();
// 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,23 +23,20 @@ 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 // defer reply because this might take a moment
new Thread(() -> { event.deferReply().queue();
// defer reply because this might take a moment
event.deferReply().queue();
// get a list of slash commands // get a list of slash commands
List<SlashCommand> registeredCommands = Cache.getSlashCommandListener().getRegisteredCommands(); List<SlashCommand> registeredCommands = Cache.getSlashCommandListener().getRegisteredCommands();
LinkedList<String> registeredCommandNames = new LinkedList<>(); LinkedList<String> registeredCommandNames = new LinkedList<>();
for(SlashCommand command : registeredCommands) for(SlashCommand command : registeredCommands)
{ {
// node: adding slash so people realize that this is specific about slash commands. // node: adding slash so people realize that this is specific about slash commands.
registeredCommandNames.add("/" + command.getCommandName()); registeredCommandNames.add("/" + command.getCommandName());
} }
// 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,53 +28,48 @@ 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(() ->
// check if user is trying to run command in dms.
String error = ClearChat.checkDMs(event.getChannel());
if(error != null)
{ {
event.deferReply().complete(); event.getHook().editOriginal(error).queue();
return;
}
// check if user is trying to run command in dms. /* get the amount from the command args.
String error = ClearChat.checkDMs(event.getChannel()); NULL should not be possible because we specified them as mandatory,
if(error != null) but apparently the mobile app doesn't care and still sends the command if you omit the args. */
{ OptionMapping amountOption = event.getOption("amount");
event.getHook().editOriginal(error).queue(); int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt();
return;
}
/* get the amount from the command args. error = ClearChat.checkDeleteAmount(toDeleteAmount);
NULL should not be possible because we specified them as mandatory, if(error != null)
but apparently the mobile app doesn't care and still sends the command if you omit the args. */ {
OptionMapping amountOption = event.getOption("amount"); event.getHook().editOriginal(error).queue();
int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt(); return;
}
error = ClearChat.checkDeleteAmount(toDeleteAmount); // answer by saying that the operation has begun.
if(error != null) String content = "\uD83D\uDEA7 Clearing...";
{ Message botMessage = event.getHook().editOriginal(content).complete();
event.getHook().editOriginal(error).queue();
return;
}
// answer by saying that the operation has begun. // actually delete the messages.
String content = "\uD83D\uDEA7 Clearing..."; int deleted = ClearChat.delete(toDeleteAmount,
Message botMessage = event.getHook().editOriginal(content).complete(); event.getInteraction().getIdLong(),
event.getChannel());
// actually delete the messages. // get a nicely formatted message that logs the deletion of messages.
int deleted = ClearChat.delete(toDeleteAmount, content = ClearChat.parseAmount(deleted);
event.getInteraction().getIdLong(),
event.getChannel());
// get a nicely formatted message that logs the deletion of messages. // edit the message text and attach a button.
content = ClearChat.parseAmount(deleted); Button dismiss = ClearChat.getDismissButton();
botMessage = botMessage.editMessage(content).setActionRow(dismiss).complete();
// edit the message text and attach a button. // add the message to database.
Button dismiss = ClearChat.getDismissButton(); Cache.getDatabaseSource().queueDisabling(botMessage);
botMessage = botMessage.editMessage(content).setActionRow(dismiss).complete(); Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getUser());
// add the message to database.
Cache.getDatabaseSource().queueDisabling(botMessage);
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();
} }
} }