-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
87 lines (76 loc) · 3.57 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
const fs = require('node:fs');
const path = require('node:path');
const changingActivityLoop = require('./loops/changing activity');
const contestsScrapingLoop = require('./loops/contests scraping');
const contestsMessageLoop = require('./loops/contests message');
const problemMessageLoop = require('./loops/problem message');
const joiningMessage = require('./utility/joining message');
const { tokenTest, tokenProd, mongourlTest, mongourlProd, isProduction, tokenTelegramProd, tokenTelegramTest, channelTelegramProd, channelTelegramTest } = require('./config.json');
const { Client, Collection, GatewayIntentBits, EmbedBuilder, PermissionFlagsBits, ActivityType } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const TelegramBot = require('node-telegram-bot-api');
const tokenTelegram = (isProduction) ? tokenTelegramProd : tokenTelegramTest;
client.channelTelegram = (isProduction) ? channelTelegramProd : channelTelegramTest;
client.telegramBot = new TelegramBot(tokenTelegram, { polling: true });
process.env.TZ = 'Asia/Kolkata'
// Initilize mongoDB connection
const mongoose = require('mongoose');
client.database = require('./database/mongo.js');
mongoose.connect((isProduction) ? mongourlProd : mongourlTest, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB.'))
.catch((err) => console.log('Unable to connect to MongoDB.\nError: ' + err));
// Fetch and initilize all the slash commands from the commands folder
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
// Fetch and initilize all the interaction hooks from the interactions folder
let interactionCommands = [];
const interactionsPath = path.join(__dirname, 'interactions');
const interactionsFiles = fs.readdirSync(interactionsPath).filter(file => file.endsWith('.js'));
for (const file of interactionsFiles) {
const filePath = path.join(interactionsPath, file);
const command = require(filePath);
interactionCommands.push(command);
}
// On interaction check and run slash commands
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
}
});
// On interaction check and run interaction hooks
client.on('interactionCreate', async interaction => {
for (const command of interactionCommands) {
try {
await command(interaction);
} catch (error) {
console.error(error);
}
}
});
// Sends a joining message in the first channel of the server where it has send messages permission
client.on('guildCreate', async guild => await joiningMessage(guild));
// Start the contests updating loop and notifications sending loop when bot is ready
let loopsInitialized = false;
client.once('ready', () => {
if (!loopsInitialized) {
changingActivityLoop(client);
contestsMessageLoop(client);
problemMessageLoop(client);
contestsScrapingLoop(client.database);
loopsInitialized = true;
}
console.log('Bot is online.');
});
// Login and start the discord bot
client.login(isProduction ? tokenProd : tokenTest);