-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (64 loc) · 2.33 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
const { Client, Collection, GatewayIntentBits, REST, Routes } = require('discord.js');
const fs = require('fs');
const path = require('path');
const express = require('express');
const { token, clientId, REST_API } = require('./config/config.json');
const logger = require('./utils/logger');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
// Load events
const loadEvents = (dir) => {
const eventFiles = fs.readdirSync(path.join(__dirname, dir)).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(path.join(__dirname, dir, file));
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
};
loadEvents('./events/client');
loadEvents('./events/guild');
// Load commands
client.commands = new Collection();
const commands = [];
const commandFolders = fs.readdirSync('./commands');
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
}
}
const rest = new REST({ version: '10' }).setToken(token);
(async () => {
try {
logger.info('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands(clientId),
{ body: commands },
);
logger.info('Successfully reloaded application (/) commands.');
} catch (error) {
logger.error(error);
}
})();
client.login(token).then(() => {
logger.info(`Logged in as ${client.user.tag}`);
});
if (REST_API) {
// Set up the Express server
const app = express();
const port = 3000;
// Store the Discord client in app locals
app.locals.client = client;
// Use the routes
const statsRoute = require('./api/v1/stats');
const commandsRoute = require('./api/v1/commands');
app.use('/api/v1', statsRoute);
app.use('/api/v1', commandsRoute);
app.listen(port, () => {
logger.info(`API server running on http://localhost:${port}`);
});
}