From 163619a7f825a4636d0773d8a65ceb7f93dbddf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 21 Nov 2022 16:24:09 +0100 Subject: [PATCH] Improve registered commands caching Discord's API is slow in updating and registering new commands, so we set up a runnable to periodically check. --- .../wtf/beatrice/hidekobot/Configuration.java | 4 +-- .../wtf/beatrice/hidekobot/HidekoBot.java | 13 +++++----- .../runnables/CommandsUpdateTask.java | 25 +++++++++++++++++++ .../hidekobot/utils/SlashCommandUtil.java | 3 +++ 4 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/runnables/CommandsUpdateTask.java diff --git a/src/main/java/wtf/beatrice/hidekobot/Configuration.java b/src/main/java/wtf/beatrice/hidekobot/Configuration.java index 51a47c6..7c4d810 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Configuration.java +++ b/src/main/java/wtf/beatrice/hidekobot/Configuration.java @@ -37,7 +37,7 @@ public class Configuration private static final String botName = "HidekoBot"; private static final Color botColor = Color.PINK; - private static final List registeredCommands = new ArrayList<>(); + private static List registeredCommands = new ArrayList<>(); private final static String defaultInviteLink = "https://discord.com/api/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8"; @@ -184,7 +184,7 @@ public class Configuration .sorted(Comparator.comparing(Command::getName)) .toList(); - registeredCommands.addAll(tempList); + registeredCommands = new ArrayList<>(tempList); } /** diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 486cbfb..d77bf72 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -11,6 +11,7 @@ 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.utils.Logger; import wtf.beatrice.hidekobot.utils.SlashCommandUtil; @@ -90,7 +91,7 @@ public class HidekoBot // update slash commands (delayed) final boolean finalForceUpdateCommands = forceUpdateCommands; - Executors.newSingleThreadScheduledExecutor().schedule(() -> + Executors.newSingleThreadScheduledExecutor().schedule(() -> // todo: try-with-resources SlashCommandUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS); // set the bot's status @@ -114,11 +115,11 @@ public class HidekoBot } // start scheduled runnables - ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); - ExpiredMessageTask task = new ExpiredMessageTask(); - int initDelay = 5; - int periodicDelay = 5; - scheduler.scheduleAtFixedRate(task, initDelay, periodicDelay, TimeUnit.SECONDS); + 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 // register shutdown interrupt signal listener for proper shutdown. Signal.handle(new Signal("INT"), signal -> shutdown()); diff --git a/src/main/java/wtf/beatrice/hidekobot/runnables/CommandsUpdateTask.java b/src/main/java/wtf/beatrice/hidekobot/runnables/CommandsUpdateTask.java new file mode 100644 index 0000000..4384990 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/runnables/CommandsUpdateTask.java @@ -0,0 +1,25 @@ +package wtf.beatrice.hidekobot.runnables; + +import net.dv8tion.jda.api.JDA; +import wtf.beatrice.hidekobot.Configuration; +import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.utils.Logger; + +public class CommandsUpdateTask implements Runnable { + + private final Logger logger; + + public CommandsUpdateTask() + { + logger = new Logger(getClass()); + } + + @Override + public void run() { + if(Configuration.isVerbose()) logger.log("Refreshing commands cache..."); + JDA instance = HidekoBot.getAPI(); + if(instance == null) return; + Configuration.setRegisteredCommands(instance.retrieveCommands().complete()); + if(Configuration.isVerbose()) logger.log("Commands cache refreshed!"); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java index 80102f7..4c97ee4 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java @@ -125,6 +125,9 @@ public class SlashCommandUtil } // 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). Configuration.setRegisteredCommands(jdaInstance.retrieveCommands().complete()); } }