-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
56 lines (43 loc) · 2.06 KB
/
index.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
const Discord = require('discord.js');
module.exports = async (client, commands, options = {
debug: false,
guildId: null
}) => {
const log = (message) => options.debug && console.log(message);
const ready = client.readyAt ? Promise.resolve() : new Promise(resolve => client.once('ready', resolve));
await ready;
const currentCommands = await client.application.commands.fetch(options.guildId && { guildId: options.guildId });
log(`Synchronizing commands...`);
log(`Currently ${currentCommands.size} commands.`);
const newCommands = commands.filter((command) => !currentCommands.some((c) => c.name === command.name));
for (let newCommand of newCommands) {
await client.application.commands.create(newCommand, options.guildId);
}
log(`Created ${newCommands.length} commands!`);
const deletedCommands = currentCommands.filter((command) => !commands.some((c) => c.name === command.name)).toJSON();
for (let deletedCommand of deletedCommands) {
await deletedCommand.delete();
}
log(`Deleted ${deletedCommands.length} commands!`);
const updatedCommands = commands.filter((command) => currentCommands.some((c) => c.name === command.name));
let updatedCommandCount = 0;
for (let updatedCommand of updatedCommands) {
const newCommand = updatedCommand;
const previousCommand = currentCommands.find((c) => c.name === updatedCommand.name);
let modified = false;
if (previousCommand.description !== newCommand.description) modified = true;
if (!Discord.ApplicationCommand.optionsEqual(previousCommand.options ?? [], newCommand.options ?? [])) modified = true;
if (modified) {
await previousCommand.edit(newCommand);
updatedCommandCount++;
}
}
log(`Updated ${updatedCommandCount} commands!`);
log(`Commands synchronized!`);
return {
currentCommandCount: currentCommands.size,
newCommandCount: newCommands.length,
deletedCommandCount: deletedCommands.length,
updatedCommandCount
};
};