From d13e11eac5b38e2c01421905b46608e7f674727c Mon Sep 17 00:00:00 2001 From: anthorob Date: Tue, 15 Mar 2022 19:41:12 -0400 Subject: [PATCH] Make debug logging configurable --- .../command/bot/ConfigureCommand.java | 58 +++++++++++++++---- .../command/music/VolumeCommand.java | 13 +++-- .../java/netneutrality/core/GuildManager.java | 14 +++++ 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/main/java/netneutrality/command/bot/ConfigureCommand.java b/src/main/java/netneutrality/command/bot/ConfigureCommand.java index 0474c82..a89d60f 100644 --- a/src/main/java/netneutrality/command/bot/ConfigureCommand.java +++ b/src/main/java/netneutrality/command/bot/ConfigureCommand.java @@ -41,7 +41,7 @@ public class ConfigureCommand extends AbstractCommand { @Override public CommandUsage getUsage() { return CommandUsage.create(this, - new CommandUsage.Argument(CommandUsage.Argument.Type.REQUIRED, new String[] {"musicrole", "musicvolume"}, "") + new CommandUsage.Argument(CommandUsage.Argument.Type.REQUIRED, new String[] {"musicrole", "musicvolume", "debug"}, "") ); } @@ -62,8 +62,8 @@ public class ConfigureCommand extends AbstractCommand { if (args.length > 0) { + // Role for music commands if (context.getArg(0).equals("musicrole")) { - if (args.length == 1) { String roleDb = guildManager.getSetting(GuildManager.Settings.MUSIC_ROLE); Role role = null; @@ -72,11 +72,19 @@ public class ConfigureCommand extends AbstractCommand { } context.reply("Current role needed to use music commands (default is everyone): %s", (role == null ? "Everyone" : role.getAsMention())); + context.reply("Reset role using 'conf musicrole reset' or set it using 'conf musicrole @'"); } if (args.length == 2) { - List roles = context.getMessage().getMentionedRoles(); + if (context.getArg(1).equals("reset")) { + if (!guildManager.getSetting(GuildManager.Settings.MUSIC_ROLE).isEmpty()) { + guildManager.setSettingValue(GuildManager.Settings.MUSIC_ROLE, null); + } + context.reply("Successful reset of the role"); + } + + List roles = context.getMessage().getMentionedRoles(); if (roles.size() == 1) { Role role = roles.get(0); if (role.getGuild().getId().equals(context.getGuild().getId())) { @@ -84,32 +92,58 @@ public class ConfigureCommand extends AbstractCommand { context.reply("Successfully set %s as the role needed for music command", role.getAsMention()); } } - - if (context.getArg(1).equals("reset")) { - guildManager.setSettingValue(GuildManager.Settings.MUSIC_ROLE, null); - context.reply("Successful reset of the role"); - } } } + // Default volume for music player if (context.getArg(0).equals("musicvolume")) { if (args.length == 1) { int volume = Integer.parseInt(guildManager.getSetting(GuildManager.Settings.MUSIC_VOLUME)); context.reply("Default volume for music player: " + volume); + context.reply("Set using 'conf musicvolume '"); } if (args.length == 2) { try { int volume = Integer.parseInt(args[1]); - guildManager.setSettingValue(GuildManager.Settings.MUSIC_VOLUME, String.valueOf(volume)); - MusicManager.get(context.getGuild()).setVolume(volume); - context.reply("Default volume for music player is now %d", volume); + if (volume > 0 && volume <= 100) { + guildManager.setSettingValue(GuildManager.Settings.MUSIC_VOLUME, String.valueOf(volume)); + MusicManager.get(context.getGuild()).setVolume(volume); + + context.reply("Default volume for music player is now %d", volume); + } else + throw new NumberFormatException(); + } catch (NumberFormatException e) { - throw new CommandErrorException("Not a value between 0 and 100", context, e); + context.reply("Not a value between 0 and 100"); + } + } + } + + // Debug + if (context.getArg(0).equals("debug")) { + if (args.length == 1) { + boolean debug = guildManager.isDebugEnabled(); + + context.reply("Error logging is currently %s for this guild.", (debug ? "enabled" : "disabled")); + context.reply("Use 'conf debug 0/1' to toggle between states."); + } + + if (args.length == 2) { + String str = args[1]; + boolean debug = guildManager.isDebugEnabled(); + if (str.equals("0") || str.equalsIgnoreCase("false")) { + debug = false; } + if (str.equals("1") || str.equalsIgnoreCase("true")) { + debug = true; + } + + guildManager.setDebug(debug); + context.reply("Error logging is now %s for this guild", (debug ? "enabled" : "disabled")); } } diff --git a/src/main/java/netneutrality/command/music/VolumeCommand.java b/src/main/java/netneutrality/command/music/VolumeCommand.java index bd03f77..e7f12cb 100644 --- a/src/main/java/netneutrality/command/music/VolumeCommand.java +++ b/src/main/java/netneutrality/command/music/VolumeCommand.java @@ -51,11 +51,16 @@ public class VolumeCommand extends AbstractMusicCommand { return; try { - int volume = Integer.parseInt(context.getArgumentsAsArray()[0]); - player.setVolume(volume); - context.reply("Volume set to %s", volume); + int volume = Integer.parseInt(context.getArg(0)); + + if (volume > 0 && volume <= 100) { + player.setVolume(volume); + context.reply("Volume set to %s", volume); + } else + throw new NumberFormatException(); + } catch (NumberFormatException e) { - throw new CommandErrorException("Please enter a value from 0 to 100", context, e); + context.reply("Please enter a value from 0 to 100"); } } diff --git a/src/main/java/netneutrality/core/GuildManager.java b/src/main/java/netneutrality/core/GuildManager.java index 93b8d7b..ea06606 100644 --- a/src/main/java/netneutrality/core/GuildManager.java +++ b/src/main/java/netneutrality/core/GuildManager.java @@ -174,6 +174,20 @@ public class GuildManager { session.close(); } + public void setDebug(boolean toggle) { + if (toggle == isDebugEnabled()) + return; + + Session session = sessionFactory.openSession(); + session.beginTransaction(); + + guildEntity.setDebug(toggle); + + session.update(guildEntity); + session.getTransaction().commit(); + session.close(); + } + public static boolean reloadSettingsForGuild(String guildId) { GuildManager manager = GuildManager.getManager(guildId); manager.reloadSettingsCache(true);