-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
72 lines (58 loc) · 2.17 KB
/
app.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
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const config = require('./config.json');
const logger = require('./helpers/logging');
const baseLogger = logger.baseLogger;
const contextLogger = new logger.ctxLogger(baseLogger);
baseLogger.info('PepoJS Init. Attempting to start, no promise I will work but sure can try :)');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
shards: 'auto',
});
// register events
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
baseLogger.info(`Registering Once Event: ${event.name}`);
client.once(event.name, (...args) => event.execute(...args, baseLogger));
}
else {
baseLogger.info(`Registering Reusable Event: ${event.name}`);
client.on(event.name, (...args) => {
const ctxLogger = contextLogger.addContextLogger(...args);
event.execute(...args, ctxLogger);
});
}
}
// register commands, loop through scope folders and load all commands
client.commands = new Collection();
for (const commandFolderScope of fs.readdirSync('./commands')) {
const commandFiles = fs.readdirSync(`./commands/${commandFolderScope}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${commandFolderScope}/${file}`);
baseLogger.info(`Registering ${commandFolderScope} slash command: ${command.data.name}`);
client.commands.set(command.data.name, command);
}
}
client.on('interactionCreate', async interaction => {
// build context
const ctx = {
interaction: interaction,
logger: contextLogger.addContextLogger(interaction),
};
if (!ctx.interaction.isCommand()) return;
const command = client.commands.get(ctx.interaction.commandName);
if (!command) return;
try {
ctx.logger.info(`Attempting to execute command ${ctx.interaction.commandName}`);
await command.execute(ctx);
}
catch (error) {
console.error(error);
}
});
process.on('unhandledRejection', err => {
baseLogger.error(err);
});
client.login(config.token);