From 6365be253693a98a9e71290c8a479ee8507f7f0d Mon Sep 17 00:00:00 2001 From: danthe1st Date: Mon, 11 Sep 2023 10:53:18 +0200 Subject: [PATCH 1/2] automatically determine quiet based on channel --- .../systems/moderation/BanCommand.java | 3 +- .../systems/moderation/KickCommand.java | 3 +- .../moderation/ModerateUserCommand.java | 29 +++++++++++++++++++ .../systems/moderation/UnbanCommand.java | 3 +- .../timeout/AddTimeoutSubcommand.java | 3 +- .../timeout/RemoveTimeoutSubcommand.java | 3 +- .../moderation/warn/WarnAddSubcommand.java | 8 ++++- 7 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/BanCommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/BanCommand.java index af5f3f0cf..ceafcf94a 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/BanCommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/BanCommand.java @@ -5,7 +5,6 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.Commands; import net.javadiscord.javabot.data.config.BotConfig; @@ -52,7 +51,7 @@ protected WebhookMessageCreateAction handleModerationUserCommand(@Nonnu if (!Checks.hasPermission(event.getGuild(), Permission.BAN_MEMBERS)) { return Responses.replyInsufficientPermissions(event.getHook(), Permission.BAN_MEMBERS); } - boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean); + boolean quiet = isQuiet(event); ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool); service.ban(target, reason, commandUser, event.getChannel(), quiet); return Responses.success(event.getHook(), "User Banned", "%s has been banned.", target.getAsMention()); diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/KickCommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/KickCommand.java index ba095f7fd..3b9722dd9 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/KickCommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/KickCommand.java @@ -5,7 +5,6 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.Commands; import net.javadiscord.javabot.data.config.BotConfig; @@ -52,7 +51,7 @@ protected WebhookMessageCreateAction handleModerationUserCommand(@Nonnu if (!Checks.hasPermission(event.getGuild(), Permission.KICK_MEMBERS)) { return Responses.replyInsufficientPermissions(event.getHook(), Permission.KICK_MEMBERS); } - boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean); + boolean quiet = isQuiet(event); ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool); service.kick(target, reason, event.getMember(), event.getChannel(), quiet); return Responses.success(event.getHook(), "User Kicked", "%s has been kicked.", target.getAsMention()); diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/ModerateUserCommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/ModerateUserCommand.java index 0b82ecb60..7dce08e35 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/ModerateUserCommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/ModerateUserCommand.java @@ -49,4 +49,33 @@ protected ReplyCallbackAction handleModerationCommand(@NotNull SlashCommandInter } protected abstract WebhookMessageCreateAction handleModerationUserCommand(@Nonnull SlashCommandInteractionEvent event, @Nonnull Member commandUser, @Nonnull User target, @Nullable String reason); + + /** + * Determines whether this command is executed quitely. + * + * If it is, no (public) response should be sent in the current channel. This does not effect logging. + * + * By default, moderative actions in the log channel are quiet. + * @param event The {@link SlashCommandInteractionEvent} corresponding to the executed command + * @return {@code true} iff the command is quiet, else {@code false} + */ + protected boolean isQuiet(SlashCommandInteractionEvent event) { + return isQuiet(botConfig, event); + } + + /** + * Determines whether this command is executed quitely. + * + * If it is, no (public) response should be sent in the current channel. This does not effect logging. + * + * By default, moderative actions in the log channel are quiet. + * @param botConfig the main configuration of the bot + * @param event The {@link SlashCommandInteractionEvent} corresponding to the executed command + * @return {@code true} iff the command is quiet, else {@code false} + */ + public static boolean isQuiet(BotConfig botConfig, SlashCommandInteractionEvent event) { + return event.getOption("quiet", + () -> event.getChannel().getIdLong() == botConfig.get(event.getGuild()).getModerationConfig().getLogChannelId(), + OptionMapping::getAsBoolean); + } } diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/UnbanCommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/UnbanCommand.java index 7b1f93b0d..59ef74abb 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/UnbanCommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/UnbanCommand.java @@ -40,6 +40,7 @@ public UnbanCommand(NotificationService notificationService, BotConfig botConfig setModerationSlashCommandData(Commands.slash("unban", "Unbans a member") .addOption(OptionType.STRING, "id", "The id of the user you want to unban", true) .addOption(OptionType.STRING, "reason", "The reason for unbanning this user", true) + .addOption(OptionType.BOOLEAN, "quiet", "If true, don't send a message in the server channel where the unban is issued.", false) ); } @@ -51,7 +52,7 @@ protected ReplyCallbackAction handleModerationCommand(@NotNull SlashCommandInter return Responses.replyMissingArguments(event); } long id = idOption.getAsLong(); - boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean); + boolean quiet = ModerateUserCommand.isQuiet(botConfig, event); ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool); if (service.unban(id, reasonOption.getAsString(), event.getMember(), event.getChannel(), quiet)) { return Responses.success(event, "User Unbanned", "User with id `%s` has been unbanned.", id); diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/timeout/AddTimeoutSubcommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/timeout/AddTimeoutSubcommand.java index e09a4c819..3b352f5b6 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/timeout/AddTimeoutSubcommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/timeout/AddTimeoutSubcommand.java @@ -10,6 +10,7 @@ import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import net.javadiscord.javabot.data.config.BotConfig; +import net.javadiscord.javabot.systems.moderation.ModerateUserCommand; import net.javadiscord.javabot.systems.moderation.ModerationService; import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository; import net.javadiscord.javabot.systems.notification.NotificationService; @@ -75,7 +76,7 @@ protected ReplyCallbackAction handleTimeoutCommand(@NotNull SlashCommandInteract return Responses.error(event, "This command can only be performed in a server message channel."); } MessageChannel channel = event.getMessageChannel(); - boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean); + boolean quiet = ModerateUserCommand.isQuiet(botConfig, event); if (member.isTimedOut()) { return Responses.error(event, "Could not timeout %s; they're already timed out.", member.getAsMention()); } diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/timeout/RemoveTimeoutSubcommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/timeout/RemoveTimeoutSubcommand.java index 6f66573cf..2df121cbf 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/timeout/RemoveTimeoutSubcommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/timeout/RemoveTimeoutSubcommand.java @@ -9,6 +9,7 @@ import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import net.javadiscord.javabot.data.config.BotConfig; +import net.javadiscord.javabot.systems.moderation.ModerateUserCommand; import net.javadiscord.javabot.systems.moderation.ModerationService; import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository; import net.javadiscord.javabot.systems.notification.NotificationService; @@ -59,7 +60,7 @@ protected ReplyCallbackAction handleTimeoutCommand(@NotNull SlashCommandInteract if (!channel.getType().isMessage()) { return Responses.error(event, "This command can only be performed in a server message channel."); } - boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean); + boolean quiet = ModerateUserCommand.isQuiet(botConfig, event); if (!member.isTimedOut()) { return Responses.error(event, "Could not remove timeout from member %s; they're not timed out.", member.getAsMention()); } diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java index 7a4c7de45..d2f4a8c63 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java @@ -8,10 +8,12 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.javadiscord.javabot.data.config.BotConfig; +import net.javadiscord.javabot.systems.moderation.ModerateUserCommand; import net.javadiscord.javabot.systems.moderation.ModerationService; import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository; import net.javadiscord.javabot.systems.moderation.warn.model.WarnSeverity; import net.javadiscord.javabot.systems.notification.NotificationService; +import net.javadiscord.javabot.util.Checks; import net.javadiscord.javabot.util.Responses; import java.util.concurrent.ExecutorService; @@ -66,13 +68,17 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { Responses.replyGuildOnly(event).queue(); return; } + if(!Checks.hasStaffRole(botConfig, event.getMember())) { + Responses.replyStaffOnly(event, botConfig.get(event.getGuild())); + return; + } User target = userMapping.getAsUser(); WarnSeverity severity = WarnSeverity.valueOf(severityMapping.getAsString().trim().toUpperCase()); if (target.isBot()) { Responses.warning(event, "You cannot warn bots.").queue(); return; } - boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean); + boolean quiet = ModerateUserCommand.isQuiet(botConfig, event); ModerationService service = new ModerationService(notificationService, botConfig, event, warnRepository, asyncPool); service.warn(target, severity, reasonMapping.getAsString(), event.getMember(), event.getChannel(), quiet); Responses.success(event, "User Warned", "%s has been successfully warned.", target.getAsMention()).queue(); From dd758d2141cb6704f2366152b12caa5df1b91f8b Mon Sep 17 00:00:00 2001 From: danthe1st Date: Mon, 11 Sep 2023 10:59:47 +0200 Subject: [PATCH 2/2] replies for /warn for non-staff members --- .../systems/moderation/warn/DiscardAllWarnsSubcommand.java | 5 +++++ .../systems/moderation/warn/DiscardWarnByIdSubCommand.java | 5 +++++ .../javabot/systems/moderation/warn/WarnAddSubcommand.java | 2 +- .../systems/moderation/warn/WarnExportSubcommand.java | 5 +++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardAllWarnsSubcommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardAllWarnsSubcommand.java index 9ddace733..a1dc91ae2 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardAllWarnsSubcommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardAllWarnsSubcommand.java @@ -11,6 +11,7 @@ import net.javadiscord.javabot.systems.moderation.ModerationService; import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository; import net.javadiscord.javabot.systems.notification.NotificationService; +import net.javadiscord.javabot.util.Checks; import net.javadiscord.javabot.util.Responses; import java.util.concurrent.ExecutorService; @@ -54,6 +55,10 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { Responses.replyGuildOnly(event).queue(); return; } + if(!Checks.hasStaffRole(botConfig, event.getMember())) { + Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue(); + return; + } User target = userMapping.getAsUser(); new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool).discardAllWarns(target, event.getMember()); Responses.success(event, "Warns Discarded", "Successfully discarded all warns from **%s**.", UserUtils.getUserTag(target)).queue(); diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardWarnByIdSubCommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardWarnByIdSubCommand.java index ab1c81104..6a23e7ac8 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardWarnByIdSubCommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardWarnByIdSubCommand.java @@ -9,6 +9,7 @@ import net.javadiscord.javabot.systems.moderation.ModerationService; import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository; import net.javadiscord.javabot.systems.notification.NotificationService; +import net.javadiscord.javabot.util.Checks; import net.javadiscord.javabot.util.Responses; import java.util.concurrent.ExecutorService; @@ -52,6 +53,10 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { Responses.replyGuildOnly(event).queue(); return; } + if(!Checks.hasStaffRole(botConfig, event.getMember())) { + Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue(); + return; + } int id = idMapping.getAsInt(); ModerationService service = new ModerationService(notificationService, botConfig, event, warnRepository, asyncPool); if (service.discardWarnById(id, event.getUser())) { diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java index d2f4a8c63..a94c36170 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java @@ -69,7 +69,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { return; } if(!Checks.hasStaffRole(botConfig, event.getMember())) { - Responses.replyStaffOnly(event, botConfig.get(event.getGuild())); + Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue(); return; } User target = userMapping.getAsUser(); diff --git a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnExportSubcommand.java b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnExportSubcommand.java index 6682bdf46..c4d31732d 100644 --- a/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnExportSubcommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnExportSubcommand.java @@ -15,6 +15,7 @@ import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository; import net.javadiscord.javabot.systems.moderation.warn.model.Warn; import net.javadiscord.javabot.systems.notification.NotificationService; +import net.javadiscord.javabot.util.Checks; import net.javadiscord.javabot.util.ExceptionLogger; import net.javadiscord.javabot.util.Responses; @@ -71,6 +72,10 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { Responses.replyGuildOnly(event).queue(); return; } + if(!Checks.hasStaffRole(botConfig, event.getMember())) { + Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue(); + return; + } User target = userMapping.getAsUser(); ModerationService service = new ModerationService(notificationService, botConfig, event, warnRepository, asyncPool); List warns = service.getAllWarns(target.getIdLong());