Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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 .+",
"ignoreMessageAutoDetectionRolePattern": "Top Helpers .+",
"suggestions": {
"channelPattern": "tj_suggestions",
"upVoteEmoteName": "peepo_yes",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.togetherjava.tjbot.commands.code;

import net.dv8tion.jda.api.entities.Member;
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 @@ -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 String ignoredRolePattern;

/**
* 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,14 @@ public CodeMessageAutoDetection(Config config, CodeMessageHandler codeMessageHan

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

ignoredRolePattern = config.getIgnoreMessageAutoDetectionRolePattern();
}

@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.isWebhookMessage() || event.getAuthor().isBot() || !isHelpThread(event)) {
if (event.isWebhookMessage() || event.getAuthor().isBot() || !isHelpThread(event)
|| isSentByIgnoredMember(event.getMember())) {
return;
}

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

private boolean isSentByIgnoredMember(Member member) {
return member.getRoles()
.stream()
.map(Role::getName)
.anyMatch(role -> role.matches(ignoredRolePattern));
}

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 ignoreMessageAutoDetectionRolePattern;
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 = "ignoreMessageAutoDetectionRolePattern",
required = true) String ignoreMessageAutoDetectionRolePattern,
@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.ignoreMessageAutoDetectionRolePattern =
Objects.requireNonNull(ignoreMessageAutoDetectionRolePattern);
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 getIgnoreMessageAutoDetectionRolePattern() {
return ignoreMessageAutoDetectionRolePattern;
}

/**
* Gets the config for the suggestion system.
*
Expand Down