-
Notifications
You must be signed in to change notification settings - Fork 17
/
InteractionHandler.js
73 lines (63 loc) · 2.13 KB
/
InteractionHandler.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 fs = require(`fs`);
const Discord = require(`discord.js`);
class InteractionHandler {
constructor(client) {
// Dynamically load commands
this.commands = new Discord.Collection();
this.client = client;
fs.readdirSync(`./Commands`)
.filter(file => file.endsWith(`.js`))
.filter(file => file !== 'Command.js')
.map(file => require(`./Commands/${file}`))
.filter(cmd => cmd.name)
.forEach(cmd => this.commands.set(cmd.name.toLowerCase(), new cmd()), this);
}
/**
* Update commands
*/
async updateCommands() {
const data = [];
this.commands.forEach(async cmd => {
if (cmd.types.includes(`CHAT_INPUT`)) {
data.push({
name: cmd.name,
description: cmd.description,
options: cmd.options
});
}
if (cmd.types.includes(`USER`)) {
data.push({
name: cmd.name.toLowerCase().split(' ').map(function(word) {
return word.replace(word[0], word[0].toUpperCase());
}).join(' '), // Title Case*
type: `USER`
});
}
if (cmd.types.includes(`MESSAGE`)) {
data.push({
name: cmd.name.toLowerCase().split(' ').map(function(word) {
return word.replace(word[0], word[0].toUpperCase());
}).join(' '), // Title Case*
type: `MESSAGE`
});
}
});
// *This is a known bug for now that you can't set slash and context commands with the same name, so convert to title case for context.
if (process.env.NODE_ENV === `production`) {
await this.client.application?.commands.set(data);
} else {
await this.client.guilds.cache.first()?.commands.set(data);
}
}
/**
* Handles understanding an incoming interaction and passing it to the correct command handler.
* @param {Interaction} Interaction The Discord interaction object
*/
handleInteraction(Interaction) {
if (Interaction.isCommand() || Interaction.isContextMenu()) {
const command = this.commands.get(Interaction.commandName.toLowerCase());
command.execute(Interaction);
}
}
}
module.exports = InteractionHandler;