From 24a55e14fd8bed512b066c68d8e6f62e03b27a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 20 Dec 2022 14:51:29 +0100 Subject: [PATCH] Merge two classes --- .../wtf/beatrice/hidekobot/HidekoBot.java | 4 +- .../beatrice/hidekobot/util/CommandUtil.java | 103 ++++++++++++++++ .../hidekobot/util/SlashCommandUtil.java | 110 ------------------ 3 files changed, 105 insertions(+), 112 deletions(-) delete mode 100644 src/main/java/wtf/beatrice/hidekobot/util/SlashCommandUtil.java diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 94913a3..62ebe78 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -18,8 +18,8 @@ import wtf.beatrice.hidekobot.listeners.SlashCommandCompletionListener; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask; import wtf.beatrice.hidekobot.runnables.HeartBeatTask; +import wtf.beatrice.hidekobot.util.CommandUtil; import wtf.beatrice.hidekobot.util.Logger; -import wtf.beatrice.hidekobot.util.SlashCommandUtil; import java.io.File; import java.time.LocalDateTime; @@ -152,7 +152,7 @@ public class HidekoBot // update slash commands (delayed) final boolean finalForceUpdateCommands = forceUpdateCommands; Executors.newSingleThreadScheduledExecutor().schedule(() -> // todo: try-with-resources - SlashCommandUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS); + CommandUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS); // set the bot's status jda.getPresence().setStatus(OnlineStatus.ONLINE); diff --git a/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java index 4878ce9..990b3fa 100644 --- a/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java @@ -1,11 +1,21 @@ package wtf.beatrice.hidekobot.util; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.Command; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.objects.commands.SlashCommand; + +import java.util.ArrayList; +import java.util.List; public class CommandUtil { + private static final Logger logger = new Logger(CommandUtil.class); + /** * Function to delete a message when a user clicks the "delete" button attached to that message. * This will check in the database if that user ran the command originally. @@ -24,4 +34,97 @@ public class CommandUtil event.getInteraction().getMessage().delete().queue(); // no need to manually untrack it from database, it will be purged on the next planned check. } + + + public static void updateSlashCommands(boolean force) + { + + // populate commands list from registered commands + List allCommands = new ArrayList<>(); + for(SlashCommand cmd : Cache.getSlashCommandListener().getRegisteredCommands()) + { allCommands.add(cmd.getSlashCommandData()); } + + JDA jdaInstance = HidekoBot.getAPI(); + + // get all the already registered commands + List registeredCommands = jdaInstance.retrieveCommands().complete(); + + boolean update = false; + + if(force) + { + update = true; + } else + { + + // for each command that we have already registered... + for(Command currRegCmd : registeredCommands) + { + boolean found = false; + + // iterate through all "recognized" commands + for(CommandData cmdData : allCommands) + { + // if we find the same command... + if(cmdData.getName().equals(currRegCmd.getName())) + { + // quit the loop since we found it. + found = true; + break; + } + } + + // if no match was found, we need to send an updated command list because + // an old command was probably removed. + if(!found) + { + update = true; + + // quit the loop since we only need to trigger this once. + break; + } + } + + // if an update is not already queued... + if(!update) + { + // for each "recognized" valid command + for(CommandData currCmdData : allCommands) + { + boolean found = false; + + // iterate through all already registered commands. + for(Command cmd : registeredCommands) + { + // if this command was already registered... + if(cmd.getName().equals(currCmdData.getName())) + { + // quit the loop since we found a match. + found = true; + break; + } + } + + // if no match was found, we need to send an updated command list because + // a new command was probably added. + if(!found) + { + update = true; + + // quit the loop since we only need to trigger this once. + break; + } + } + } + } + + logger.log("Found " + registeredCommands.size() + " commands."); + + if(update) + { + // send updated command list. + jdaInstance.updateCommands().addCommands(allCommands).queue(); + logger.log("Commands updated. New total: " + allCommands.size() + "."); + } + } } diff --git a/src/main/java/wtf/beatrice/hidekobot/util/SlashCommandUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/SlashCommandUtil.java deleted file mode 100644 index 696614e..0000000 --- a/src/main/java/wtf/beatrice/hidekobot/util/SlashCommandUtil.java +++ /dev/null @@ -1,110 +0,0 @@ -package wtf.beatrice.hidekobot.util; - -import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.interactions.commands.Command; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; -import wtf.beatrice.hidekobot.Cache; -import wtf.beatrice.hidekobot.HidekoBot; -import wtf.beatrice.hidekobot.listeners.MessageCommandListener; -import wtf.beatrice.hidekobot.objects.commands.SlashCommand; - -import java.util.ArrayList; -import java.util.List; - -public class SlashCommandUtil -{ - - private static final Logger logger = new Logger(MessageCommandListener.class); - - public static void updateSlashCommands(boolean force) - { - - // populate commands list from registered commands - List allCommands = new ArrayList<>(); - for(SlashCommand cmd : Cache.getSlashCommandListener().getRegisteredCommands()) - { allCommands.add(cmd.getSlashCommandData()); } - - JDA jdaInstance = HidekoBot.getAPI(); - - // get all the already registered commands - List registeredCommands = jdaInstance.retrieveCommands().complete(); - - boolean update = false; - - if(force) - { - update = true; - } else - { - - // for each command that we have already registered... - for(Command currRegCmd : registeredCommands) - { - boolean found = false; - - // iterate through all "recognized" commands - for(CommandData cmdData : allCommands) - { - // if we find the same command... - if(cmdData.getName().equals(currRegCmd.getName())) - { - // quit the loop since we found it. - found = true; - break; - } - } - - // if no match was found, we need to send an updated command list because - // an old command was probably removed. - if(!found) - { - update = true; - - // quit the loop since we only need to trigger this once. - break; - } - } - - // if an update is not already queued... - if(!update) - { - // for each "recognized" valid command - for(CommandData currCmdData : allCommands) - { - boolean found = false; - - // iterate through all already registered commands. - for(Command cmd : registeredCommands) - { - // if this command was already registered... - if(cmd.getName().equals(currCmdData.getName())) - { - // quit the loop since we found a match. - found = true; - break; - } - } - - // if no match was found, we need to send an updated command list because - // a new command was probably added. - if(!found) - { - update = true; - - // quit the loop since we only need to trigger this once. - break; - } - } - } - } - - logger.log("Found " + registeredCommands.size() + " commands."); - - if(update) - { - // send updated command list. - jdaInstance.updateCommands().addCommands(allCommands).queue(); - logger.log("Commands updated. New total: " + allCommands.size() + "."); - } - } -}