88import me .TechsCode .TechDiscordBot .util .TechEmbedBuilder ;
99import net .dv8tion .jda .api .entities .Category ;
1010import net .dv8tion .jda .api .events .message .guild .GuildMessageReceivedEvent ;
11+ import net .dv8tion .jda .api .events .message .guild .GuildMessageUpdateEvent ;
1112import net .dv8tion .jda .api .hooks .SubscribeEvent ;
1213
1314import java .awt .*;
1718import java .net .URL ;
1819import java .util .ArrayList ;
1920import java .util .List ;
21+ import java .util .Objects ;
2022import java .util .concurrent .TimeUnit ;
23+ import java .util .concurrent .atomic .AtomicBoolean ;
2124import java .util .regex .Pattern ;
2225
2326public 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 \n If 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