From 3474593dc960d8cee3d00604ebbed023301ec066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 20 Nov 2022 18:54:13 +0100 Subject: [PATCH] Implement command force-refresh arg --- README.MD | 3 ++- .../wtf/beatrice/hidekobot/HidekoBot.java | 26 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.MD b/README.MD index fdf6f8f..7b389f6 100644 --- a/README.MD +++ b/README.MD @@ -10,7 +10,8 @@ java -jar HidekoBot.jar [additional parameters] Where `HidekoBot.jar` is the executable archive and `` is your bot token passed as an argument. Additionally available parameters are: - - **verbose**: log every message that the bot receives. Very spammy and performance heavy. + - **verbose**: log every message that the bot receives. Very spammy and performance heavy. + - **refresh**: force refresh the bot's commands. *Note: Java 16 or later is required.* diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 0af5be7..9936aa6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -7,6 +7,7 @@ import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.requests.GatewayIntent; import sun.misc.Signal; import wtf.beatrice.hidekobot.listeners.MessageListener; +import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.utils.Logger; import wtf.beatrice.hidekobot.utils.SlashCommandsUtil; @@ -64,16 +65,8 @@ public class HidekoBot String botUserId = jda.getSelfUser().getId(); Configuration.setBotApplicationId(botUserId); - // register listeners - jda.addEventListener(new MessageListener()); - jda.addEventListener(new SlashCommandListener()); + boolean forceUpdateCommands = false; - // update slash commands (delayed) - Executors.newSingleThreadScheduledExecutor().schedule(SlashCommandsUtil::updateSlashCommands, 1, TimeUnit.SECONDS); - - // set the bot's status - jda.getPresence().setStatus(OnlineStatus.ONLINE); - jda.getPresence().setActivity(Activity.playing("Hatsune Miku: Project DIVA")); // if there is more than 1 arg, then iterate through them because we have additional things to do. // we are doing this at the end because we might need the API to be already initialized for some things. @@ -81,8 +74,23 @@ public class HidekoBot List argsList = new ArrayList<>(Arrays.asList(args).subList(1, args.length)); if(argsList.contains("verbose")) Configuration.setVerbose(true); + if(argsList.contains("refresh")) forceUpdateCommands = true; } + // register listeners + jda.addEventListener(new MessageListener()); + jda.addEventListener(new SlashCommandListener()); + jda.addEventListener(new SlashCommandCompleter()); + + // update slash commands (delayed) + final boolean finalForceUpdateCommands = forceUpdateCommands; + Executors.newSingleThreadScheduledExecutor().schedule(() -> + SlashCommandsUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS); + + // set the bot's status + jda.getPresence().setStatus(OnlineStatus.ONLINE); + jda.getPresence().setActivity(Activity.playing("Hatsune Miku: Project DIVA")); + // print the bot logo. logger.log("\n\n" + logger.getLogo() + "\nv" + version + " - bot is ready!\n", 2);