Implement verbosity-changer command at runtime
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Bea 2022-08-26 20:43:12 +02:00
parent d085a671c5
commit ff4ffba45d
3 changed files with 74 additions and 12 deletions

View File

@ -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; }
}

View File

@ -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<String> 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<String> 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);

View File

@ -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;
}
}
}