From ff4ffba45d5eb9c6e380fffc9f09cccce1b55c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Fri, 26 Aug 2022 20:43:12 +0200 Subject: [PATCH] Implement verbosity-changer command at runtime --- .../wtf/beatrice/hidekobot/Configuration.java | 49 ++++++++++++++++++- .../wtf/beatrice/hidekobot/HidekoBot.java | 20 ++++---- .../hidekobot/listeners/MessageListener.java | 17 +++++++ 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/Configuration.java b/src/main/java/wtf/beatrice/hidekobot/Configuration.java index 7107efe..c5297d2 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Configuration.java +++ b/src/main/java/wtf/beatrice/hidekobot/Configuration.java @@ -1,19 +1,64 @@ package wtf.beatrice.hidekobot; +import wtf.beatrice.hidekobot.listeners.MessageLogger; + public class Configuration { private static boolean verbose = false; + private static MessageLogger verbosityLogger; private static boolean paused = false; + private static String prefix = "."; + /** + * Checks if the bot has been started with the verbose argument. + * + * @return a boolean which is true if the bot is in verbose-mode + */ public static boolean isVerbose() { return verbose; } - // WARNING: verbosity spams the logs a LOT! - public static void setVerbose(boolean v) { verbose = v; } + /** + * Set the bot's verbosity status at runtime. + * This also registers or unregisters the message-logger listener. + * + * @param v the verbosity boolean value + */ + public static void setVerbose(boolean v) + { + verbose = v; + if(v) + { + if(verbosityLogger == null) + { + verbosityLogger = new MessageLogger(); + } + + HidekoBot.getAPI().addEventListener(verbosityLogger); + } else { + if(verbosityLogger != null) + { + HidekoBot.getAPI().removeEventListener(verbosityLogger); + verbosityLogger = null; + } + } + } + + /** + * Checks if the bot has paused all operation. + * + * @return a boolean which is true if the bot is currently paused + */ public static boolean isPaused() { return paused; } + + /** + * Set the bot in paused or unpaused state. + * Paused means that it will not reply to anything expect the unpause command. + * + * @param p a boolean specifying if the bot should be paused + */ public static void setPaused(boolean p) { paused = p; } } diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index d1045c4..527638b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -40,15 +40,6 @@ public class HidekoBot // load token from args botToken = args[0]; - // if there are more than 1 args, then iterate through them because we have additional things to do - if(args.length > 1) { - List argsList = new ArrayList<>(); - for(int i = 1; i < args.length; i++) - { argsList.add(args[i]); } - - if(argsList.contains("verbose")) Configuration.setVerbose(true); - } - try { // try to create the bot object and authenticate it with discord. @@ -74,12 +65,21 @@ public class HidekoBot // register listeners jda.addEventListener(new MessageListener()); - if(Configuration.isVerbose()) jda.addEventListener(new MessageLogger()); // set the bot's status jda.getPresence().setStatus(OnlineStatus.ONLINE); jda.getPresence().setActivity(Activity.playing("Hatsune Miku: Project DIVA")); + // if there are more than 1 args, 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. + if(args.length > 1) { + List argsList = new ArrayList<>(); + for(int i = 1; i < args.length; i++) + { argsList.add(args[i]); } + + if(argsList.contains("verbose")) Configuration.setVerbose(true); + } + // print the bot logo. logger.log("Ready!\n\n" + logger.getLogo() + "\nv" + version + " - bot is ready!\n", 2); diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java index ad5321c..83b2509 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java @@ -120,5 +120,22 @@ public class MessageListener extends ListenerAdapter return; } + + if(eventMessage.equalsIgnoreCase("hideko verbose")) + { + MessageChannel channel = event.getChannel(); + + boolean verbose = Configuration.isVerbose(); + + String msg = verbose ? "off" : "on"; + msg = "Turning verbosity " + msg + "!"; + + Configuration.setVerbose(!verbose); + + channel.sendMessage(msg).queue(); + logger.log(msg); + + return; + } } }