-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.js
97 lines (86 loc) · 2.46 KB
/
startup.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
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { logger } = require('./logger');
const { db } = require('./db');
const startup = async (plugins) => {
const client = new Client({
intents: [GatewayIntentBits.Guilds]
});
const commands = new Collection();
const cleanups = [];
client.on(Events.InteractionCreate, async (interaction) => {
logger.silly(
interaction,
interaction.isChatInputCommand(),
interaction.commandName,
interaction.channelId,
interaction.member,
interaction.user,
interaction.memberPermissions
);
const command = commands.get(interaction.commandName);
if (!command) {
logger.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
ephemeral: true
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
}
});
client.on(Events.ClientReady, (client) => {
logger.info(`${client.user.tag} activated`);
logger.info(
`Connected to ${client.guilds.cache.size} servers with a total of ${client.users.cache.size} users.`
);
client.user.setActivity('[active]');
});
const { settings } = await db.get();
logger.level = settings.logLevel;
for (let i = 0; i < plugins.length; i++) {
const plugin = require(plugins[i]);
if (plugin.startup) {
await plugin.startup(client);
}
if (plugin.cleanup) {
cleanups.push(plugin.cleanup);
}
if (plugin.commands) {
plugin.commands.forEach((command) => {
commands.set(command.data.name, command);
});
}
}
const shutdown = () => {
logger.info('Shutting down');
db.release();
client.destroy();
cleanups.forEach((cleanup) => cleanup());
};
process.on('unhandledRejection', (reason, p) => {
logger.error(
'Unhandled Rejection at: Promise',
p,
'reason:',
reason,
'stack:',
reason.stack
);
});
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
client.login(settings.discord.token);
return client;
};
module.exports = { startup };