Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
EazyFTW committed Sep 23, 2021
1 parent f219450 commit 2eff2a4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.regex.Pattern;

public class UrlWhitelistModule extends Module {

private final DefinedQuery<Role> STAFF_ROLE = new DefinedQuery<Role>() {
@Override
protected Query<Role> newQuery() {
Expand All @@ -34,10 +35,13 @@ protected Query<Role> newQuery() {

private final DefinedQuery<Category> IGNORED_CATEGORIES = new DefinedQuery<Category>() {
@Override
protected Query<Category> newQuery() { return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions", "\uD83C\uDFAB ︱Tickets"); }
protected Query<Category> newQuery() {
return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions", "\uD83C\uDFAB ︱Tickets");
}
};

List<String> whitelistedUrls = new ArrayList<String>();
private final List<String> 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);
Expand All @@ -50,70 +54,64 @@ 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";
}

@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<String> 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();
Expand All @@ -132,22 +130,24 @@ private Set<String> 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.")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
import java.util.regex.Pattern;

public class WordBlacklistModule extends Module {

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

List<String> blackListedWords = new ArrayList<String>();
private final List<String> BLACKLISTED_WORDS = new ArrayList<>();
private final String URL = "https://raw.githubusercontent.com/TechsCode-Team/TechBot-Whitelists/main/wordBlacklist";

public WordBlacklistModule(TechDiscordBot bot) {
super(bot);
Expand All @@ -39,69 +43,72 @@ 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";
}

@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();
}
}

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.")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 2eff2a4

Please sign in to comment.