Skip to content

Commit ac8f246

Browse files
committed
Fixed Word Blacklist
Fixed made by @Faab007 on Greazi's fork.
1 parent e666429 commit ac8f246

1 file changed

Lines changed: 46 additions & 10 deletions

File tree

src/main/java/me/TechsCode/TechDiscordBot/module/modules/WordBlacklistModule.java

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import me.TechsCode.TechDiscordBot.util.TechEmbedBuilder;
99
import net.dv8tion.jda.api.entities.Category;
1010
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
11+
import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
1112
import net.dv8tion.jda.api.hooks.SubscribeEvent;
1213

1314
import java.awt.*;
@@ -17,15 +18,17 @@
1718
import java.net.URL;
1819
import java.util.ArrayList;
1920
import java.util.List;
21+
import java.util.Objects;
2022
import java.util.concurrent.TimeUnit;
23+
import java.util.concurrent.atomic.AtomicBoolean;
2124
import java.util.regex.Pattern;
2225

2326
public class WordBlacklistModule extends Module {
2427

2528
private final DefinedQuery<Category> IGNORED_CATEGORIES = new DefinedQuery<Category>() {
2629
@Override
2730
protected Query<Category> newQuery() {
28-
return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions");
31+
return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "staff discussions", "⚖ | Leadership-Discussions");
2932
}
3033
};
3134

@@ -58,15 +61,22 @@ public String getName() {
5861

5962
@SubscribeEvent
6063
public void onMessage(GuildMessageReceivedEvent e) {
61-
if (e.getMember() == null || e.getAuthor().isBot() || IGNORED_CATEGORIES.query().stream().anyMatch(c -> c.getId().equals(e.getChannel().getParent().getId()))) return;
64+
if (e.getMember() == null || e.getAuthor().isBot() || IGNORED_CATEGORIES.query().stream().anyMatch(c -> c.getId().equals(Objects.requireNonNull(e.getChannel().getParent()).getId()))) return;
6265

63-
String message = e.getMessage().getContentRaw().toLowerCase();
64-
String blacklist = String.join("|", BLACKLISTED_WORDS);
66+
if (runMatcher(e.getMessage().getContentRaw().toLowerCase())){
67+
e.getMessage().delete().queue();
68+
new TechEmbedBuilder("Blocked Word(s)")
69+
.color(Color.RED)
70+
.text("Your message contained a world which is in our blacklist.\n\nIf you think this is a mistake, take a look at our [**word blacklist**](" + URL + ").")
71+
.sendTemporary(e.getChannel(), 10, TimeUnit.SECONDS);
72+
}
73+
}
6574

66-
String regex = "[^!@#$%^&*]*(" + blacklist + ")[^!@#$%^&*]*";
67-
boolean blockMessage= Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL).matcher(message).matches();
75+
@SubscribeEvent
76+
public void onMessageUpdate(GuildMessageUpdateEvent e) {
77+
if (e.getMember() == null || e.getAuthor().isBot() || IGNORED_CATEGORIES.query().stream().anyMatch(c -> c.getId().equals(Objects.requireNonNull(e.getChannel().getParent()).getId()))) return;
6878

69-
if (blockMessage) {
79+
if (runMatcher(e.getMessage().getContentRaw().toLowerCase())){
7080
e.getMessage().delete().queue();
7181
new TechEmbedBuilder("Blocked Word(s)")
7282
.color(Color.RED)
@@ -75,6 +85,18 @@ public void onMessage(GuildMessageReceivedEvent e) {
7585
}
7686
}
7787

88+
public boolean runMatcher(String message){
89+
AtomicBoolean blockMessage = new AtomicBoolean(false);
90+
for (String regex : BLACKLISTED_WORDS) {
91+
boolean match = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL).matcher(message).find();
92+
if (match) {
93+
blockMessage.set(true);
94+
break;
95+
}
96+
}
97+
return blockMessage.get();
98+
}
99+
78100
private void getBlacklist() {
79101
try {
80102
URL url = new URL(URL);
@@ -88,11 +110,25 @@ private void getBlacklist() {
88110
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
89111
String inputLine;
90112

113+
BLACKLISTED_WORDS.clear();
91114
while ((inputLine = in.readLine()) != null) {
92115
String word = inputLine.trim().toLowerCase();
93116

94-
if(!BLACKLISTED_WORDS.contains(word))
95-
BLACKLISTED_WORDS.add(word);
117+
String[] letters = word.split("");
118+
StringBuilder regex = new StringBuilder();
119+
regex.append("\\b(?i)(");
120+
for (int i = 0; i < letters.length; i++) {
121+
if (i == 0){
122+
regex.append(letters[i]).append("+(\\W|_)*");
123+
}else if(i == letters.length - 1){
124+
regex.append(letters[i]).append("+");
125+
}else{
126+
regex.append("(").append(letters[i]).append("?)+(\\W|_)*");
127+
}
128+
}
129+
regex.append(")");
130+
131+
BLACKLISTED_WORDS.add(regex.toString());
96132
}
97133

98134
in.close();
@@ -111,4 +147,4 @@ public Requirement[] getRequirements() {
111147
new Requirement(IGNORED_CATEGORIES, IGNORED_CATEGORIES.query().amount(), "Could not find all of the ignored category channels.")
112148
};
113149
}
114-
}
150+
}

0 commit comments

Comments
 (0)