Skip to content

Commit 1b4c13a

Browse files
authored
Added bot-trap-channel scam detection (#1214)
1 parent d357b6e commit 1b4c13a

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

application/config.json.template

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"scamBlocker": {
2222
"mode": "AUTO_DELETE_BUT_APPROVE_QUARANTINE",
2323
"reportChannelPattern": "commands",
24+
"botTrapChannelPattern": "bot-trap",
2425
"suspiciousKeywords": [
2526
"nitro",
2627
"boob",

application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
public final class ScamBlockerConfig {
1818
private final Mode mode;
1919
private final String reportChannelPattern;
20+
private final String botTrapChannelPattern;
2021
private final Set<String> suspiciousKeywords;
2122
private final Set<String> hostWhitelist;
2223
private final Set<String> hostBlacklist;
@@ -27,6 +28,8 @@ public final class ScamBlockerConfig {
2728
private ScamBlockerConfig(@JsonProperty(value = "mode", required = true) Mode mode,
2829
@JsonProperty(value = "reportChannelPattern",
2930
required = true) String reportChannelPattern,
31+
@JsonProperty(value = "botTrapChannelPattern",
32+
required = true) String botTrapChannelPattern,
3033
@JsonProperty(value = "suspiciousKeywords",
3134
required = true) Set<String> suspiciousKeywords,
3235
@JsonProperty(value = "hostWhitelist", required = true) Set<String> hostWhitelist,
@@ -37,6 +40,7 @@ private ScamBlockerConfig(@JsonProperty(value = "mode", required = true) Mode mo
3740
required = true) int isHostSimilarToKeywordDistanceThreshold) {
3841
this.mode = Objects.requireNonNull(mode);
3942
this.reportChannelPattern = Objects.requireNonNull(reportChannelPattern);
43+
this.botTrapChannelPattern = Objects.requireNonNull(botTrapChannelPattern);
4044
this.suspiciousKeywords = new HashSet<>(Objects.requireNonNull(suspiciousKeywords));
4145
this.hostWhitelist = new HashSet<>(Objects.requireNonNull(hostWhitelist));
4246
this.hostBlacklist = new HashSet<>(Objects.requireNonNull(hostBlacklist));
@@ -63,6 +67,16 @@ public String getReportChannelPattern() {
6367
return reportChannelPattern;
6468
}
6569

70+
/**
71+
* Gets the REGEX pattern used to identify the channel that is used to as bot-trap. Sending
72+
* messages in this channel identifies the author as bot.
73+
*
74+
* @return the channel name pattern
75+
*/
76+
public String getBotTrapChannelPattern() {
77+
return botTrapChannelPattern;
78+
}
79+
6680
/**
6781
* Gets the set of keywords that are considered suspicious if they appear in a message.
6882
*

application/src/main/java/org/togetherjava/tjbot/features/moderation/scam/ScamBlocker.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public final class ScamBlocker extends MessageReceiverAdapter implements UserInt
6464

6565
private final ScamBlockerConfig.Mode mode;
6666
private final String reportChannelPattern;
67+
private final String botTrapChannelPattern;
6768
private final Predicate<TextChannel> isReportChannel;
69+
private final Predicate<TextChannel> isBotTrapChannel;
6870
private final ScamDetector scamDetector;
6971
private final Config config;
7072
private final ModerationActionsStore actionsStore;
@@ -92,6 +94,12 @@ public ScamBlocker(ModerationActionsStore actionsStore, ScamHistoryStore scamHis
9294
Predicate<String> isReportChannelName =
9395
Pattern.compile(reportChannelPattern).asMatchPredicate();
9496
isReportChannel = channel -> isReportChannelName.test(channel.getName());
97+
98+
botTrapChannelPattern = config.getScamBlocker().getBotTrapChannelPattern();
99+
Predicate<String> isBotTrapChannelName =
100+
Pattern.compile(botTrapChannelPattern).asMatchPredicate();
101+
isBotTrapChannel = channel -> isBotTrapChannelName.test(channel.getName());
102+
95103
hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate();
96104

97105
componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName());
@@ -122,9 +130,15 @@ public void onMessageReceived(MessageReceivedEvent event) {
122130
return;
123131
}
124132

133+
boolean isSafe = !isBotTrapChannel.test(event.getChannel().asTextChannel());
134+
125135
Message message = event.getMessage();
126136
String content = message.getContentDisplay();
127-
if (!scamDetector.isScam(content)) {
137+
if (isSafe && scamDetector.isScam(content)) {
138+
isSafe = false;
139+
}
140+
141+
if (isSafe) {
128142
return;
129143
}
130144

0 commit comments

Comments
 (0)