-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
413 lines (336 loc) · 14.2 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
const rolesInfo = require("./config/roles");
const channelsInfo = require("./config/channels");
const newsInfo = require("./config/news");
const gamesInfo = require("./config/games");
// Sequelize
const models = require('./models/');
// Bot
const router = require('./bot/router');
// Utils
const logger = require('./utils/logger');
const leveling = require('./utils/leveling');
const role = require('./utils/role');
const helpers = require('./utils/helpers');
const moment = require('moment');
const CronJob = require('cron').CronJob;
const news = require('./service/news');
const memberCron = require('./service/members');
// Database
const db_member = require('./database/member');
// Bot
const prefix = "#!";
const parser = require('./bot/parser.js');
let logChannel, welcomeChannel, afkChannelID, defaultRole, residentRole, pilierRole, missingRole;
require('./client')().then(({client, guild}) => {
client.on('ready', async () => {
// Set default channels
welcomeChannel = guild.channels.cache.find(channel => channel.name === channelsInfo.welcomeChannel);
if(!welcomeChannel) {
await guild.channels.create(channelsInfo.welcomeChannel);
logger.addLog("Missing channel: " + channelsInfo.welcomeChannel + " in guild, creating one: Please make sure to setup its permission.");
}
logChannel = guild.channels.cache.find(channel => channel.name === channelsInfo.logChannel);
if(!logChannel) {
await guild.channels.create(channelsInfo.logChannel);
logger.addLog("Missing channel: " + channelsInfo.logChannel + " in guild, creating one: Please make sure to setup its permission.");
}
afkChannelID = guild.afkChannelID;
// Set default role
pilierRole = guild.roles.cache.find(role => role.name === rolesInfo.pilier.name);
if (!pilierRole) {
pilierRole = await guild.roles.create({
data: {
name: rolesInfo.pilier.name,
color: rolesInfo.pilier.color
}
});
logger.addLog("Missing role: " + rolesInfo.pilier.name + " in guild, creating one: Please make sure to setup its permission.");
}
residentRole = guild.roles.cache.find(role => role.name === rolesInfo.resident.name);
if (!residentRole) {
residentRole = await guild.roles.create({
data: {
name: rolesInfo.resident.name,
color: rolesInfo.resident.color
}
});
logger.addLog("Missing role: " + rolesInfo.resident.name + " in guild, creating one: Please make sure to setup its permission.");
}
defaultRole = guild.roles.cache.find(role => role.name === rolesInfo.default.name);
if (!defaultRole) {
defaultRole = await guild.roles.create({
data: {
name: rolesInfo.default.name,
color: rolesInfo.default.color
}
});
logger.addLog("Missing role: " + rolesInfo.default.name + " in guild, creating one: Please make sure to setup its permission.");
}
missingRole = guild.roles.cache.find(role => role.name === rolesInfo.missing.name);
if (!missingRole) {
missingRole = await guild.roles.create({
data: {
name: rolesInfo.missing.name,
color: rolesInfo.missing.color
}
});
logger.addLog("Missing role: " + rolesInfo.missing.name + " in guild, creating one: Please make sure to setup its permission.");
}
// Generate game role
let gameRole;
for (var i = 0; i < gamesInfo.length; i++) {
gameRole = guild.roles.cache.find(role => role.name === gamesInfo[i].rolename);
if(!gameRole) {
await guild.roles.create({
data: {
name: gamesInfo[i].rolename
}
});
logger.addLog("Missing game role: " + gamesInfo[i].rolename + " in guild, creating one: Please make sure to setup its permission.");
}
}
console.log(`Logged in as ${client.user.tag}!`);
const job = new CronJob('0 * * * *', () => {
(async () => {
console.time("NEWS DURATION");
// Steam News
for (var i = 0; i < newsInfo.steam.length; i++) {
try {
await news.steamNews(newsInfo.steam[i].id, newsInfo.steam[i].chan);
} catch(err) {
console.error("ERROR FOR STEAM NEWS => " + newsInfo.steam[i].game);
console.error(err.message);
}
}
// Jvc News
for (var i = 0; i < newsInfo.jvc.length; i++) {
try {
await news.jvcNews(newsInfo.jvc[i], newsInfo.jvc[i].chan);
} catch(err) {
console.error("ERROR FOR JVC NEWS => " + newsInfo.jvc[i].game);
}
}
// Missing person
try {
await memberCron.checkMissingPersons(guild, missingRole.id, residentRole.id, pilierRole.id);
} catch(err) {
console.error(err);
}
// Update pseudo in DB
try {
await memberCron.updateDbPseudo(guild, missingRole.id, residentRole.id, pilierRole.id);
} catch(err) {
console.error(err);
}
// Update user game role
try {
await memberCron.updateGameRole(guild, missingRole.id, residentRole.id, pilierRole.id);
} catch(err) {
console.error(err);
}
console.timeEnd("NEWS DURATION");
})().catch(err => {
console.error(err);
});
});
job.start();
// Check missing on start
setTimeout(function(){
try {
memberCron.checkMissingPersons(guild, missingRole.id, residentRole.id, pilierRole.id);
memberCron.updateDbPseudo(guild);
memberCron.updateGameRole(guild);
} catch(err) {
console.error(err);
}
}, 3000);
});
client.on('guildMemberAdd', member => {
logger.debugLog('------ guildMemberAdd ------');
(async () => {
await member.roles.add(defaultRole);
// Remove to avoid double call at the same time with getDbMember that trigger simultaneously the addNewMember fn
await db_member.addNewMember(member.user);
// Welcome message
var welcomeMsg = helpers.generateBotAnswer("newArrival", {
"__ARRIVALUSERID__": member.user.id,
"__NOOTID__": "214087533208928256"
})
welcomeChannel.send(welcomeMsg);
logger.debugLog('------ guildMemberAdd END ------');
})().catch(err => {
logger.addError(err);
})
});
client.on('message', msg => {
(async () => {
if (msg.author.bot)
return;
logger.debugLog('------ message ------');
// Check if we have to remove the Missing role
await role.checkToRemoveMissingPerson(msg.member, missingRole.id, residentRole.id, pilierRole.id);
// Check if we have to add the Resident role
await role.checkToAddResident(msg.member, residentRole.id);
// Check if we have to add the Pilier de bar role
await role.checkToAddPilier(msg.member, pilierRole.id);
// It is not a bot command
if (msg.content.substring(0, 2) != prefix){
await leveling.doXp(msg); // Add text XP
return logger.debugLog('------ message END 2 ------');
}
let command = msg.content.substring(2);
let data = parser.parse(command);
if(data.error)
throw data.error;
data.msg = msg;
// Routing command
let answer = await router[data.function](data);
if (Array.isArray(answer)) {
for (var i = 0; i < answer.length; i++) {
(function(ibis) {
setTimeout(_ => {
if (answer[ibis].reply)
msg.reply(answer[ibis].message);
else
msg.channel.send(answer[ibis].message);
}, answer[ibis].wait || 0);
})(i);
}
} else if (typeof answer === "object") {
if (answer.reply)
msg.reply(answer.message);
else
msg.channel.send(answer.message);
} else if(answer === true) {
msg.reply("OK !");
} else {
msg.reply(answer);
}
logger.debugLog('------ message END 3 ------');
})().catch(err => {
logger.addError(err);
msg.reply(err.message || "Désolé, une erreur s'est produite... Voit avec Syno ! Niark niark.");
});
});
client.on('voiceStateUpdate', (oldState, newState) => {
(async () => {
let oldMember = oldState.member;
let newMember = newState.member;
if (oldMember.user.bot || newMember.user.bot)
return false;
logger.debugLog('------ voiceStateUpdate ------');
let oldUserChannel = oldState.channel;
let newUserChannel = newState.channel;
let newChanMembers = [];
let oldChanMembers = [];
if (newUserChannel)
newChanMembers = newUserChannel.members.array();
if (oldUserChannel)
oldChanMembers = oldUserChannel.members.array();
// Start XP earning on someone that was alone in a voice chan
async function notAloneAnymore(arrivingMember, membersInChan) {
// Looking for the user that was alone
const aloneMember = membersInChan.filter(x => x.id != arrivingMember.user.id)[0].user;
// Start XP earning for the guys that was alone
await db_member.enterVoiceChannel(aloneMember)
let msg = moment().format("HH:mm:ss") + ": " + aloneMember.username + " was alone in " + newUserChannel.name + ". But " + arrivingMember.user.username + " join him/her.";
logger.addLog(msg);
logChannel.send(msg);
}
// Stop XP earning on someone that become alone in a voice chan
async function foreverAlone(theAloneGuy) {
const xpInfos = await leveling.doVoiceXp(theAloneGuy.user);
const member = await db_member.leaveVoiceChannel(theAloneGuy.user, xpInfos.timeInChannel);
let answer = moment().format("HH:mm:ss") + ": " + theAloneGuy.user.username + " now alone in " + oldUserChannel.name + ". LvL " + member.level + " -> " + member.xp + " total XP ( " + xpInfos.timeInChannel + " min = " + xpInfos.earnedXp + " XP ).";
logger.addLog(answer);
logChannel.send(answer);
}
// An user join an active voice channel
if (!oldUserChannel && newUserChannel) {
// Check if we have to remove the Missing role
await role.checkToRemoveMissingPerson(newMember, missingRole.id, residentRole.id, pilierRole.id);
// Check if we have to add the Résident role
await role.checkToAddResident(newMember, residentRole.id);
// Check if we have to add the Pilier de bar role
await role.checkToAddPilier(newMember, pilierRole.id);
// IF the user join AFK chan then we stop here
if (newUserChannel.id == afkChannelID)
return logger.debugLog('------ voiceStateUpdate END 1 ------');
// The user is alone, we stop here too
if (newChanMembers.length <= 1)
return logger.debugLog('------ voiceStateUpdate END 2 ------');
// A user was alone before the new user arrive, so we need to start XP for this guy too
if (newChanMembers.length == 2)
notAloneAnymore(newMember, newChanMembers);
await db_member.enterVoiceChannel(newMember.user);
let msg = moment().format("HH:mm:ss") + ": " + newMember.user.username + " join " + newUserChannel.name + ".";
logger.addLog(msg);
logChannel.send(msg);
}
// User leave totally Discord by disconnecting
else if (!newUserChannel) {
// User leave discord and was already in AFK chan, then we don't care go away captain !
if (oldUserChannel.id == afkChannelID)
return logger.debugLog('------ voiceStateUpdate END 3 ------');
// Leaving a guy behind alone :'(
if (oldChanMembers.length == 1)
await foreverAlone(oldChanMembers[0]);
let xpInfos = await leveling.doVoiceXp(oldMember.user);
const dbMember = await db_member.leaveVoiceChannel(oldMember.user, xpInfos.timeInChannel);
let msg = moment().format("HH:mm:ss") + ": " + oldMember.user.username + " leave " + oldUserChannel.name + ". LvL " + dbMember.level + " -> " + dbMember.xp + " total XP ( " + xpInfos.timeInChannel + "min = " + xpInfos.earnedXp + " XP ). Now disconnected.";
logger.addLog(msg);
logChannel.send(msg);
}
// Handling switch between voice chan
else {
// Come from AFK chan and not alone in new chan
if (oldUserChannel.id == afkChannelID && newChanMembers.length > 1) {
await db_member.enterVoiceChannel(newMember.user);
let msg = moment().format("HH:mm:ss") + ": " + newMember.user.username + " join " + newUserChannel.name + ".";
logger.addLog(msg);
logChannel.send(msg);
}
// Go in AFK from other voice chan
else if (oldUserChannel !== undefined && newUserChannel.id == afkChannelID && oldChanMembers.length >= 1) {
const xpInfos = await leveling.doVoiceXp(oldMember.user);
const dbMember = await db_member.leaveVoiceChannel(newMember.user, xpInfos.timeInChannel);
let answer = moment().format("HH:mm:ss") + ": " + oldMember.user.username + " leave " + oldUserChannel.name + ". LvL " + dbMember.level + " -> " + dbMember.xp + " total XP ( " + xpInfos.timeInChannel + " min = " + xpInfos.earnedXp + " XP ). Now AFK.";
logger.addLog(answer);
logChannel.send(answer);
}
// Check if in the old chan there is now a alone guy that we'll stop XP earning
if (oldChanMembers.length == 1 && oldChanMembers[0].user.id != newMember.user.id)
await foreverAlone(oldChanMembers[0]);
// New channel handling, if it's AFK then we stop here
if (newUserChannel.id == afkChannelID)
return logger.debugLog('------ voiceStateUpdate END 4 ------');
// Check if in the new chan there is a alone guy that need XP
if (newChanMembers.length == 2 && (newUserChannel.id != oldUserChannel.id))
await notAloneAnymore(newMember, newChanMembers);
// Check if the guy that switch was alone in the old chan to activate XP
if (oldChanMembers.length == 0 && newChanMembers.length >= 2 && oldUserChannel.id != afkChannelID) {
await db_member.enterVoiceChannel(newMember.user);
let msg = moment().format("HH:mm:ss") + ": Member " + oldMember.user.username + " not alone anymore: he join " + newUserChannel.name + " with others.";
logger.addLog(msg);
logChannel.send(msg);
}
// Check if the guy go alone in another voice chan
if ((newChanMembers.length == 1 && oldChanMembers.length >= 1) && (newUserChannel.id != oldUserChannel.id))
await foreverAlone(newChanMembers[0]);
}
logger.debugLog('------ voiceStateUpdate END 5 ------');
})().catch(err => {
logger.addError(err);
});
});
});
if(process.argv[2] == 'moneysync') {
console.log("moneysync");
models.Member.findAll().then(members => {
for (let i = 0; i < members.length; i++) {
members[i].update({
money: 100 + parseInt(members[i].level)
});
}
})
}