-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathInteractionHandler.js
85 lines (74 loc) · 2.32 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
74
75
76
77
78
79
80
81
82
83
84
85
const fs = require(`fs`);
const Discord = require(`discord.js`);
class InteractionHandler {
constructor(client) {
// Dynamically load commands
this.commands = new Discord.Collection();
this.buttons = 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);
fs.readdirSync(`./Buttons`)
.filter(file => file.endsWith(`.js`))
.filter(file => file !== `Button.js`)
.map(file => require(`./Buttons/${file}`))
.filter(cmd => cmd.name)
.forEach(cmd => this.buttons.set(cmd.name.toLowerCase(), new cmd()), this);
}
/**
* Create slash commands
*/
async createCommands() {
// TODO: Loop over guilds?
const data = [];
this.commands.forEach(async cmd => {
data.push({
name: cmd.name,
description: cmd.description,
options: cmd.options
});
});
//console.log('Create - guilds', await this.client.guilds.cache);
await this.client.guilds.cache.first()?.commands.set(data);
}
/**
* Update slash commands
*/
async updateCommands() {
const data = [];
this.commands.forEach(async cmd => {
data.push({
name: cmd.name,
description: cmd.description,
options: cmd.options
});
});
// Loop over guilds?
this.client.guilds.cache.forEach(async guild => {
await guild.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.commandName == `help`){
const command = this.commands.get(`help`);
command.execute(Interaction, this.commands);
} else if (Interaction.isCommand()) {
const command = this.commands.get(Interaction.commandName);
command.execute(Interaction);
} else if (Interaction.isButton()) {
const button = this.buttons.get(Interaction.customId);
button.execute(Interaction);
} else {
return;
}
}
}
module.exports = InteractionHandler;