diff --git a/application/config.json.template b/application/config.json.template index 26980743ee..d85f39e581 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -10,6 +10,7 @@ "heavyModerationRolePattern": "Moderator", "softModerationRolePattern": "Moderator|Community Ambassador", "tagManageRolePattern": "Moderator|Community Ambassador|Top Helpers .+", + "excludeCodeAutoDetectionRolePattern": "Top Helpers .+|Moderator|Community Ambassador|Expert", "suggestions": { "channelPattern": "tj_suggestions", "upVoteEmoteName": "peepo_yes", diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/code/CodeMessageAutoDetection.java b/application/src/main/java/org/togetherjava/tjbot/commands/code/CodeMessageAutoDetection.java index 30d7aea1b5..1a8cd42173 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/code/CodeMessageAutoDetection.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/code/CodeMessageAutoDetection.java @@ -1,6 +1,7 @@ package org.togetherjava.tjbot.commands.code; import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; @@ -10,6 +11,7 @@ import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; +import java.util.List; import java.util.Optional; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -22,12 +24,12 @@ public final class CodeMessageAutoDetection extends MessageReceiverAdapter { private static final long MINIMUM_LINES_OF_CODE = 3; private final CodeMessageHandler codeMessageHandler; - private final Predicate isHelpForumName; + private final Predicate isExcludedRole; /** * Creates a new instance. - * + * * @param config to figure out whether a message is from a help thread * @param codeMessageHandler to register detected code messages at for further handling */ @@ -38,11 +40,15 @@ public CodeMessageAutoDetection(Config config, CodeMessageHandler codeMessageHan isHelpForumName = Pattern.compile(config.getHelpSystem().getHelpForumPattern()).asMatchPredicate(); + + isExcludedRole = + Pattern.compile(config.getExcludeCodeAutoDetectionRolePattern()).asMatchPredicate(); } @Override public void onMessageReceived(MessageReceivedEvent event) { - if (event.isWebhookMessage() || event.getAuthor().isBot() || !isHelpThread(event)) { + if (event.isWebhookMessage() || event.getAuthor().isBot() || !isHelpThread(event) + || isSentByExcludedRole(event.getMember().getRoles())) { return; } @@ -63,6 +69,10 @@ public void onMessageReceived(MessageReceivedEvent event) { codeMessageHandler.addAndHandleCodeMessage(originalMessage, true); } + private boolean isSentByExcludedRole(List roles) { + return roles.stream().map(Role::getName).anyMatch(isExcludedRole); + } + private boolean isHelpThread(MessageReceivedEvent event) { if (event.getChannelType() != ChannelType.GUILD_PUBLIC_THREAD) { return false; diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 7c078a97f7..830d4cadde 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -26,6 +26,7 @@ public final class Config { private final String heavyModerationRolePattern; private final String softModerationRolePattern; private final String tagManageRolePattern; + private final String excludeCodeAutoDetectionRolePattern; private final SuggestionsConfig suggestions; private final String quarantinedRolePattern; private final ScamBlockerConfig scamBlocker; @@ -54,6 +55,8 @@ private Config(@JsonProperty(value = "token", required = true) String token, required = true) String softModerationRolePattern, @JsonProperty(value = "tagManageRolePattern", required = true) String tagManageRolePattern, + @JsonProperty(value = "excludeCodeAutoDetectionRolePattern", + required = true) String excludeCodeAutoDetectionRolePattern, @JsonProperty(value = "suggestions", required = true) SuggestionsConfig suggestions, @JsonProperty(value = "quarantinedRolePattern", required = true) String quarantinedRolePattern, @@ -79,6 +82,8 @@ private Config(@JsonProperty(value = "token", required = true) String token, this.heavyModerationRolePattern = Objects.requireNonNull(heavyModerationRolePattern); this.softModerationRolePattern = Objects.requireNonNull(softModerationRolePattern); this.tagManageRolePattern = Objects.requireNonNull(tagManageRolePattern); + this.excludeCodeAutoDetectionRolePattern = + Objects.requireNonNull(excludeCodeAutoDetectionRolePattern); this.suggestions = Objects.requireNonNull(suggestions); this.quarantinedRolePattern = Objects.requireNonNull(quarantinedRolePattern); this.scamBlocker = Objects.requireNonNull(scamBlocker); @@ -209,6 +214,16 @@ public String getTagManageRolePattern() { return tagManageRolePattern; } + /** + * Gets the REGEX pattern used to identify roles that will be ignored for code actions + * auto-detection + * + * @return the REGEX pattern + */ + public String getExcludeCodeAutoDetectionRolePattern() { + return excludeCodeAutoDetectionRolePattern; + } + /** * Gets the config for the suggestion system. *