-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlashCommandCreator.js
50 lines (42 loc) · 1.21 KB
/
SlashCommandCreator.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
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const EventEmitter = require('events');
class SlashCommandCreator {
constructor(clientId, token, commandFile, client) {
this.commands = require(commandFile);
this.commands = this.commands.map(command => command.toJSON());
this.clientId = clientId;
this.token = token;
this.client = client;
this.rest = new REST({ version: '9' }).setToken(this.token);
this.emitter = new EventEmitter();
this.setupReplies();
return this;
}
on(event, listener) {
this.emitter.on(event, listener);
}
once(event, listener) {
this.emitter.once(event, listener);
}
setupReplies() {
this.client.on("interactionCreate", interaction => {
if (!interaction.isCommand()) return;
this.emitter.emit("command", interaction);
})
}
async reloadCommands(guildId) {
return new Promise(async (res, rej) => {
try {
await this.rest.put(
Routes.applicationGuildCommands(this.clientId, guildId),
{ body: this.commands },
);
res();
} catch (error) {
rej(error);
}
});
}
}
module.exports = SlashCommandCreator;