From 244e8ace76ee26ad5c69a7dbc19ca63b098447ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 22 Nov 2022 14:53:46 +0100 Subject: [PATCH] Remove redundant API command fetcher We have our own command listener now, so we don't need to rely on Discord's slow API. --- .../java/wtf/beatrice/hidekobot/Cache.java | 34 ++++--------------- .../wtf/beatrice/hidekobot/HidekoBot.java | 4 +-- .../commands/slash/BotInfoCommand.java | 7 ++-- .../listeners/SlashCommandListener.java | 13 +++++-- .../runnables/CommandsUpdateTask.java | 25 -------------- .../hidekobot/util/SlashCommandUtil.java | 7 ---- 6 files changed, 22 insertions(+), 68 deletions(-) delete mode 100644 src/main/java/wtf/beatrice/hidekobot/runnables/CommandsUpdateTask.java diff --git a/src/main/java/wtf/beatrice/hidekobot/Cache.java b/src/main/java/wtf/beatrice/hidekobot/Cache.java index efb596e..077daba 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Cache.java +++ b/src/main/java/wtf/beatrice/hidekobot/Cache.java @@ -1,18 +1,15 @@ package wtf.beatrice.hidekobot; -import net.dv8tion.jda.api.interactions.commands.Command; import org.jetbrains.annotations.Nullable; import wtf.beatrice.hidekobot.datasource.ConfigurationSource; import wtf.beatrice.hidekobot.datasource.DatabaseSource; import wtf.beatrice.hidekobot.listeners.MessageLogger; +import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.util.Logger; import java.awt.*; import java.lang.reflect.Field; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; public class Cache { @@ -36,7 +33,8 @@ public class Cache private static final String botVersion = "0.2.0-slash"; // we should probably find a way to make this consistent with Maven private static final String botName = "HidekoBot"; - private static List registeredCommands = new ArrayList<>(); + + private static SlashCommandListener slashCommandListener = null; private final static String defaultInviteLink = "https://discord.com/api/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8"; @@ -202,32 +200,14 @@ public class Cache return color == null ? defaultColor : color; } - /** - * Set the list of registered commands. They will be sorted alphabetically. - * - * @param commands a list of registered commands. - */ - public static void setRegisteredCommands(List commands) + + public static void setSlashCommandListener(SlashCommandListener commandListener) { - // sort alphabetically by field getName() - List tempList = commands - .stream() - .sorted(Comparator.comparing(Command::getName)) - .toList(); - - registeredCommands = new ArrayList<>(tempList); + slashCommandListener = commandListener; } - /** - * Get a list of all bot registered commands, sorted alphabetically. - * - * @return a copy of the List. - */ - public static List getRegisteredCommands() - { - return new ArrayList<>(registeredCommands); - } + public static SlashCommandListener getSlashCommandListener() { return slashCommandListener; } /** * Set the bot's startup time. Generally only used at boot time. diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index fe9f6fe..013aeef 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -13,7 +13,6 @@ import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener; import wtf.beatrice.hidekobot.listeners.MessageListener; import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; -import wtf.beatrice.hidekobot.runnables.CommandsUpdateTask; import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask; import wtf.beatrice.hidekobot.runnables.HeartBeatTask; import wtf.beatrice.hidekobot.util.Logger; @@ -109,6 +108,7 @@ public class HidekoBot slashCommandListener.registerCommand(new InviteCommand()); slashCommandListener.registerCommand(new PingCommand()); slashCommandListener.registerCommand(new SayCommand()); + Cache.setSlashCommandListener(slashCommandListener); // register listeners jda.addEventListener(new MessageListener()); @@ -145,8 +145,6 @@ public class HidekoBot ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // todo: try-with-resources ExpiredMessageTask expiredMessageTask = new ExpiredMessageTask(); scheduler.scheduleAtFixedRate(expiredMessageTask, 5, 5, TimeUnit.SECONDS); //every 5 seconds - CommandsUpdateTask commandsUpdateTask = new CommandsUpdateTask(); - scheduler.scheduleAtFixedRate(commandsUpdateTask, 10, 300, TimeUnit.SECONDS); //every 5 minutes HeartBeatTask heartBeatTask = new HeartBeatTask(); scheduler.scheduleAtFixedRate(heartBeatTask, 10, 30, TimeUnit.SECONDS); //every 30 seconds 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 91dc1a6..45020e0 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java @@ -2,7 +2,6 @@ package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import net.dv8tion.jda.api.interactions.commands.Command; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; @@ -27,7 +26,7 @@ public class BotInfoCommand implements SlashCommand // defer reply because this might take a moment event.deferReply().queue(); - List registeredCommands = Cache.getRegisteredCommands(); + List registeredCommands = Cache.getSlashCommandListener().getRegisteredCommands(); EmbedBuilder embedBuilder = new EmbedBuilder(); @@ -51,8 +50,8 @@ public class BotInfoCommand implements SlashCommand commandsListBuilder.append(registeredCommands.size()).append( " total - "); for(int i = 0; i < registeredCommands.size(); i++) { - Command cmd = registeredCommands.get(i); - commandsListBuilder.append("`" + cmd.getName() + "`"); + SlashCommand cmd = registeredCommands.get(i); + commandsListBuilder.append("`").append(cmd.getCommandName()).append("`"); if(i + 1 != registeredCommands.size()) // don't add comma in last iteration { diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index dd16e35..0d6920f 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -5,12 +5,13 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.objects.SlashCommand; -import java.util.HashMap; +import java.util.LinkedList; +import java.util.TreeMap; public class SlashCommandListener extends ListenerAdapter { - HashMap registeredCommands = new HashMap<>(); + TreeMap registeredCommands = new TreeMap<>(); public void registerCommand(SlashCommand command) { @@ -18,6 +19,14 @@ public class SlashCommandListener extends ListenerAdapter registeredCommands.put(command.getCommandName(), command); } + public SlashCommand getRegisteredCommand(String label) + { + return registeredCommands.get(label); + } + + public LinkedList getRegisteredCommands() + { return new LinkedList<>(registeredCommands.values()); } + @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { diff --git a/src/main/java/wtf/beatrice/hidekobot/runnables/CommandsUpdateTask.java b/src/main/java/wtf/beatrice/hidekobot/runnables/CommandsUpdateTask.java deleted file mode 100644 index 661827b..0000000 --- a/src/main/java/wtf/beatrice/hidekobot/runnables/CommandsUpdateTask.java +++ /dev/null @@ -1,25 +0,0 @@ -package wtf.beatrice.hidekobot.runnables; - -import net.dv8tion.jda.api.JDA; -import wtf.beatrice.hidekobot.Cache; -import wtf.beatrice.hidekobot.HidekoBot; -import wtf.beatrice.hidekobot.util.Logger; - -public class CommandsUpdateTask implements Runnable { - - private final Logger logger; - - public CommandsUpdateTask() - { - logger = new Logger(getClass()); - } - - @Override - public void run() { - if(Cache.isVerbose()) logger.log("Refreshing commands cache..."); - JDA instance = HidekoBot.getAPI(); - if(instance == null) return; - Cache.setRegisteredCommands(instance.retrieveCommands().complete()); - if(Cache.isVerbose()) logger.log("Commands cache refreshed!"); - } -} diff --git a/src/main/java/wtf/beatrice/hidekobot/util/SlashCommandUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/SlashCommandUtil.java index 9646c2a..2efcb27 100644 --- a/src/main/java/wtf/beatrice/hidekobot/util/SlashCommandUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/util/SlashCommandUtil.java @@ -7,7 +7,6 @@ import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; -import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; import wtf.beatrice.hidekobot.listeners.MessageListener; @@ -123,11 +122,5 @@ public class SlashCommandUtil jdaInstance.updateCommands().addCommands(allCommands).queue(); logger.log("Commands updated. New total: " + allCommands.size() + "."); } - - // cache the registered commands. - // note that if this is the first time the bot runs after updating commands, - // this will probably still return the previous configuration because the discord api - // needs to propagate. this is why we also set up a command updater task (ExpiredMessageTask). - Cache.setRegisteredCommands(jdaInstance.retrieveCommands().complete()); } }