-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
57 lines (48 loc) · 1.94 KB
/
main.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
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const gameday = require('./modules/gameday');
const globalCache = require("./modules/global-cache");
const queries = require("./database/queries");
const ReusableBrowser = require("./modules/classes/ReusableBrowser");
const { LOG_LEVEL } = require('./config/globals');
const LOGGER = require('./modules/logger')(process.env.LOG_LEVEL?.trim() || LOG_LEVEL.INFO);
const BOT = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
});
BOT.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);
BOT.commands.set(command.data.name, command);
}
BOT.once('ready', async () => {
LOGGER.info('Ready!');
globalCache.values.subscribedChannels = await queries.getAllSubscribedChannels();
LOGGER.info('Subscribed channels: ' + JSON.stringify(globalCache.values.subscribedChannels, null, 2));
globalCache.values.browser = new ReusableBrowser();
await gameday.statusPoll(BOT);
});
BOT.login(process.env.TOKEN?.trim()).then(() => {
LOGGER.info('bot successfully logged in');
});
BOT.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = BOT.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, BOT.guilds);
} catch (error) {
LOGGER.error(error);
if (interaction.deferred) {
await interaction.followUp({ content: 'error', ephemeral: true });
} else if (!interaction.replied) {
await interaction.reply({ content: 'error', ephemeral: true });
}
}
});