-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
293 lines (229 loc) · 9.54 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const { Client, REST, Routes, GatewayIntentBits } = require("discord.js");
const { handleTeam } = require("./commandHandler/team");
const { handleUser } = require("./commandHandler/user");
const { handleDebug } = require("./commandHandler/debug");
const { mongoClient } = require("./mongodb");
const { badges } = require("./data/badges.json");
const banList = require("./data/bannedwords.json");
const { commands } = require("./data/commands.js");
require("dotenv").config();
const BOT_TOKEN = process.env.BOT_TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
const DATABASE = process.env.DATABASE;
const HACKER_COLLECTION = process.env.HACKER_COLLECTION;
const SIGNUPS_COLLECTION = process.env.SIGNUPS_COLLECTION;
const TEAM_COLLECTION = process.env.TEAM_COLLECTION;
const EXEC_ID = process.env.EXEC_ID;
const TEACHER_ID = process.env.TEACHER_ID;
const FORMER_EXEC_ID = process.env.FORMER_EXEC_ID;
const HACKER_ID = process.env.HACKER_ID;
const QUARANTINE_ID = process.env.QUARANTINE_ID;
const SERVER_ID = process.env.SERVER_ID;
const LOG_CHANNEL_ID = process.env.LOG_CHANNEL_ID;
const run = async () => {
const handlerDictionary = {
"team": handleTeam,
"user": handleUser,
"debug": handleDebug,
}
const rest = new REST({ version: "10" }).setToken(BOT_TOKEN);
try {
console.log("Started refreshing application (/) commands.");
await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands });
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages] });
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
if (message.guildId != SERVER_ID) return;
if (message.author.bot) return false;
let flagged = false;
message.content.split(" ").forEach((word) => {
if (banList.includes(word)) {
flagged = true;
}
});
if (!flagged) {
return;
}
message.delete();
message.author.send("# Please refrain from using inappropriate language in the YRHacks Discord Server.").catch(err => console.log(err));
logAction(message, `<@${message.author.id}>'s message got flagged \`${message.content}\`.`);
});
client.on("interactionCreate", async interaction => {
if (interaction.guildId != SERVER_ID) {
if (interaction.isCommand()) {
await interaction.reply({
embeds: [
{
description: "You can only run commands in YRHacks Discord Server.",
timestamp: new Date().toISOString(),
color: "8076741"
}
],
ephemeral: true
}).catch(err => console.log(err));
}
return;
}
if (interaction.isAutocomplete()) {
const db = mongoClient.db(DATABASE);
const collection = db.collection(TEAM_COLLECTION);
const focusedValue = interaction.options.getFocused();
if (interaction.options.getSubcommand() == "accept") {
const invited = await collection.find({ invited: interaction.user.username }).toArray();
const names = invited.map(t => t.teamName);
const filtered = names.filter((t) => t.startsWith(focusedValue));
await interaction.respond(
filtered.slice(0, 25).map(v => ({ name: v, value: v }))
).catch(err => console.log(err));
} else if (interaction.options.getSubcommand() == "view") {
const teams = await collection.distinct("teamName");
const filtered = teams.filter((t) => t.startsWith(focusedValue));
await interaction.respond(
filtered.slice(0, 25).map(v => ({ name: v, value: v }))
).catch(err => console.log(err));
} else if (interaction.options.getSubcommand() == "addbadge") {
await interaction.respond(
Object.keys(badges).map(v => ({ name: v, value: v }))
).catch(err => console.log(err));
}
}
if (!interaction.isCommand()) return;
if (!(await existsUser(interaction.user.username))) {
await addUser(interaction.member);
}
if (interaction.commandName === "ping") {
await interaction.reply("Pong! <@" + interaction.user.id + ">").catch(err => console.log(err));
return;
}
if (handlerDictionary.hasOwnProperty(interaction.commandName)) {
handlerDictionary[interaction.commandName](interaction);
} else {
await interaction.reply({
embeds: [{
description: `Command name ${interaction.commandName} not found.`,
color: "8076741"
}]
}).catch(err => console.log(err));
}
});
client.on("guildMemberAdd", async member => {
console.log(member.user.username);
try {
const db = mongoClient.db(DATABASE);
const collection = db.collection(SIGNUPS_COLLECTION);
const whitelisted = await collection.findOne({
discord: member.user.username,
}, { collation: { strength: 2, locale: 'en' } });
if (!whitelisted) {
await member.user.send(
`
# Sorry!
You are not whitelisted for YRHacks' Discord Server.
> If you believe that this was a mistake:
> - Please use the contact form at [www.yrhacks.ca](https://yrhacks.ca/#contact).
> - Or fill out the Google Form posted on the YRHacks Google Classroom to sign up for a team.
`
).catch(err => console.log(err));
const role = member.guild.roles.cache.find(r => r.id == QUARANTINE_ID);
member.roles.add(role);
return;
}
const role = member.guild.roles.cache.find(r => r.id == HACKER_ID);
member.roles.add(role);
} catch (error) {
console.error("Error validating hacker:", error);
}
addUser(member);
});
client.on("guildMemberUpdate", async (oldMember, newMember) => {
const USERNAME = newMember.user.username;
if (oldMember.roles.cache.size !== newMember.roles.cache.size) {
const db = mongoClient.db(DATABASE);
const hackerCollection = db.collection(HACKER_COLLECTION);
let badges = [];
if (newMember.roles.cache.has(EXEC_ID)) {
badges.push("Executive");
}
if (newMember.roles.cache.has(TEACHER_ID)) {
badges.push("Teacher");
}
if (newMember.roles.cache.has(FORMER_EXEC_ID)) {
badges.push("Former Exec");
}
const result = await hackerCollection.updateOne(
{ username: USERNAME },
{ $set: { badges: badges } }
);
}
});
client.login(BOT_TOKEN);
}
const addUser = async (member) => {
const username = member.user.username;
try {
const db = mongoClient.db(DATABASE);
const hackerCollection = db.collection(HACKER_COLLECTION);
const signupCollection = db.collection(SIGNUPS_COLLECTION);
const existingHacker = await existsUser(username);
if (!existingHacker) {
const signupData = await signupCollection.findOne({
discord: username,
});
let badges = [];
if (member.roles.cache.has(EXEC_ID)) {
badges.push("Executive");
}
if (member.roles.cache.has(TEACHER_ID)) {
badges.push("Teacher");
}
if (member.roles.cache.has(FORMER_EXEC_ID)) {
badges.push("Former Exec");
}
const { firstName, lastName } = signupData;
const hacker_data = {
username: username,
fullName: `${firstName} ${lastName}`,
aboutMe: "No information provided...",
team: "N/A",
leader: false,
inTeam: false,
badges: badges,
...signupData
};
member.setNickname(hacker_data.fullName)
.catch(err => console.log(err)); // Error catching without try catch
const result = await hackerCollection.insertOne({ ...hacker_data });
}
} catch (error) {
console.error("Error inserting hacker:", error);
}
}
const existsUser = async (username) => {
try {
const db = mongoClient.db(DATABASE);
const collection = db.collection(HACKER_COLLECTION);
const existingHacker = await collection.findOne({
username: username,
});
return existingHacker ? true : false;
} catch (error) {
console.error("Error checking hacker:", error);
}
return false;
}
const logAction = async (interaction, message) => {
const channel = interaction.client.channels.cache.find(c => c.id == LOG_CHANNEL_ID)
channel.send({
embeds: [{
description: message,
color: "8076741"
}]
})
}
run();