Skip to content

Commit 4c6ef57

Browse files
committed
Enabled URL Blocking Module
1 parent 2052c8e commit 4c6ef57

1 file changed

Lines changed: 171 additions & 171 deletions

File tree

Lines changed: 171 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,171 @@
1-
//package me.TechsCode.TechDiscordBot.module.modules;
2-
//
3-
//import me.TechsCode.TechDiscordBot.TechDiscordBot;
4-
//import me.TechsCode.TechDiscordBot.module.Module;
5-
//import me.TechsCode.TechDiscordBot.objects.DefinedQuery;
6-
//import me.TechsCode.TechDiscordBot.objects.Query;
7-
//import me.TechsCode.TechDiscordBot.objects.Requirement;
8-
//import me.TechsCode.TechDiscordBot.util.TechEmbedBuilder;
9-
//import net.dv8tion.jda.api.entities.Category;
10-
//import net.dv8tion.jda.api.entities.Role;
11-
//import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
12-
//import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
13-
//import net.dv8tion.jda.api.hooks.SubscribeEvent;
14-
//
15-
//import java.awt.*;
16-
//import java.io.BufferedReader;
17-
//import java.io.InputStreamReader;
18-
//import java.net.HttpURLConnection;
19-
//import java.net.URL;
20-
//import java.util.ArrayList;
21-
//import java.util.LinkedHashSet;
22-
//import java.util.List;
23-
//import java.util.Set;
24-
//import java.util.concurrent.TimeUnit;
25-
//import java.util.regex.Matcher;
26-
//import java.util.regex.Pattern;
27-
//
28-
//public class UrlWhitelistModule extends Module {
29-
//
30-
// private final DefinedQuery<Role> STAFF_ROLE = new DefinedQuery<Role>() {
31-
// @Override
32-
// protected Query<Role> newQuery() {
33-
// return bot.getRoles("Staff");
34-
// }
35-
// };
36-
//
37-
// private final DefinedQuery<Category> IGNORED_CATEGORIES = new DefinedQuery<Category>() {
38-
// @Override
39-
// protected Query<Category> newQuery() {
40-
// return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions", "\uD83C\uDFAB ︱Tickets");
41-
// }
42-
// };
43-
//
44-
// private final List<String> WHITELISTED_URLS = new ArrayList<>();
45-
// private final String URL = "https://raw.githubusercontent.com/TechsCode-Team/TechBot-Whitelists/main/urlWhitelist.txt";
46-
//
47-
// public UrlWhitelistModule(TechDiscordBot bot) {
48-
// super(bot);
49-
// }
50-
//
51-
// @Override
52-
// public void onEnable() {
53-
// new Thread(() -> {
54-
// while (true) {
55-
// getWhitelist();
56-
//
57-
// try {
58-
// Thread.sleep(TimeUnit.MINUTES.toMillis(10));
59-
// } catch (Exception ignored) { }
60-
// }
61-
// }).start();
62-
// }
63-
//
64-
// @Override
65-
// public void onDisable() { }
66-
//
67-
// public String getName() {
68-
// return "UrlWhitelistModule";
69-
// }
70-
//
71-
// @SubscribeEvent
72-
// public void onMessage(GuildMessageReceivedEvent e) {
73-
// 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;
74-
//
75-
// boolean block = checkMessage(e.getMessage().getContentRaw());
76-
// if (block) {
77-
// e.getMessage().delete().queue();
78-
// new TechEmbedBuilder("Blocked URL(s)")
79-
// .color(Color.RED)
80-
// .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 + ").")
81-
// .sendTemporary(e.getChannel(), 10, TimeUnit.SECONDS);
82-
// }
83-
// }
84-
//
85-
// @SubscribeEvent
86-
// public void onMessageUpdate(GuildMessageUpdateEvent e) {
87-
// 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;
88-
//
89-
// boolean block = checkMessage(e.getMessage().getContentRaw());
90-
// if (block) {
91-
// e.getMessage().delete().queue();
92-
// new TechEmbedBuilder("Blocked URL(s)")
93-
// .color(Color.RED)
94-
// .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 + ").")
95-
// .sendTemporary(e.getChannel(), 10, TimeUnit.SECONDS);
96-
// }
97-
// }
98-
//
99-
// private boolean checkMessage(String message){
100-
// boolean blockMessage = false;
101-
// if (!WHITELISTED_URLS.isEmpty()) {
102-
// Set<String> extractedUrls = extractUrls(message);
103-
// blockMessage = extractedUrls.stream().anyMatch(extractedUrl -> !WHITELISTED_URLS.contains(extractedUrl));
104-
// }
105-
// return blockMessage;
106-
// }
107-
//
108-
// private void getWhitelist() {
109-
// try {
110-
// URL url = new URL(URL);
111-
//
112-
// HttpURLConnection con = (HttpURLConnection) url.openConnection();
113-
// con.setRequestMethod("GET");
114-
//
115-
// int status = con.getResponseCode();
116-
//
117-
// if (status == 200) {
118-
// BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
119-
// String inputLine;
120-
//
121-
// while ((inputLine = in.readLine()) != null) {
122-
// String word = inputLine.trim().toLowerCase();
123-
//
124-
// if(!WHITELISTED_URLS.contains(word))
125-
// WHITELISTED_URLS.add(word);
126-
// }
127-
//
128-
// in.close();
129-
// } else {
130-
// TechDiscordBot.log("ERROR", "Failed to fetch the white listed urls.");
131-
// }
132-
//
133-
// con.disconnect();
134-
// } catch (Exception e) {
135-
// e.printStackTrace();
136-
// }
137-
// }
138-
//
139-
// private Set<String> extractUrls(String text) {
140-
// Set<String> containedUrls = new LinkedHashSet<>();
141-
//
142-
// String domain = "";
143-
// boolean successfulParse = false;
144-
//
145-
// String regex = "(https?://(?:www\\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|https?://(?:www\\.|(?!www))[a-zA-Z0-9]+\\.[^\\s]{2,}|www\\.[a-zA-Z0-9]+\\.[^\\s]{2,})";
146-
// Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
147-
// Matcher m = p.matcher(text);
148-
//
149-
// while (m.find()) {
150-
// String regexResponse = m.group(0);
151-
//
152-
// try {
153-
// URL url = new URL(regexResponse);
154-
// String[] domainExploded = url.getHost().split("\\.");
155-
// domain = domainExploded[domainExploded.length - 2] + "." + domainExploded[domainExploded.length - 1];
156-
// successfulParse = true;
157-
// } catch (Exception ignored) {}
158-
//
159-
// if(successfulParse)
160-
// containedUrls.add(domain);
161-
// }
162-
//
163-
// return containedUrls;
164-
// }
165-
//
166-
// public Requirement[] getRequirements() {
167-
// return new Requirement[] {
168-
// new Requirement(IGNORED_CATEGORIES, IGNORED_CATEGORIES.query().amount(), "Could not find all of the ignored category channels.")
169-
// };
170-
// }
171-
//}
1+
package me.TechsCode.TechDiscordBot.module.modules;
2+
3+
import me.TechsCode.TechDiscordBot.TechDiscordBot;
4+
import me.TechsCode.TechDiscordBot.module.Module;
5+
import me.TechsCode.TechDiscordBot.objects.DefinedQuery;
6+
import me.TechsCode.TechDiscordBot.objects.Query;
7+
import me.TechsCode.TechDiscordBot.objects.Requirement;
8+
import me.TechsCode.TechDiscordBot.util.TechEmbedBuilder;
9+
import net.dv8tion.jda.api.entities.Category;
10+
import net.dv8tion.jda.api.entities.Role;
11+
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
12+
import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
13+
import net.dv8tion.jda.api.hooks.SubscribeEvent;
14+
15+
import java.awt.*;
16+
import java.io.BufferedReader;
17+
import java.io.InputStreamReader;
18+
import java.net.HttpURLConnection;
19+
import java.net.URL;
20+
import java.util.ArrayList;
21+
import java.util.LinkedHashSet;
22+
import java.util.List;
23+
import java.util.Set;
24+
import java.util.concurrent.TimeUnit;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
27+
28+
public class UrlWhitelistModule extends Module {
29+
30+
private final DefinedQuery<Role> STAFF_ROLE = new DefinedQuery<Role>() {
31+
@Override
32+
protected Query<Role> newQuery() {
33+
return bot.getRoles("Staff");
34+
}
35+
};
36+
37+
private final DefinedQuery<Category> IGNORED_CATEGORIES = new DefinedQuery<Category>() {
38+
@Override
39+
protected Query<Category> newQuery() {
40+
return bot.getCategories("\uD83D\uDCC1 | Archives", "\uD83D\uDCD1 | Staff Logs", "Other Staff Discussions", "staff discussions", "⚖ | Leadership-Discussions", "\uD83C\uDFAB ︱Tickets");
41+
}
42+
};
43+
44+
private final List<String> WHITELISTED_URLS = new ArrayList<>();
45+
private final String URL = "https://raw.githubusercontent.com/TechsCode-Team/TechBot-Whitelists/main/urlWhitelist.txt";
46+
47+
public UrlWhitelistModule(TechDiscordBot bot) {
48+
super(bot);
49+
}
50+
51+
@Override
52+
public void onEnable() {
53+
new Thread(() -> {
54+
while (true) {
55+
getWhitelist();
56+
57+
try {
58+
Thread.sleep(TimeUnit.MINUTES.toMillis(10));
59+
} catch (Exception ignored) { }
60+
}
61+
}).start();
62+
}
63+
64+
@Override
65+
public void onDisable() { }
66+
67+
public String getName() {
68+
return "UrlWhitelistModule";
69+
}
70+
71+
@SubscribeEvent
72+
public void onMessage(GuildMessageReceivedEvent e) {
73+
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;
74+
75+
boolean block = checkMessage(e.getMessage().getContentRaw());
76+
if (block) {
77+
e.getMessage().delete().queue();
78+
new TechEmbedBuilder("Blocked URL(s)")
79+
.color(Color.RED)
80+
.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 + ").")
81+
.sendTemporary(e.getChannel(), 10, TimeUnit.SECONDS);
82+
}
83+
}
84+
85+
@SubscribeEvent
86+
public void onMessageUpdate(GuildMessageUpdateEvent e) {
87+
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;
88+
89+
boolean block = checkMessage(e.getMessage().getContentRaw());
90+
if (block) {
91+
e.getMessage().delete().queue();
92+
new TechEmbedBuilder("Blocked URL(s)")
93+
.color(Color.RED)
94+
.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 + ").")
95+
.sendTemporary(e.getChannel(), 10, TimeUnit.SECONDS);
96+
}
97+
}
98+
99+
private boolean checkMessage(String message){
100+
boolean blockMessage = false;
101+
if (!WHITELISTED_URLS.isEmpty()) {
102+
Set<String> extractedUrls = extractUrls(message);
103+
blockMessage = extractedUrls.stream().anyMatch(extractedUrl -> !WHITELISTED_URLS.contains(extractedUrl));
104+
}
105+
return blockMessage;
106+
}
107+
108+
private void getWhitelist() {
109+
try {
110+
URL url = new URL(URL);
111+
112+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
113+
con.setRequestMethod("GET");
114+
115+
int status = con.getResponseCode();
116+
117+
if (status == 200) {
118+
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
119+
String inputLine;
120+
121+
while ((inputLine = in.readLine()) != null) {
122+
String word = inputLine.trim().toLowerCase();
123+
124+
if(!WHITELISTED_URLS.contains(word))
125+
WHITELISTED_URLS.add(word);
126+
}
127+
128+
in.close();
129+
} else {
130+
TechDiscordBot.log("ERROR", "Failed to fetch the white listed urls.");
131+
}
132+
133+
con.disconnect();
134+
} catch (Exception e) {
135+
e.printStackTrace();
136+
}
137+
}
138+
139+
private Set<String> extractUrls(String text) {
140+
Set<String> containedUrls = new LinkedHashSet<>();
141+
142+
String domain = "";
143+
boolean successfulParse = false;
144+
145+
String regex = "(https?://(?:www\\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|https?://(?:www\\.|(?!www))[a-zA-Z0-9]+\\.[^\\s]{2,}|www\\.[a-zA-Z0-9]+\\.[^\\s]{2,})";
146+
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
147+
Matcher m = p.matcher(text);
148+
149+
while (m.find()) {
150+
String regexResponse = m.group(0);
151+
152+
try {
153+
URL url = new URL(regexResponse);
154+
String[] domainExploded = url.getHost().split("\\.");
155+
domain = domainExploded[domainExploded.length - 2] + "." + domainExploded[domainExploded.length - 1];
156+
successfulParse = true;
157+
} catch (Exception ignored) {}
158+
159+
if(successfulParse)
160+
containedUrls.add(domain);
161+
}
162+
163+
return containedUrls;
164+
}
165+
166+
public Requirement[] getRequirements() {
167+
return new Requirement[] {
168+
new Requirement(IGNORED_CATEGORIES, IGNORED_CATEGORIES.query().amount(), "Could not find all of the ignored category channels.")
169+
};
170+
}
171+
}

0 commit comments

Comments
 (0)