Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions application/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<String> isHelpForumName;
private final Predicate<String> isExcludedRolePattern;

/**
* 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
*/
Expand All @@ -38,11 +40,15 @@ public CodeMessageAutoDetection(Config config, CodeMessageHandler codeMessageHan

isHelpForumName =
Pattern.compile(config.getHelpSystem().getHelpForumPattern()).asMatchPredicate();

isExcludedRolePattern =
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;
}

Expand All @@ -63,6 +69,10 @@ public void onMessageReceived(MessageReceivedEvent event) {
codeMessageHandler.addAndHandleCodeMessage(originalMessage, true);
}

private boolean isSentByExcludedRole(List<Role> roles) {
return roles.stream().map(Role::getName).anyMatch(isExcludedRolePattern);
}

private boolean isHelpThread(MessageReceivedEvent event) {
if (event.getChannelType() != ChannelType.GUILD_PUBLIC_THREAD) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down