From 3dafa0dd8ac8c6a52786bd2094d85650492450fa Mon Sep 17 00:00:00 2001 From: Taz Date: Mon, 15 Aug 2022 12:06:09 +0530 Subject: [PATCH 01/21] removed unnecessary checks from AskCommand --- .../tjbot/commands/help/AskCommand.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 63f0b23951..b02f98c8e0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -77,10 +77,6 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { String title = event.getOption(TITLE_OPTION).getAsString(); String category = event.getOption(CATEGORY_OPTION).getAsString(); - if (!handleIsStagingChannel(event)) { - return; - } - if (!handleIsValidTitle(title, event)) { return; } @@ -104,18 +100,6 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { }, e -> handleFailure(e, eventHook)); } - private boolean handleIsStagingChannel(@NotNull IReplyCallback event) { - if (helper.isStagingChannelName(event.getChannel().getName())) { - return true; - } - - event.reply("Sorry, but this command can only be used in the help staging channel.") - .setEphemeral(true) - .queue(); - - return false; - } - private boolean handleIsValidTitle(@NotNull CharSequence title, @NotNull IReplyCallback event) { if (HelpSystemHelper.isTitleValid(title)) { return true; From 562119cbf9be7b1269329216c7a3cfc777a98fe4 Mon Sep 17 00:00:00 2001 From: Taz Date: Mon, 15 Aug 2022 12:10:59 +0530 Subject: [PATCH 02/21] removed unnecessary checks from ChangeHelpCategoryCommand --- .../tjbot/commands/help/ChangeHelpCategoryCommand.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java index 576890ecb3..9bf24e54f0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java @@ -71,10 +71,6 @@ public ChangeHelpCategoryCommand(@NotNull Config config, @NotNull HelpSystemHelp public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { String category = event.getOption(CATEGORY_OPTION).getAsString(); - if (!helper.handleIsHelpThread(event)) { - return; - } - ThreadChannel helpThread = event.getThreadChannel(); if (helpThread.isArchived()) { event.reply("This thread is already closed.").setEphemeral(true).queue(); From b756e1a1d64667be27c3b4011cb75104dcc61df0 Mon Sep 17 00:00:00 2001 From: Taz Date: Mon, 15 Aug 2022 12:12:31 +0530 Subject: [PATCH 03/21] removed unnecessary checks from ChangeHelpTitleCommand --- .../tjbot/commands/help/ChangeHelpTitleCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java index 14a6d0cb5b..5090f26d07 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java @@ -58,7 +58,7 @@ public ChangeHelpTitleCommand(@NotNull HelpSystemHelper helper) { public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { String title = event.getOption(TITLE_OPTION).getAsString(); - if (!helper.handleIsHelpThread(event) || !handleIsValidTitle(title, event)) { + if (!handleIsValidTitle(title, event)) { return; } From c5481f1ba77666062e37c8f63e30f2ac285f9c21 Mon Sep 17 00:00:00 2001 From: Taz Date: Mon, 15 Aug 2022 12:14:37 +0530 Subject: [PATCH 04/21] removed unnecessary checks from CloseCommand --- .../org/togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/help/CloseCommand.java | 11 +---------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 401171192a..1963b00c7f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -109,7 +109,7 @@ public enum Features { features.add(new WhoIsCommand()); features.add(new WolframAlphaCommand(config)); features.add(new AskCommand(config, helpSystemHelper)); - features.add(new CloseCommand(helpSystemHelper)); + features.add(new CloseCommand()); features.add(new ChangeHelpCategoryCommand(config, helpSystemHelper)); features.add(new ChangeHelpTitleCommand(helpSystemHelper)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java index 14106631ee..437e75131c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java @@ -26,31 +26,22 @@ public final class CloseCommand extends SlashCommandAdapter { private static final int COOLDOWN_DURATION_VALUE = 30; private static final ChronoUnit COOLDOWN_DURATION_UNIT = ChronoUnit.MINUTES; - private final HelpSystemHelper helper; private final Cache helpThreadIdToLastClose; /** * Creates a new instance. - * - * @param helper the helper to use */ - public CloseCommand(@NotNull HelpSystemHelper helper) { + public CloseCommand() { super("close", "Close this question thread", SlashCommandVisibility.GUILD); helpThreadIdToLastClose = Caffeine.newBuilder() .maximumSize(1_000) .expireAfterAccess(COOLDOWN_DURATION_VALUE, TimeUnit.of(COOLDOWN_DURATION_UNIT)) .build(); - - this.helper = helper; } @Override public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { - if (!helper.handleIsHelpThread(event)) { - return; - } - ThreadChannel helpThread = event.getThreadChannel(); if (helpThread.isArchived()) { event.reply("This thread is already closed.").setEphemeral(true).queue(); From 4c33875d473d1fa1b1b60936228c1f9b2bd88625 Mon Sep 17 00:00:00 2001 From: Taz Date: Mon, 15 Aug 2022 12:18:13 +0530 Subject: [PATCH 05/21] removed unused code from HelpSystemHelper --- .../tjbot/commands/help/HelpSystemHelper.java | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index c6bbd74544..46bd5a6533 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -2,7 +2,6 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.*; -import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.requests.restaction.MessageAction; import org.jetbrains.annotations.NotNull; @@ -40,8 +39,6 @@ public final class HelpSystemHelper { private final Predicate isOverviewChannelName; private final String overviewChannelPattern; - private final Predicate isStagingChannelName; - private final String stagingChannelPattern; private final String categoryRoleSuffix; @@ -56,9 +53,6 @@ public HelpSystemHelper(@NotNull Config config) { overviewChannelPattern = helpConfig.getOverviewChannelPattern(); isOverviewChannelName = Pattern.compile(overviewChannelPattern).asMatchPredicate(); - stagingChannelPattern = helpConfig.getStagingChannelPattern(); - isStagingChannelName = Pattern.compile(stagingChannelPattern).asMatchPredicate(); - categoryRoleSuffix = helpConfig.getCategoryRoleSuffix(); } @@ -106,22 +100,6 @@ RestAction sendExplanationMessage(@NotNull MessageChannel threadChannel .build(); } - boolean handleIsHelpThread(@NotNull IReplyCallback event) { - if (event.getChannelType() == ChannelType.GUILD_PUBLIC_THREAD) { - ThreadChannel thread = event.getThreadChannel(); - - if (isOverviewChannelName.test(thread.getParentChannel().getName())) { - return true; - } - } - - event.reply("Sorry, but this command can only be used in a help thread.") - .setEphemeral(true) - .queue(); - - return false; - } - @NotNull Optional handleFindRoleForCategory(@NotNull String category, @NotNull Guild guild) { String roleName = category + categoryRoleSuffix; @@ -165,15 +143,6 @@ String getOverviewChannelPattern() { return overviewChannelPattern; } - boolean isStagingChannelName(@NotNull String channelName) { - return isStagingChannelName.test(channelName); - } - - @NotNull - String getStagingChannelPattern() { - return stagingChannelPattern; - } - static boolean isTitleValid(@NotNull CharSequence title) { String titleCompact = TITLE_COMPACT_REMOVAL_PATTERN.matcher(title).replaceAll(""); From 2afddf4cd3968ab9ec5221ea435c9940446db2d3 Mon Sep 17 00:00:00 2001 From: Taz Date: Mon, 15 Aug 2022 14:21:03 +0530 Subject: [PATCH 06/21] removed unnecessary checks from AuditCommand --- .../org/togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/moderation/AuditCommand.java | 15 ++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 1963b00c7f..0b107600ae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -97,7 +97,7 @@ public enum Features { features.add(new KickCommand(actionsStore, config)); features.add(new BanCommand(actionsStore, config)); features.add(new UnbanCommand(actionsStore, config)); - features.add(new AuditCommand(actionsStore, config)); + features.add(new AuditCommand(actionsStore)); features.add(new MuteCommand(actionsStore, config)); features.add(new UnmuteCommand(actionsStore, config)); features.add(new TopHelpersCommand(database, config)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 6097286232..5af07e0f05 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -18,14 +18,11 @@ import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.config.Config; import java.time.Instant; import java.time.ZoneOffset; import java.util.*; import java.util.function.Function; -import java.util.function.Predicate; -import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -42,24 +39,20 @@ public final class AuditCommand extends SlashCommandAdapter { private static final int MAX_PAGE_LENGTH = 25; private static final String PREVIOUS_BUTTON_LABEL = "⬅"; private static final String NEXT_BUTTON_LABEL = "➡"; - private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; /** * Constructs an instance. * * @param actionsStore used to store actions issued by this command - * @param config the config to use for this */ - public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config config) { + public AuditCommand(@NotNull ModerationActionsStore actionsStore) { super(COMMAND_NAME, "Lists all moderation actions that have been taken against a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, TARGET_OPTION, "The user who to retrieve actions for", true); - hasRequiredRole = - Pattern.compile(config.getHeavyModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -144,11 +137,11 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { private boolean handleChecks(@NotNull Member bot, @NotNull Member author, @Nullable Member target, @NotNull IReplyCallback event) { // Member doesn't exist if attempting to audit a user who is not part of the guild. - if (target != null && !ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, - target, event)) { + if (target == null) { return false; } - return ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event); + return ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, + target, event); } private @NotNull List> groupActionsByPages( From 54426f80b0f32679c28196633b629c49702384a9 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 00:00:10 +0530 Subject: [PATCH 07/21] remove unnecessary checks from ModerationUtils#handleRoleChangeChecks --- .../tjbot/commands/moderation/ModerationUtils.java | 7 +------ .../tjbot/commands/moderation/MuteCommand.java | 4 +--- .../tjbot/commands/moderation/QuarantineCommand.java | 4 +--- .../tjbot/commands/moderation/UnmuteCommand.java | 4 +--- .../tjbot/commands/moderation/UnquarantineCommand.java | 4 +--- 5 files changed, 5 insertions(+), 18 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java index 48119596d4..263642f3ed 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java @@ -185,7 +185,6 @@ private static void handleAbsentTarget(@NotNull String actionVerb, *
  • the target is not member of the guild
  • *
  • the bot or author do not have enough permissions to interact with the target
  • *
  • the bot or author do not have enough permissions to interact with the role
  • - *
  • the author does not have the required role for this interaction
  • *
  • the bot does not have the MANAGE_ROLES permission
  • *
  • the given reason is too long
  • * @@ -206,8 +205,7 @@ private static void handleAbsentTarget(@NotNull String actionVerb, "squid:S107"}) static boolean handleRoleChangeChecks(@Nullable Role role, @NotNull String actionVerb, @Nullable Member target, @NotNull Member bot, @NotNull Member author, - @NotNull Guild guild, @NotNull Predicate hasRequiredRole, - @NotNull CharSequence reason, @NotNull IReplyCallback event) { + @NotNull Guild guild, @NotNull CharSequence reason, @NotNull IReplyCallback event) { if (role == null) { event .reply("Can not %s the user, unable to find the corresponding role on this server" @@ -231,9 +229,6 @@ static boolean handleRoleChangeChecks(@Nullable Role role, @NotNull String actio if (!handleCanInteractWithRole(bot, author, role, event)) { return false; } - if (!handleHasAuthorRole(actionVerb, hasRequiredRole, author, event)) { - return false; - } if (!handleHasBotPermissions(actionVerb, Permission.MANAGE_ROLES, bot, guild, event)) { return false; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java index 7b4f244ef1..74d116688c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java @@ -41,7 +41,6 @@ public final class MuteCommand extends SlashCommandAdapter { @SuppressWarnings("StaticCollection") private static final List DURATIONS = List.of("10 minutes", "30 minutes", "1 hour", "3 hours", "1 day", "3 days", "7 days", ModerationUtils.PERMANENT_DURATION); - private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; private final Config config; @@ -64,7 +63,6 @@ public MuteCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be muted", true); this.config = config; - hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -141,7 +139,7 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, @NotNull IReplyCallback event) { if (!ModerationUtils.handleRoleChangeChecks( ModerationUtils.getMutedRole(guild, config).orElse(null), ACTION_VERB, target, bot, - author, guild, hasRequiredRole, reason, event)) { + author, guild, reason, event)) { return false; } if (Objects.requireNonNull(target) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java index cc3453dff5..8a2a3a96aa 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java @@ -34,7 +34,6 @@ public final class QuarantineCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final String COMMAND_NAME = "quarantine"; private static final String ACTION_VERB = "quarantine"; - private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; private final Config config; @@ -55,7 +54,6 @@ public QuarantineCommand(@NotNull ModerationActionsStore actionsStore, @NotNull true); this.config = config; - hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -123,7 +121,7 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, @NotNull IReplyCallback event) { if (!ModerationUtils.handleRoleChangeChecks( ModerationUtils.getQuarantinedRole(guild, config).orElse(null), ACTION_VERB, target, - bot, author, guild, hasRequiredRole, reason, event)) { + bot, author, guild, reason, event)) { return false; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java index bd2e79bdd7..8a4ac19c22 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java @@ -33,7 +33,6 @@ public final class UnmuteCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final String COMMAND_NAME = "unmute"; private static final String ACTION_VERB = "unmute"; - private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; private final Config config; @@ -52,7 +51,6 @@ public UnmuteCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Conf .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be unmuted", true); this.config = config; - hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -115,7 +113,7 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, @NotNull IReplyCallback event) { if (!ModerationUtils.handleRoleChangeChecks( ModerationUtils.getMutedRole(guild, config).orElse(null), ACTION_VERB, target, bot, - author, guild, hasRequiredRole, reason, event)) { + author, guild, reason, event)) { return false; } if (Objects.requireNonNull(target) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java index 9e33c4841f..eb4db79dc2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java @@ -34,7 +34,6 @@ public final class UnquarantineCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final String COMMAND_NAME = "unquarantine"; private static final String ACTION_VERB = "unquarantine"; - private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; private final Config config; @@ -57,7 +56,6 @@ public UnquarantineCommand(@NotNull ModerationActionsStore actionsStore, true); this.config = config; - hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -122,7 +120,7 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, @NotNull IReplyCallback event) { if (!ModerationUtils.handleRoleChangeChecks( ModerationUtils.getQuarantinedRole(guild, config).orElse(null), ACTION_VERB, target, - bot, author, guild, hasRequiredRole, reason, event)) { + bot, author, guild, reason, event)) { return false; } From 1ef949c78bd43db9e60e836a60049b82be21689e Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 00:06:03 +0530 Subject: [PATCH 08/21] remove unused imports --- .../togetherjava/tjbot/commands/moderation/AuditCommand.java | 3 +-- .../togetherjava/tjbot/commands/moderation/MuteCommand.java | 2 -- .../tjbot/commands/moderation/QuarantineCommand.java | 2 -- .../togetherjava/tjbot/commands/moderation/UnmuteCommand.java | 2 -- .../tjbot/commands/moderation/UnquarantineCommand.java | 2 -- 5 files changed, 1 insertion(+), 10 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 5af07e0f05..3e69396575 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -140,8 +140,7 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, if (target == null) { return false; } - return ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, - target, event); + return ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, target, event); } private @NotNull List> groupActionsByPages( diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java index 74d116688c..4c9e9622b7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java @@ -21,8 +21,6 @@ import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.function.Predicate; -import java.util.regex.Pattern; /** * This command can mute users. Muting can also be paired with a reason. The command will also try diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java index 8a2a3a96aa..3e845c643a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java @@ -17,8 +17,6 @@ import org.togetherjava.tjbot.config.Config; import java.util.Objects; -import java.util.function.Predicate; -import java.util.regex.Pattern; /** * This command can quarantine users. Quarantining can also be paired with a reason. The command diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java index 8a4ac19c22..d2b3041660 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java @@ -17,8 +17,6 @@ import org.togetherjava.tjbot.config.Config; import java.util.Objects; -import java.util.function.Predicate; -import java.util.regex.Pattern; /** * This command can unmute muted users. Unmuting can also be paired with a reason. The command will diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java index eb4db79dc2..c04b173f96 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java @@ -17,8 +17,6 @@ import org.togetherjava.tjbot.config.Config; import java.util.Objects; -import java.util.function.Predicate; -import java.util.regex.Pattern; /** * This command can unquarantine quarantined users. Unquarantining can also be paired with a reason. From ea68206d568c0c0bfbaf769b6cf0283fa716fcdc Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 00:13:11 +0530 Subject: [PATCH 09/21] removed unnecessary checks from BanCommand --- .../org/togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/moderation/BanCommand.java | 12 +----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 0b107600ae..f5cd51b25d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -95,7 +95,7 @@ public enum Features { features.add(new VcActivityCommand()); features.add(new WarnCommand(actionsStore, config)); features.add(new KickCommand(actionsStore, config)); - features.add(new BanCommand(actionsStore, config)); + features.add(new BanCommand(actionsStore)); features.add(new UnbanCommand(actionsStore, config)); features.add(new AuditCommand(actionsStore)); features.add(new MuteCommand(actionsStore, config)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 109b1275bb..b59450c4ad 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -20,14 +20,11 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.config.Config; import java.time.Instant; import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.function.Predicate; -import java.util.regex.Pattern; /** * This command can ban users and optionally remove their messages from the past days. Banning can @@ -48,16 +45,14 @@ public final class BanCommand extends SlashCommandAdapter { @SuppressWarnings("StaticCollection") private static final List DURATIONS = List.of(ModerationUtils.PERMANENT_DURATION, "1 hour", "3 hours", "1 day", "2 days", "3 days", "7 days", "30 days"); - private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; /** * Constructs an instance. * * @param actionsStore used to store actions issued by this command - * @param config the config to use for this */ - public BanCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config config) { + public BanCommand(@NotNull ModerationActionsStore actionsStore) { super(COMMAND_NAME, "Bans the given user from the server", SlashCommandVisibility.GUILD); OptionData durationData = new OptionData(OptionType.STRING, DURATION_OPTION, @@ -71,8 +66,6 @@ public BanCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config "the amount of days of the message history to delete, none means no messages are deleted.", true).addChoice("none", 0).addChoice("recent", 1).addChoice("all", 7)); - hasRequiredRole = - Pattern.compile(config.getHeavyModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -182,9 +175,6 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, target, event)) { return false; } - if (!ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event)) { - return false; - } if (!ModerationUtils.handleHasBotPermissions(ACTION_VERB, Permission.BAN_MEMBERS, bot, guild, event)) { return false; From e9197c1a54a4a8be42ef4df19562535fdf91ed88 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 00:14:21 +0530 Subject: [PATCH 10/21] removed unnecessary checks from KickCommand --- .../org/togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/moderation/KickCommand.java | 12 +----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index f5cd51b25d..3427d73350 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -94,7 +94,7 @@ public enum Features { features.add(new TagsCommand(tagSystem)); features.add(new VcActivityCommand()); features.add(new WarnCommand(actionsStore, config)); - features.add(new KickCommand(actionsStore, config)); + features.add(new KickCommand(actionsStore)); features.add(new BanCommand(actionsStore)); features.add(new UnbanCommand(actionsStore, config)); features.add(new AuditCommand(actionsStore)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 17bd9cf460..8a079587f9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -18,12 +18,8 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.config.Config; import java.util.Objects; -import java.util.function.Predicate; -import java.util.regex.Pattern; - /** * This command can kicks users. Kicking can also be paired with a kick reason. The command will @@ -38,22 +34,19 @@ public final class KickCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final String COMMAND_NAME = "kick"; private static final String ACTION_VERB = "kick"; - private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; /** * Constructs an instance. * * @param actionsStore used to store actions issued by this command - * @param config the config to use for this */ - public KickCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config config) { + public KickCommand(@NotNull ModerationActionsStore actionsStore) { super(COMMAND_NAME, "Kicks the given user from the server", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, TARGET_OPTION, "The user who you want to kick", true) .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be kicked", true); - hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -123,9 +116,6 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, if (!ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, target, event)) { return false; } - if (!ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event)) { - return false; - } if (!ModerationUtils.handleHasBotPermissions(ACTION_VERB, Permission.KICK_MEMBERS, bot, guild, event)) { return false; From 68b67ae4dbac0e1c08a6fd8bde2d714544f1a420 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 00:15:23 +0530 Subject: [PATCH 11/21] removed unnecessary checks from NoteCommand --- .../org/togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/moderation/NoteCommand.java | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 3427d73350..fe1d583e8a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -102,7 +102,7 @@ public enum Features { features.add(new UnmuteCommand(actionsStore, config)); features.add(new TopHelpersCommand(database, config)); features.add(new RoleSelectCommand()); - features.add(new NoteCommand(actionsStore, config)); + features.add(new NoteCommand(actionsStore)); features.add(new RemindCommand(database)); features.add(new QuarantineCommand(actionsStore, config)); features.add(new UnquarantineCommand(actionsStore, config)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java index 285e4973ca..54bcc11426 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java @@ -11,12 +11,8 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.config.Config; import java.util.Objects; -import java.util.function.Predicate; -import java.util.regex.Pattern; - /** * This command allows users to write notes about others. Notes are persisted and can be retrieved @@ -32,15 +28,13 @@ public final class NoteCommand extends SlashCommandAdapter { private static final String CONTENT_OPTION = "content"; private static final String ACTION_VERB = "write a note about"; private final ModerationActionsStore actionsStore; - private final Predicate hasRequiredRole; /** * Creates a new instance. * * @param actionsStore used to store actions issued by this command - * @param config the config to use for this */ - public NoteCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config config) { + public NoteCommand(@NotNull ModerationActionsStore actionsStore) { super("note", "Writes a note about the given user", SlashCommandVisibility.GUILD); getData() @@ -49,7 +43,6 @@ public NoteCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config .addOption(OptionType.STRING, CONTENT_OPTION, "The content of the note you want to write", true); - hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -76,10 +69,6 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return false; } - if (!ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event)) { - return false; - } - return ModerationUtils.handleReason(content, event); } From b2d69eeb6e07cd7a602c50b15ba43da6e1705ad7 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 00:16:34 +0530 Subject: [PATCH 12/21] removed unnecessary checks from UnbanCommand --- .../org/togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/moderation/UnbanCommand.java | 12 +----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index fe1d583e8a..0a6fab9c5d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -96,7 +96,7 @@ public enum Features { features.add(new WarnCommand(actionsStore, config)); features.add(new KickCommand(actionsStore)); features.add(new BanCommand(actionsStore)); - features.add(new UnbanCommand(actionsStore, config)); + features.add(new UnbanCommand(actionsStore)); features.add(new AuditCommand(actionsStore)); features.add(new MuteCommand(actionsStore, config)); features.add(new UnmuteCommand(actionsStore, config)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index c26670f831..654ac014e9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -12,11 +12,8 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.config.Config; import java.util.Objects; -import java.util.function.Predicate; -import java.util.regex.Pattern; /** * Unbans a given user. Unbanning can also be paired with a reason. The command fails if the user is @@ -28,16 +25,14 @@ public final class UnbanCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final String COMMAND_NAME = "unban"; private static final String ACTION_VERB = "unban"; - private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; /** * Constructs an instance. * * @param actionsStore used to store actions issued by this command - * @param config the config to use for this */ - public UnbanCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config config) { + public UnbanCommand(@NotNull ModerationActionsStore actionsStore) { super(COMMAND_NAME, "Unbans the given user from the server", SlashCommandVisibility.GUILD); getData() @@ -45,8 +40,6 @@ public UnbanCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi true) .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be unbanned", true); - hasRequiredRole = - Pattern.compile(config.getHeavyModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -92,9 +85,6 @@ private static void handleFailure(@NotNull Throwable unbanFailure, @NotNull User @SuppressWarnings({"BooleanMethodNameMustStartWithQuestion"}) private boolean handleChecks(@NotNull IPermissionHolder bot, @NotNull Member author, @NotNull CharSequence reason, @NotNull Guild guild, @NotNull IReplyCallback event) { - if (!ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event)) { - return false; - } if (!ModerationUtils.handleHasBotPermissions(ACTION_VERB, Permission.BAN_MEMBERS, bot, guild, event)) { return false; From 01743fe807d832b3a5b09990ab831c5347f841fa Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 00:17:33 +0530 Subject: [PATCH 13/21] removed unnecessary checks from WarnCommand --- .../org/togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/moderation/WarnCommand.java | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 0a6fab9c5d..3a7e3f49ce 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -93,7 +93,7 @@ public enum Features { features.add(new TagManageCommand(tagSystem, config, modAuditLogWriter)); features.add(new TagsCommand(tagSystem)); features.add(new VcActivityCommand()); - features.add(new WarnCommand(actionsStore, config)); + features.add(new WarnCommand(actionsStore)); features.add(new KickCommand(actionsStore)); features.add(new BanCommand(actionsStore)); features.add(new UnbanCommand(actionsStore)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java index 32c84a400d..a29b1ea51d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java @@ -13,12 +13,8 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.config.Config; import java.util.Objects; -import java.util.function.Predicate; -import java.util.regex.Pattern; - /** * This command can warn users. The command will also try to DM the user to inform them about the @@ -33,22 +29,18 @@ public final class WarnCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final String ACTION_VERB = "warn"; private final ModerationActionsStore actionsStore; - private final Predicate hasRequiredRole; /** * Creates a new instance. * * @param actionsStore used to store actions issued by this command - * @param config the config to use for this */ - public WarnCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config config) { + public WarnCommand(@NotNull ModerationActionsStore actionsStore) { super("warn", "Warns the given user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, USER_OPTION, "The user who you want to warn", true) .addOption(OptionType.STRING, REASON_OPTION, "Why you want to warn the user", true); - hasRequiredRole = - Pattern.compile(config.getHeavyModerationRolePattern()).asMatchPredicate(); this.actionsStore = Objects.requireNonNull(actionsStore); } @@ -123,9 +115,6 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, target, event)) { return false; } - if (!ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event)) { - return false; - } return ModerationUtils.handleReason(reason, event); } } From a95b6667fe0601364fb91ef4a1467b80308f6f6a Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 00:18:51 +0530 Subject: [PATCH 14/21] removed unused method from MoerationUtils --- .../commands/moderation/ModerationUtils.java | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java index 263642f3ed..3ea5799eb6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java @@ -196,7 +196,6 @@ private static void handleAbsentTarget(@NotNull String actionVerb, * @param bot the bot executing this interaction * @param author the author attempting to interact with the target * @param guild the guild this interaction is executed on - * @param hasRequiredRole a predicate used to identify required roles by their name * @param reason the reason for this interaction * @param event the event used to respond to the user * @return Whether the bot and the author have enough permission @@ -262,33 +261,6 @@ static boolean handleHasAuthorPermissions(@NotNull String actionVerb, return true; } - /** - * Checks whether the given bot has enough permission to execute the given action. For example - * whether it has enough permissions to ban users. - *

    - * If not, it will handle the situation and respond to the user. - * - * @param actionVerb the interaction as verb, for example {@code "ban"} or {@code "kick"} - * @param hasRequiredRole a predicate used to identify required roles by their name - * @param author the author attempting to interact with the target - * @param event the event used to respond to the user - * @return Whether the bot has the required permission - */ - @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") - static boolean handleHasAuthorRole(@NotNull String actionVerb, - @NotNull Predicate hasRequiredRole, @NotNull Member author, - @NotNull IReplyCallback event) { - if (author.getRoles().stream().map(Role::getName).anyMatch(hasRequiredRole)) { - return true; - } - event - .reply("You can not %s users in this guild since you do not have the required role." - .formatted(actionVerb)) - .setEphemeral(true) - .queue(); - return false; - } - /** * Creates a message to be displayed as response to a moderation action. *

    From aa2e6c739332168d693a56bb7dabdbe5d5945c5a Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 03:33:24 +0530 Subject: [PATCH 15/21] removed unnecessary checks from TagManageCommand --- .../togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/tags/TagManageCommand.java | 22 +------------------ 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 3a7e3f49ce..2e552a0d3b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -90,7 +90,7 @@ public enum Features { features.add(new PingCommand()); features.add(new TeXCommand()); features.add(new TagCommand(tagSystem)); - features.add(new TagManageCommand(tagSystem, config, modAuditLogWriter)); + features.add(new TagManageCommand(tagSystem, modAuditLogWriter)); features.add(new TagsCommand(tagSystem)); features.add(new VcActivityCommand()); features.add(new WarnCommand(actionsStore)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 3eb996a7cb..4aa8257480 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -2,8 +2,6 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.exceptions.ErrorResponseException; @@ -18,7 +16,6 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import java.time.temporal.TemporalAccessor; @@ -30,8 +27,6 @@ import java.util.OptionalLong; import java.util.function.BiConsumer; import java.util.function.Consumer; -import java.util.function.Predicate; -import java.util.regex.Pattern; /** * Implements the {@code /tag-manage} command which allows management of tags, such as creating, @@ -67,7 +62,6 @@ public final class TagManageCommand extends SlashCommandAdapter { private static final String UNABLE_TO_GET_CONTENT_MESSAGE = "Was unable to retrieve content"; private final TagSystem tagSystem; - private final Predicate hasRequiredRole; private final ModAuditLogWriter modAuditLogWriter; @@ -75,15 +69,12 @@ public final class TagManageCommand extends SlashCommandAdapter { * Creates a new instance, using the given tag system as base. * * @param tagSystem the system providing the actual tag data - * @param config the config to use for this * @param modAuditLogWriter to log tag changes for audition */ - public TagManageCommand(@NotNull TagSystem tagSystem, @NotNull Config config, - @NotNull ModAuditLogWriter modAuditLogWriter) { + public TagManageCommand(@NotNull TagSystem tagSystem,@NotNull ModAuditLogWriter modAuditLogWriter) { super("tag-manage", "Provides commands to manage all tags", SlashCommandVisibility.GUILD); this.tagSystem = tagSystem; - hasRequiredRole = Pattern.compile(config.getTagManageRolePattern()).asMatchPredicate(); this.modAuditLogWriter = modAuditLogWriter; @@ -152,13 +143,6 @@ private static OptionalLong parseMessageIdAndHandle(@NotNull String messageId, @Override public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { - if (!hasTagManageRole(Objects.requireNonNull(event.getMember()))) { - event.reply("Tags can only be managed by users with a corresponding role.") - .setEphemeral(true) - .queue(); - return; - } - switch (Subcommand.fromName(event.getSubcommandName())) { case RAW -> rawTag(event); case CREATE -> createTag(event); @@ -399,10 +383,6 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, triggeredAt, guild, attachments.toArray(ModAuditLogWriter.Attachment[]::new)); } - private boolean hasTagManageRole(@NotNull Member member) { - return member.getRoles().stream().map(Role::getName).anyMatch(hasRequiredRole); - } - private enum TagStatus { EXISTS, NOT_EXISTS From 5eb4235dccc33f4b174fb37577b2af0d0bd9b233 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 03:38:05 +0530 Subject: [PATCH 16/21] removed unnecessary checks from TopHelpersCommand --- .../togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/tags/TagManageCommand.java | 3 ++- .../commands/tophelper/TopHelpersCommand.java | 24 +------------------ 3 files changed, 4 insertions(+), 25 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 2e552a0d3b..a58580fbe7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -100,7 +100,7 @@ public enum Features { features.add(new AuditCommand(actionsStore)); features.add(new MuteCommand(actionsStore, config)); features.add(new UnmuteCommand(actionsStore, config)); - features.add(new TopHelpersCommand(database, config)); + features.add(new TopHelpersCommand(database)); features.add(new RoleSelectCommand()); features.add(new NoteCommand(actionsStore)); features.add(new RemindCommand(database)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 4aa8257480..3cebad8f10 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -71,7 +71,8 @@ public final class TagManageCommand extends SlashCommandAdapter { * @param tagSystem the system providing the actual tag data * @param modAuditLogWriter to log tag changes for audition */ - public TagManageCommand(@NotNull TagSystem tagSystem,@NotNull ModAuditLogWriter modAuditLogWriter) { + public TagManageCommand(@NotNull TagSystem tagSystem, + @NotNull ModAuditLogWriter modAuditLogWriter) { super("tag-manage", "Provides commands to manage all tags", SlashCommandVisibility.GUILD); this.tagSystem = tagSystem; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java index 479b1dae19..f7f6a44e4f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java @@ -5,10 +5,8 @@ import com.github.freva.asciitable.ColumnData; import com.github.freva.asciitable.HorizontalAlign; import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback; -import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.OptionData; @@ -20,7 +18,6 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; import java.math.BigDecimal; @@ -29,8 +26,6 @@ import java.util.*; import java.util.function.Function; import java.util.function.IntFunction; -import java.util.function.Predicate; -import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -49,15 +44,13 @@ public final class TopHelpersCommand extends SlashCommandAdapter { private static final int TOP_HELPER_LIMIT = 20; private final Database database; - private final Predicate hasRequiredRole; /** * Creates a new instance. * * @param database the database containing the message records of top helpers - * @param config the config to use for this */ - public TopHelpersCommand(@NotNull Database database, @NotNull Config config) { + public TopHelpersCommand(@NotNull Database database) { super(COMMAND_NAME, "Lists top helpers for the last month, or a given month", SlashCommandVisibility.GUILD); @@ -68,15 +61,11 @@ public TopHelpersCommand(@NotNull Database database, @NotNull Config config) { month.getDisplayName(TextStyle.FULL_STANDALONE, Locale.US), month.name())); getData().addOptions(monthData); - hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); this.database = database; } @Override public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { - if (!handleHasAuthorRole(event.getMember(), event)) { - return; - } OptionMapping atMonthData = event.getOption(MONTH_OPTION); TimeRange timeRange = computeTimeRange(computeMonth(atMonthData)); @@ -99,17 +88,6 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { .onSuccess(members -> handleTopHelpers(topHelpers, members, timeRange, event)); } - @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") - private boolean handleHasAuthorRole(@NotNull Member author, @NotNull IReplyCallback event) { - if (author.getRoles().stream().map(Role::getName).anyMatch(hasRequiredRole)) { - return true; - } - event.reply("You can not compute the top-helpers since you do not have the required role.") - .setEphemeral(true) - .queue(); - return false; - } - private static @NotNull Month computeMonth(@Nullable OptionMapping atMonthData) { if (atMonthData != null) { return Month.valueOf(atMonthData.getAsString()); From 35324f857e7336ad37d62723ed3a8b0054fbf119 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 03:41:11 +0530 Subject: [PATCH 17/21] removed unused entry from comfig template --- application/config.json.template | 1 - 1 file changed, 1 deletion(-) diff --git a/application/config.json.template b/application/config.json.template index 8238dba370..4aa3d02389 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -24,7 +24,6 @@ }, "wolframAlphaAppId": "79J52T-6239TVXHR7", "helpSystem": { - "stagingChannelPattern": "ask_here", "overviewChannelPattern": "active_questions", "categories": [ "Java", From f4e46ff4951b42ada3df89053db47626a6451cd0 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 03:45:07 +0530 Subject: [PATCH 18/21] fixed tag manage command test --- .../togetherjava/tjbot/commands/tags/TagManageCommandTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java index 3bec6f8ac3..575f34227a 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java @@ -52,7 +52,7 @@ void setUp() { Database database = Database.createMemoryDatabase(Tags.TAGS); system = spy(new TagSystem(database)); jdaTester = new JdaTester(); - command = new TagManageCommand(system, config, modAuditLogWriter); + command = new TagManageCommand(system, modAuditLogWriter); moderator = jdaTester.createMemberSpy(1); Role moderatorRole = mock(Role.class); From 5bb4d72fb1da3c50810b5d65d9c6f1b7ed28addf Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 17 Aug 2022 03:53:17 +0530 Subject: [PATCH 19/21] fixed TagManagedCommandTest --- .../tjbot/commands/tags/TagManageCommandTest.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java index 575f34227a..9f3ddf9d13 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java @@ -149,20 +149,6 @@ private void failOnRetrieveMessage(@NotNull String messageId, @NotNull Throwable .retrieveMessageById(messageId); } - @Test - @DisplayName("Users without the required role can not use '/tag-manage'") - void commandCanNotBeUsedWithoutRoles() { - // GIVEN a regular user without roles - Member regularUser = jdaTester.createMemberSpy(1); - - // WHEN the regular user triggers any '/tag-manage' command - SlashCommandInteractionEvent event = triggerRawCommandWithUser("foo", regularUser); - - // THEN the command can not be used since the user lacks roles - verify(event).reply("Tags can only be managed by users with a corresponding role."); - verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); - } - @Test @DisplayName("'/tag-manage raw' can not be used on unknown tags") void rawTagCanNotFindUnknownTag() { From 7c2db702c7a07e1e66f42d644f849732eae0baea Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 24 Aug 2022 15:56:39 +0530 Subject: [PATCH 20/21] restore stagingChannelPattern --- application/config.json.template | 1 + .../tjbot/commands/help/HelpSystemHelper.java | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/application/config.json.template b/application/config.json.template index 4aa3d02389..8238dba370 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -24,6 +24,7 @@ }, "wolframAlphaAppId": "79J52T-6239TVXHR7", "helpSystem": { + "stagingChannelPattern": "ask_here", "overviewChannelPattern": "active_questions", "categories": [ "Java", diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index 46bd5a6533..0cf533a1b5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -39,9 +39,10 @@ public final class HelpSystemHelper { private final Predicate isOverviewChannelName; private final String overviewChannelPattern; + private final Predicate isStagingChannelName; + private final String stagingChannelPattern; private final String categoryRoleSuffix; - /** * Creates a new instance. * @@ -53,6 +54,9 @@ public HelpSystemHelper(@NotNull Config config) { overviewChannelPattern = helpConfig.getOverviewChannelPattern(); isOverviewChannelName = Pattern.compile(overviewChannelPattern).asMatchPredicate(); + stagingChannelPattern = helpConfig.getStagingChannelPattern(); + isStagingChannelName = Pattern.compile(stagingChannelPattern).asMatchPredicate(); + categoryRoleSuffix = helpConfig.getCategoryRoleSuffix(); } @@ -143,6 +147,15 @@ String getOverviewChannelPattern() { return overviewChannelPattern; } + boolean isStagingChannelName(@NotNull String channelName) { + return isStagingChannelName.test(channelName); + } + + @NotNull + String getStagingChannelPattern() { + return stagingChannelPattern; + } + static boolean isTitleValid(@NotNull CharSequence title) { String titleCompact = TITLE_COMPACT_REMOVAL_PATTERN.matcher(title).replaceAll(""); From abc5dc6ea9cb25bf0e0ac0af5b3f688607e5b3b1 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 24 Aug 2022 17:02:24 +0530 Subject: [PATCH 21/21] fixed the logic of AuditCommand checks --- .../togetherjava/tjbot/commands/moderation/AuditCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 3e69396575..0ff92a1a0b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -138,7 +138,7 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, @Nullable Member target, @NotNull IReplyCallback event) { // Member doesn't exist if attempting to audit a user who is not part of the guild. if (target == null) { - return false; + return true; } return ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, target, event); }