From 5f73c4069b484e00999e1ab822e16f142c9ffc45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 22 Nov 2022 21:59:58 +0100 Subject: [PATCH] Make bot commands run in separate threads by default --- .../commands/message/BotInfoCommand.java | 21 +++--- .../commands/message/ClearCommand.java | 70 +++++++++--------- .../commands/slash/BotInfoCommand.java | 29 ++++---- .../commands/slash/ClearCommand.java | 73 +++++++++---------- .../listeners/MessageCommandListener.java | 4 +- .../listeners/SlashCommandListener.java | 4 +- 6 files changed, 95 insertions(+), 106 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java index 15272fd..cf92536 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java @@ -34,18 +34,15 @@ public class BotInfoCommand implements MessageCommand @Override 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 - LinkedList messageCommands = Cache.getMessageCommandListener().getRegisteredCommands(); - LinkedList commandNames = new LinkedList<>(); - for (MessageCommand command : messageCommands) { - commandNames.add(command.getCommandLabels().get(0)); - } + // get a list of message commands + LinkedList messageCommands = Cache.getMessageCommandListener().getRegisteredCommands(); + LinkedList commandNames = new LinkedList<>(); + for (MessageCommand command : messageCommands) { + commandNames.add(command.getCommandLabels().get(0)); + } - // send the list - MessageEmbed embed = BotInfo.generateEmbed(commandNames); - event.getMessage().replyEmbeds(embed).queue(); - }).start(); + // send the list + MessageEmbed embed = BotInfo.generateEmbed(commandNames); + event.getMessage().replyEmbeds(embed).queue(); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/ClearCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/ClearCommand.java index 51ef22d..b0dfa7b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/ClearCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/ClearCommand.java @@ -31,51 +31,47 @@ public class ClearCommand implements MessageCommand @Override 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(); - // check if user is trying to run command in dms. - String error = ClearChat.checkDMs(event.getChannel()); - if (error != null) { - event.getMessage().reply(error).queue(); - return; - } + // check if user is trying to run command in dms. + String error = ClearChat.checkDMs(event.getChannel()); + if (error != null) { + event.getMessage().reply(error).queue(); + return; + } - // get the amount from the command args. - Integer toDeleteAmount; - if (args.length == 0) toDeleteAmount = 1; - else toDeleteAmount = Integer.parseInt(args[0]); + // get the amount from the command args. + Integer toDeleteAmount; + if (args.length == 0) toDeleteAmount = 1; + else toDeleteAmount = Integer.parseInt(args[0]); - error = ClearChat.checkDeleteAmount(toDeleteAmount); - if (error != null) { - event.getMessage().reply(error).queue(); - return; - } + error = ClearChat.checkDeleteAmount(toDeleteAmount); + if (error != null) { + event.getMessage().reply(error).queue(); + return; + } - // answer by saying that the operation has begun. - String content = "\uD83D\uDEA7 Clearing..."; - Message botMessage = event.getMessage().reply(content).complete(); + // answer by saying that the operation has begun. + String content = "\uD83D\uDEA7 Clearing..."; + Message botMessage = event.getMessage().reply(content).complete(); - int deleted = ClearChat.delete(toDeleteAmount, - event.getMessageIdLong(), - event.getChannel()); + int deleted = ClearChat.delete(toDeleteAmount, + event.getMessageIdLong(), + event.getChannel()); - // get a nicely formatted message that logs the deletion of messages. - content = ClearChat.parseAmount(deleted); + // get a nicely formatted message that logs the deletion of messages. + content = ClearChat.parseAmount(deleted); - // edit the message text and attach a button. - Button dismiss = ClearChat.getDismissButton(); - // ^ 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: that message's id too. - botMessage = botMessage.editMessage(content).setActionRow(dismiss).complete(); + // edit the message text and attach a button. + Button dismiss = ClearChat.getDismissButton(); + // ^ 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: that message's id too. + botMessage = botMessage.editMessage(content).setActionRow(dismiss).complete(); - // add the message to database. - Cache.getDatabaseSource().queueDisabling(botMessage); - Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getAuthor()); - }).start(); + // add the message to database. + Cache.getDatabaseSource().queueDisabling(botMessage); + Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getAuthor()); } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java index 80df263..f9866e8 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java @@ -23,23 +23,20 @@ public class BotInfoCommand extends SlashCommandImpl @Override 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 - event.deferReply().queue(); + // defer reply because this might take a moment + event.deferReply().queue(); - // get a list of slash commands - List registeredCommands = Cache.getSlashCommandListener().getRegisteredCommands(); - LinkedList registeredCommandNames = new LinkedList<>(); - for(SlashCommand command : registeredCommands) - { - // node: adding slash so people realize that this is specific about slash commands. - registeredCommandNames.add("/" + command.getCommandName()); - } + // get a list of slash commands + List registeredCommands = Cache.getSlashCommandListener().getRegisteredCommands(); + LinkedList registeredCommandNames = new LinkedList<>(); + for(SlashCommand command : registeredCommands) + { + // node: adding slash so people realize that this is specific about slash commands. + registeredCommandNames.add("/" + command.getCommandName()); + } - // send the list - MessageEmbed embed = BotInfo.generateEmbed(registeredCommandNames); - event.getHook().editOriginalEmbeds(embed).queue(); - }).start(); + // send the list + MessageEmbed embed = BotInfo.generateEmbed(registeredCommandNames); + event.getHook().editOriginalEmbeds(embed).queue(); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java index 17e188f..680772d 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java @@ -28,53 +28,48 @@ public class ClearCommand extends SlashCommandImpl public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { - // start a new thread, because we are doing synchronous, thread-blocking operations! - new Thread(() -> + event.deferReply().queue(); + + // 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. - String error = ClearChat.checkDMs(event.getChannel()); - if(error != null) - { - event.getHook().editOriginal(error).queue(); - return; - } + /* get the amount from the command args. + NULL should not be possible because we specified them as mandatory, + but apparently the mobile app doesn't care and still sends the command if you omit the args. */ + OptionMapping amountOption = event.getOption("amount"); + int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt(); - /* get the amount from the command args. - NULL should not be possible because we specified them as mandatory, - but apparently the mobile app doesn't care and still sends the command if you omit the args. */ - OptionMapping amountOption = event.getOption("amount"); - int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt(); + error = ClearChat.checkDeleteAmount(toDeleteAmount); + if(error != null) + { + event.getHook().editOriginal(error).queue(); + return; + } - error = ClearChat.checkDeleteAmount(toDeleteAmount); - if(error != null) - { - event.getHook().editOriginal(error).queue(); - return; - } + // answer by saying that the operation has begun. + String content = "\uD83D\uDEA7 Clearing..."; + Message botMessage = event.getHook().editOriginal(content).complete(); - // answer by saying that the operation has begun. - String content = "\uD83D\uDEA7 Clearing..."; - Message botMessage = event.getHook().editOriginal(content).complete(); + // actually delete the messages. + int deleted = ClearChat.delete(toDeleteAmount, + event.getInteraction().getIdLong(), + event.getChannel()); - // actually delete the messages. - int deleted = ClearChat.delete(toDeleteAmount, - event.getInteraction().getIdLong(), - event.getChannel()); + // get a nicely formatted message that logs the deletion of messages. + content = ClearChat.parseAmount(deleted); - // get a nicely formatted message that logs the deletion of messages. - content = ClearChat.parseAmount(deleted); + // edit the message text and attach a button. + Button dismiss = ClearChat.getDismissButton(); + botMessage = botMessage.editMessage(content).setActionRow(dismiss).complete(); - // edit the message text and attach a button. - Button dismiss = ClearChat.getDismissButton(); - botMessage = botMessage.editMessage(content).setActionRow(dismiss).complete(); - - // add the message to database. - Cache.getDatabaseSource().queueDisabling(botMessage); - Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getUser()); - - }).start(); + // add the message to database. + Cache.getDatabaseSource().queueDisabling(botMessage); + Cache.getDatabaseSource().trackRanCommandReply(botMessage, event.getUser()); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageCommandListener.java index bc42864..194749c 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageCommandListener.java @@ -130,6 +130,8 @@ public class MessageCommandListener extends ListenerAdapter // copy all split arguments to the array, except from the command label 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(); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index 6b15196..0dcf455 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -33,6 +33,8 @@ public class SlashCommandListener extends ListenerAdapter SlashCommand command = registeredCommands.get(commandName); if(command == null) return; - command.runSlashCommand(event); + + // finally run the command, in a new thread to avoid locking. + new Thread(() -> command.runSlashCommand(event)).start(); } }