From 2eff2a4879cb8bd512af85beffc3a20e4d385db2 Mon Sep 17 00:00:00 2001 From: EazyFTW Date: Thu, 23 Sep 2021 18:08:11 -0500 Subject: [PATCH] Cleanup. --- .../module/modules/UrlWhitelistModule.java | 64 +++++++++---------- .../module/modules/WordBlacklistModule.java | 49 ++++++++------ .../TechDiscordBot/objects/Requirement.java | 8 ++- 3 files changed, 65 insertions(+), 56 deletions(-) diff --git a/src/main/java/me/TechsCode/TechDiscordBot/module/modules/UrlWhitelistModule.java b/src/main/java/me/TechsCode/TechDiscordBot/module/modules/UrlWhitelistModule.java index 51945321..0d41109d 100644 --- a/src/main/java/me/TechsCode/TechDiscordBot/module/modules/UrlWhitelistModule.java +++ b/src/main/java/me/TechsCode/TechDiscordBot/module/modules/UrlWhitelistModule.java @@ -25,6 +25,7 @@ import java.util.regex.Pattern; public class UrlWhitelistModule extends Module { + private final DefinedQuery STAFF_ROLE = new DefinedQuery() { @Override protected Query newQuery() { @@ -34,10 +35,13 @@ protected Query newQuery() { private final DefinedQuery IGNORED_CATEGORIES = new DefinedQuery() { @Override - protected Query newQuery() { return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions", "\uD83C\uDFAB ︱Tickets"); } + protected Query newQuery() { + return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions", "\uD83C\uDFAB ︱Tickets"); + } }; - List whitelistedUrls = new ArrayList(); + private final List WHITELISTED_URLS = new ArrayList<>(); + private final String URL = "https://raw.githubusercontent.com/TechsCode-Team/TechBot-Whitelists/main/urlWhitelist.txt"; public UrlWhitelistModule(TechDiscordBot bot) { super(bot); @@ -50,16 +54,14 @@ public void onEnable() { getWhitelist(); try { - Thread.sleep(TimeUnit.MINUTES.toMillis(10)); //Wait every hour - } catch (Exception ignored) { - } + Thread.sleep(TimeUnit.MINUTES.toMillis(10)); + } catch (Exception ignored) { } } }).start(); } @Override - public void onDisable() { - } + public void onDisable() { } public String getName() { return "UrlWhitelistModule"; @@ -67,53 +69,49 @@ public String getName() { @SubscribeEvent public void onMessage(GuildMessageReceivedEvent e) { - if (e.getMember() == null) return; - if (e.getAuthor().isBot()) return; - if (e.getMember().getRoles().contains(STAFF_ROLE.query().first())) return; - if (IGNORED_CATEGORIES.query().stream().anyMatch(c -> c.getId().equals(e.getChannel().getParent().getId()))) return; + if (e.getMember() == null || e.getAuthor().isBot() || e.getMember().getRoles().contains(STAFF_ROLE.query().first()) || IGNORED_CATEGORIES.query().stream().anyMatch(c -> c.getId().equals(e.getChannel().getParent().getId()))) return; String message = e.getMessage().getContentRaw(); - boolean blockMessage = false; - - if (!whitelistedUrls.isEmpty()) { + if (!WHITELISTED_URLS.isEmpty()) { Set extractedUrls = extractUrls(message); - for (String extractedUrl : extractedUrls) { - if (!whitelistedUrls.contains(extractedUrl)) { - blockMessage = true; - break; - } - } + boolean blockMessage = extractedUrls.stream().anyMatch(extractedUrl -> !WHITELISTED_URLS.contains(extractedUrl)); if (blockMessage) { e.getMessage().delete().queue(); new TechEmbedBuilder("Blocked URL(s)") .color(Color.RED) - .text("Your message contained a URL which is not in our whitelist.\n\nIf you think this is a mistake, take a look at our [**link whitelist**](https://github.com/TechsCode-Team/TechBot-Whitelists/blob/main/urlWhitelist.txt).") + .text("Your message contained a URL which is not in our whitelist.\n\nIf you think this is a mistake, take a look at our [**link whitelist**](" + URL + ").") .sendTemporary(e.getChannel(), 10, TimeUnit.SECONDS); } } - } private void getWhitelist() { try { - URL url = new URL("https://raw.githubusercontent.com/TechsCode-Team/TechBot-Whitelists/main/urlWhitelist.txt"); + URL url = new URL(URL); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); int status = con.getResponseCode(); + if (status == 200) { - BufferedReader in = new BufferedReader( - new InputStreamReader(con.getInputStream())); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; + while ((inputLine = in.readLine()) != null) { - whitelistedUrls.add(inputLine.trim()); + String word = inputLine.trim().toLowerCase(); + + if(!WHITELISTED_URLS.contains(word)) + WHITELISTED_URLS.add(word); } + in.close(); } else { - System.err.println("Error getting url whitelist"); + TechDiscordBot.log("ERROR", "Failed to fetch the white listed urls."); } + con.disconnect(); } catch (Exception e) { e.printStackTrace(); @@ -132,22 +130,24 @@ private Set extractUrls(String text) { while (m.find()) { String regexResponse = m.group(0); - try{ + + try { URL url = new URL(regexResponse); String[] domainExploded = url.getHost().split("\\."); domain = domainExploded[domainExploded.length - 2] + "." + domainExploded[domainExploded.length - 1]; successfulParse = true; - }catch (Exception ignored){} + } catch (Exception ignored) {} - if(successfulParse){ + if(successfulParse) containedUrls.add(domain); - } } return containedUrls; } public Requirement[] getRequirements() { - return new Requirement[0]; + return new Requirement[] { + new Requirement(IGNORED_CATEGORIES, IGNORED_CATEGORIES.query().amount(), "Could not find all of the ignored category channels.") + }; } } diff --git a/src/main/java/me/TechsCode/TechDiscordBot/module/modules/WordBlacklistModule.java b/src/main/java/me/TechsCode/TechDiscordBot/module/modules/WordBlacklistModule.java index 5980d35b..32a62212 100644 --- a/src/main/java/me/TechsCode/TechDiscordBot/module/modules/WordBlacklistModule.java +++ b/src/main/java/me/TechsCode/TechDiscordBot/module/modules/WordBlacklistModule.java @@ -21,12 +21,16 @@ import java.util.regex.Pattern; public class WordBlacklistModule extends Module { + private final DefinedQuery IGNORED_CATEGORIES = new DefinedQuery() { @Override - protected Query newQuery() { return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions"); } + protected Query newQuery() { + return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions"); + } }; - List blackListedWords = new ArrayList(); + private final List BLACKLISTED_WORDS = new ArrayList<>(); + private final String URL = "https://raw.githubusercontent.com/TechsCode-Team/TechBot-Whitelists/main/wordBlacklist"; public WordBlacklistModule(TechDiscordBot bot) { super(bot); @@ -39,16 +43,14 @@ public void onEnable() { getBlacklist(); try { - Thread.sleep(TimeUnit.MINUTES.toMillis(10)); //Wait every hour - } catch (Exception ignored) { - } + Thread.sleep(TimeUnit.MINUTES.toMillis(10)); + } catch (Exception ignored) { } } }).start(); } @Override - public void onDisable() { - } + public void onDisable() { } public String getName() { return "WordBlacklistModule"; @@ -56,45 +58,48 @@ public String getName() { @SubscribeEvent public void onMessage(GuildMessageReceivedEvent e) { - if (e.getMember() == null) return; - if (e.getAuthor().isBot()) return; - if (IGNORED_CATEGORIES.query().stream().anyMatch(c -> c.getId().equals(e.getChannel().getParent().getId()))) return; + if (e.getMember() == null || e.getAuthor().isBot() || IGNORED_CATEGORIES.query().stream().anyMatch(c -> c.getId().equals(e.getChannel().getParent().getId()))) return; String message = e.getMessage().getContentRaw().toLowerCase(); + String blacklist = String.join("|", BLACKLISTED_WORDS); - String blacklist = String.join("|", blackListedWords); - - String regex = "[^!@#$%^&*]*("+blacklist+")[^!@#$%^&*]*"; + String regex = "[^!@#$%^&*]*(" + blacklist + ")[^!@#$%^&*]*"; boolean blockMessage= Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL).matcher(message).matches(); if (blockMessage) { e.getMessage().delete().queue(); new TechEmbedBuilder("Blocked Word(s)") .color(Color.RED) - .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**](https://github.com/TechsCode-Team/TechBot-Whitelists/blob/main/wordBlacklist).") + .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 + ").") .sendTemporary(e.getChannel(), 10, TimeUnit.SECONDS); } - } private void getBlacklist() { try { - URL url = new URL("https://raw.githubusercontent.com/TechsCode-Team/TechBot-Whitelists/main/wordBlacklist"); + URL url = new URL(URL); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); int status = con.getResponseCode(); + if (status == 200) { - BufferedReader in = new BufferedReader( - new InputStreamReader(con.getInputStream())); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; + while ((inputLine = in.readLine()) != null) { - blackListedWords.add(inputLine.trim().toLowerCase()); + String word = inputLine.trim().toLowerCase(); + + if(!BLACKLISTED_WORDS.contains(word)) + BLACKLISTED_WORDS.add(word); } + in.close(); } else { - System.err.println("Error getting world blacklist"); + TechDiscordBot.log("ERROR", "Failed to fetch the black listed words."); } + con.disconnect(); } catch (Exception e) { e.printStackTrace(); @@ -102,6 +107,8 @@ private void getBlacklist() { } public Requirement[] getRequirements() { - return new Requirement[0]; + return new Requirement[] { + new Requirement(IGNORED_CATEGORIES, IGNORED_CATEGORIES.query().amount(), "Could not find all of the ignored category channels.") + }; } } diff --git a/src/main/java/me/TechsCode/TechDiscordBot/objects/Requirement.java b/src/main/java/me/TechsCode/TechDiscordBot/objects/Requirement.java index d4f6ad2e..879d3978 100644 --- a/src/main/java/me/TechsCode/TechDiscordBot/objects/Requirement.java +++ b/src/main/java/me/TechsCode/TechDiscordBot/objects/Requirement.java @@ -11,15 +11,17 @@ public class Requirement { public Requirement(DefinedQuery query, int matchesRequired, String unmatchMessage) { Objects.requireNonNull(query, "Defined Query cannot be null!"); Objects.requireNonNull(unmatchMessage, "Message cannot be null!"); + this.query = query; this.matchesRequired = matchesRequired; this.unmatchMessage = unmatchMessage; } public boolean check() { - int matches = query.query().amount(); - return matches >= matchesRequired; + return query.query().amount() >= matchesRequired; } - public String getUnmatchMessage() { return unmatchMessage; } + public String getUnmatchMessage() { + return unmatchMessage; + } } \ No newline at end of file