forked from hemisemidemipresent/cyberquincy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slash_command_center.js
103 lines (86 loc) · 2.98 KB
/
slash_command_center.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
98
99
100
101
102
103
fs = require('fs');
function commandFiles() {
const commandFiles = fs.readdirSync('./slash_commands').filter((file) => file.endsWith('.js'));
return commandFiles.map((file) => require(`./slash_commands/${file}`));
}
function configureCommands(client) {
client.commands = new Discord.Collection();
for (const command of commandFiles()) {
// Set a new item in the commands Collection
// With the key as the command name and the value as the exported module
try {
client.commands.set(command.data.name, command);
} catch (e) {
console.log('Error setting slash command: you probably have an invalid .js file in ./slash_commands/');
}
}
}
async function handleCommand(interaction) {
try {
const command = client.commands.get(interaction.commandName);
if (!command) return;
await command.execute(interaction);
} catch (error) {
console.error(error);
try {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
} catch {
// Unknown Interaction
}
}
}
async function handleButton(interaction) {
try {
const command = client.commands.get(interaction.message.interaction.commandName);
if (!command || !command.onButtonClick) return;
await command.onButtonClick(interaction);
} catch (e) {
console.log(e);
}
}
async function handleAutocomplete(interaction) {
try {
const command = client.commands.get(interaction.commandName);
if (!command || !command.onAutocomplete) return;
await command.onAutocomplete(interaction);
} catch (e) {
console.log(e);
}
}
async function handleSelectMenu(interaction) {
try {
const command = client.commands.get(interaction.message.interaction.commandName);
if (!command || !command.onSelectMenu) return;
await command.onSelectMenu(interaction);
} catch (e) {
console.log(e);
}
}
function extendStructure(_class) {
const prototype = _class.prototype;
const deferReply = prototype.deferReply;
prototype.deferReply = async function (options = {}) {
options.ephemeral = this.options.getBoolean('hide') ?? Boolean(options.ephemeral);
return deferReply.call(this, options);
};
const reply = prototype.reply;
prototype.reply = async function (options) {
const ephemeral = this.options.getBoolean('hide') ?? Boolean(options.ephemeral);
options = {
...(typeof options === 'object' ? options : { content: options }),
ephemeral
};
const message = await (this.deferred || this.replied ? this.followUp(options) : reply.call(this, options));
this.replied = true;
return message;
};
}
module.exports = {
commandFiles,
configureCommands,
handleCommand,
handleButton,
handleAutocomplete,
handleSelectMenu,
extendStructure
};