-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·375 lines (330 loc) · 13.7 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
/**
* @file Entry point, permissions, commands
* @author jojos38
*/
// -------------------- SETTINGS -------------------- //
const OWNER_ID = 137239068567142400;
const ACTIVITY_MESSAGE = "/help";
let logMessages = false;
// -------------------- SETTINGS -------------------- //
// -------------------- SOME VARIABLES -------------------- //
require('app-module-path').addPath(__dirname);
global.config = require('config.json');
// Others
const fs = require('fs');
const tools = require('tools.js');
const logger = require('logger.js');
const messages = require('messages.js');
// Database
const DatabaseManager = require('database-manager.js');
const db = new DatabaseManager();
// Discord
const {Client, Intents} = require('discord.js');
global.client = new Client({intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]});
// Language Manager
const LanguageManager = require('language-manager.js');
global.lm = new LanguageManager();
// Api Manager
const ApiManager = require('api-manager.js');
const apiManager = new ApiManager(client, config.id, config.discordTokens);
// Game Manager
const GameManager = require('game-manager.js');
const gameManager = new GameManager();
// -------------------- SOME VARIABLES -------------------- //
// ----------------------------------- SOME FUNCTIONS ----------------------------------- //
function getUserNickname(guild, user) {
let nick;
if (guild.members.cache.get(user.userID)) nick = guild.members.cache.get(user.userID).nickname || guild.members.cache.get(user.userID).user.username;
else nick = user.username;
if (nick == "") nick = "_";
return nick;
}
async function channelAllowed(guildID, channelID) {
const channels = await db.getGuildChannels(guildID);
console.log('OK');
console.log(channels);
for (let i = 0; i < channels.length; i++) // For each channel
if (channels[i].channelID == channelID) return true; // If message is sent from allowed channel then return
return false;
}
async function isAllowed(interaction, lang) {
// Owner perms
if (interaction.user.id === OWNER_ID) return true;
// Moderator perms
if (await isModeratorAllowed(interaction)) return true;
// Channel perm
if (await channelAllowed(interaction.guildId, interaction.channelId)) return true;
// If we went there is that the user is not allowed since previous for loop should return
const channels = await db.getGuildChannels(interaction.guildId);
await tools.replyCatch(interaction, lm.getNotAllowedEmbed(lang, messages.getChannelsString(channels, lang)), 1, true);
return false;
}
async function isModeratorAllowed(message) {
if (message.user.id === OWNER_ID) return true;
// Checking
if (!message) return false;
const member = message.member || message.guild.member(message.author);
if (!member) return false;
// Admin perms
return member.permissions.has('MANAGE_GUILD');
}
/**
* Add or remove a channel from the eyes of Observation
* @param add Either the channel will be added or removed (true / false)
* @param channel The channel to add or remove
* @returns {Promise<string>}
*/
async function updateChannelDB(add, channel) {
if (channel.type === 'GUILD_TEXT') {
const success = add ? db.addGuildChannel(channel.guild.id, channel.id) : db.removeGuildChannel(channel.id);
return add ? (success ? 'settings.channelAdded' : 'settings.alreadyAuthorized') : (success ? 'settings.channelDeleted' : 'settings.channelNotInList');
}
}
async function exitHandler(options, exitCode) {
if (options.cleanup) {
logger.info("stopping bot...");
logger.info("closing database...");
await db.close();
}
if (exitCode || exitCode === 0) logger.info("Exit code: " + exitCode); process.exit();
if (options.exit) process.exit();
}
process.on('exit', exitHandler.bind(null,{cleanup:true})); // Do something when app is closing
process.on('SIGINT', exitHandler.bind(null,{exit:true})); // Catches ctrl+c event
process.on('SIGUSR1', exitHandler.bind(null,{exit:true})); // Catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR2', exitHandler.bind(null,{exit:true})); // Catches "kill pid" (for example: nodemon restart)
process.on('uncaughtException', exitHandler.bind(null,{exit:true})); // Catches uncaught exceptions
// ----------------------------------- SOME FUNCTIONS ----------------------------------- //
// ---------------------------------------------- LISTENERS ---------------------------------------------- //
client.on("disconnect", () => {
logger.error("Connection to Discord's servers lost!");
});
client.on("channelDelete", function (channel) {
db.removeGuildChannel(channel.id);
});
client.on("guildCreate", guild => {
logger.info("New server: " + guild.name);
try { guild.owner.send(lm.getString("thanks", "en")); }
catch (error) { logger.error("Error while sending a PM to the user"); logger.error(error); }
});
client.on("guildDelete", guild => {
if (guild) {
db.resetGuildSettings(guild.id);
logger.info("Bot removed from server: " + guild.name);
}
});
client.on('interactionCreate', async function(interaction) {
if (!interaction.member || !interaction.guild || interaction.user.bot) return; // Make all checks
if (interaction.isButton()) {
const customID = interaction.customId;
const lang = await db.getSetting(interaction.guildId, 'lang');
// Admin commands
if (!await isAllowed(interaction, lang)) {
await tools.replyCatch(interaction, lm.getString('noPermission', lang), 0, true);
return
}
switch (customID) {
case 'resetConfirm':
await db.resetGuildSettings(interaction.guildId);
await tools.replyCatch(interaction, lm.getString('settings.resetted', lang), 0, true);
break;
case 'resetCancel':
await tools.replyCatch(interaction, lm.getString('settings.resetCancel', lang), 0, true);
break;
}
}
if (interaction.isCommand()) {
const cmd = interaction.commandName;
const lang = await db.getSetting(interaction.guildId, 'lang');
// #################################################### USER COMMANDS #################################################### //
// If allowed to send the command
if (!await isAllowed(interaction, lang)) return;
// #################################################### USER COMMANDS #################################################### //
// User commands
switch (cmd) {
case 'help':
await tools.replyCatch(interaction, lm.getHelpEmbed(lang), 1, true);
break;
case 'admin':
await tools.replyCatch(interaction, lm.getAdminHelpEmbed(lang), 1, true);
break;
case 'stats': {
let userStats = await db.getUserStats(interaction.guildId, interaction.user.id);
await tools.replyCatch(interaction, lm.getUserStatsEmbed(lang, userStats), 1, false);
break;
}
case 'info': {
const guilds = client.guilds.cache;
let users = 0;
guilds.forEach(g => {
users += g.memberCount;
});
const uptime = process.uptime();
await tools.replyCatch(interaction, lm.getInfoEmbed(lang, users, guilds.size, tools.format(uptime)), 1, false);
break;
}
case 'play': { // play
const difficulty = interaction.options.getInteger('difficulty');
const questions = interaction.options.getInteger('questions');
if (!gameManager.running(interaction.channelId))
gameManager.startClassicGame(db, interaction.guild, interaction.channel, interaction.user.id, lang, [difficulty, questions]);
else
await tools.replyCatch(interaction, lm.getAlreadyRunningEmbed(lang, interaction.channelId), 1, true);
await tools.replyCatch(interaction, 'Ok!', 0, true);
break;
}
case 'stop': {
gameManager.stopGame(interaction.channel, interaction.member, lm.getString("game.stoppedBy", lang, {player: tools.mention(interaction.user.id, 'u')}), lang);
await tools.replyCatch(interaction, 'Ok!', 0, true);
break;
}
case 'globaltop': {
let usersTable = [];
const users = await db.getAllUsers();
let totalUsers = users.length;
let position = -1;
const user = interaction.options.getUser('user');
// If there is a user ID
if (user) {
// Get the ID from the message
let userID = user.id;
// Get the user position in the list
for (let i = 0; i < totalUsers; i++) {
let user = users[i];
if (user.userID === userID) position = i;
}
if (position !== -1) {
// Show the 5 above and before users
if (position + 5 > totalUsers) position = totalUsers - 5;
if (position - 5 < 0) position = 5;
for (let i = position - 5; i < position + 5; i++) {
let user = users[i];
usersTable.push({
score: user.score,
won: user.won,
position: i,
username: getUserNickname(interaction.guild, user, i + 1)
});
}
}
} else {
for (let i = 0; i < totalUsers; i++) {
if (i >= 10) break;
let user = users[i];
usersTable.push({
score: user.score,
won: user.won,
position: i + 1,
username: getUserNickname(interaction.guild, user, i + 1)
});
}
}
if (totalUsers === 0 || (position === -1 && user))
await tools.replyCatch(interaction, lm.getTopNoStatsEmbed(lang, totalUsers), 1, false);
else
await tools.replyCatch(interaction, lm.getTopEmbed(lang, totalUsers, usersTable, position), 1, false);
}
}
// #################################################### MODERATOR COMMANDS #################################################### //
// If moderator allowed to send the command
if (!await isModeratorAllowed(interaction)) return;
// #################################################### MODERATOR COMMANDS #################################################### //
switch(cmd) {
case 'channels':
const channels = await db.getGuildChannels(interaction.guildId);
await tools.replyCatch(interaction, await messages.getChannelsString(channels, lang), 0, true);
break;
case 'add':
case 'remove':
const channel = interaction.options.getChannel('channel');
const result = await updateChannelDB(interaction.commandName === 'add', channel);
interaction.reply(lm.getString(result, lang, {channel: '<#' + channel.id + '>'}));
break;
case 'language': {
const botLang = interaction.options.getString('language');
await db.setSetting(interaction.guildId, 'lang', botLang);
await tools.replyCatch(interaction, lm.getString('settings.langSet', botLang, {lang: botLang}), 0, true);
break;
}
case 'delayanswer': {
const delay = interaction.options.getInteger('delay');
await db.setSetting(interaction.guildId, "answerDelay", delay);
await tools.replyCatch(interaction, lm.getString("settings.answerDelaySet", lang, {delay: delay}), 0, true);
break;
}
case 'delayquestion': {
const delay = interaction.options.getInteger('delay');
await db.setSetting(interaction.guildId, "questionDelay", delay);
await tools.replyCatch(interaction, lm.getString("settings.questionDelaySet", lang, {delay: delay}), 0, true);
break;
}
case 'defdifficulty': {
const difficulty = interaction.options.getInteger('difficulty');
await db.setSetting(interaction.guildId, "defaultDifficulty", difficulty);
await tools.replyCatch(interaction, lm.getString("settings.difficultySet", lang, {difficulty: difficulty}), 0, true);
break;
}
case 'defquestions': {
const questions = interaction.options.getInteger('questions');
await db.setSetting(interaction.guildId, "defaultQuestionsAmount", questions);
await tools.replyCatch(interaction, lm.getString("settings.questionsAmountSet", lang, {amount: questions}), 0, true)
break;
}
case 'reset':
await tools.replyCatch(interaction, {
content: lm.getString('settings.resetConfirm', lang),
components: [{
type: 1,
components: [
{type: 2, label: 'Confirm', style: 4, custom_id: 'resetConfirm'},
{type: 2, label: 'Cancel', style: 2, custom_id: 'resetCancel'}
]
}]
}, 2, true);
break;
}
}
});
client.on('messageCreate', async function (message) {
console.log('ok');
if (logMessages) logger.debug("Message received");
// Check if the message is not a PM
const guild = message.guild;
if (!guild) return;
// Check if the message is not from a bot
if(message.author.bot) return;
// Get guilds settings
const {lang, prefix} = await db.getSettings(guild.id, ["prefix", "lang"])
// Check if the message starts with the prefix
if (!message.content.startsWith(`${prefix}`)) return; // If message doesn't start with / then return
tools.sendCatch(message.channel, 'Messages commands are deprecated, please use slash (/) commands!\nIf you do not see the slash commands for Quizzar please ask for the bot to be re-invited using this link (DO NOT KICK THE BOT BEFORE RE-INVITING IT OVERWISE YOU WILL LOSE YOUR DATA, just invite it again even if it\'s already on the server!)\nhttps://discord.com/oauth2/authorize?client_id=586183772136013824&scope=applications.commands+bot&permissions=273488')
});
// ---------------------------------------------- LISTENERS ---------------------------------------------- //
// ----------------- START ----------------- //
function restoreCachedGames() {
if (!fs.existsSync("cache")) {
fs.mkdirSync("cache");
} else {
fs.readdir("./cache", function (err, files) {
if (err) logger.error(err);
for (file of files) {
let gameData = fs.readFileSync('cache/' + file);
let game = gameManager.restoreClassicGame(db, file.split('.').slice(0, -1).join('.'), gameData);
}
});
}
}
async function start() {
await db.init(); // Connect database
await lm.init(); // Load languages
logger.info("Connecting to Discord...");
await client.login(config.token);
}
client.once('ready', async function () {
client.user.setActivity(ACTIVITY_MESSAGE);
apiManager.init();
logger.info('Bot ready');
restoreCachedGames();
});
start();
// ----------------- START ----------------- //