-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy-commands.js
131 lines (103 loc) · 3.89 KB
/
deploy-commands.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const fs = require('fs')
const { REST } = require('@discordjs/rest')
const { Routes } = require('discord-api-types/v9')
const { TOKEN } = require('robo-bot-utils')
const { SERVER_ID, CLIENT_ID } = require('./bot/ids')
const commands = []
const commandFiles = fs.readdirSync('./bot/commands').filter(file => file.endsWith('.js'))
for (const file of commandFiles) {
const command = require(`./bot/commands/${file}`)
if(command.alias) {
commands.push(command.data.toJSON())
let ogName = command.data.name
for (let a of command.alias) {
command.data.setName(a)
command.data.setDescription(`${command.data.description} - alias of /${ogName}`)
commands.push(command.data.toJSON())
}
} else {
commands.push(command.data.toJSON())
}
}
const rest = new REST({ version: '9' }).setToken(TOKEN);
/*** Vanilla CMD Update ***/
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, SERVER_ID),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
/*** CHUNK REGISTRATION ***/
/* async function registerCommands() {
try {
console.log('Started refreshing application (/) commands.')
const BATCH_SIZE = 10;
const total_batches = Math.ceil(commands.length / BATCH_SIZE);
for (let i = 0; i < commands.length; i += BATCH_SIZE) {
const batch = commands.slice(i, i + BATCH_SIZE);
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, SERVER_ID),
{ body: batch },
);
console.log(`Registered batch ${i / BATCH_SIZE + 1}/${total_batches}`);
}
console.log('Successfully reloaded application (/) commands.')
} catch (error) {
console.error('Error refreshing commands:', error)
}
}
registerCommands()
*/
/*** DIFF REGISTRATION ***/
/* const getExistingCommands = () => rest.get(Routes.applicationGuildCommands(CLIENT_ID, SERVER_ID)).catch(() => []);
const commandsEqual = (cmd1, cmd2) => JSON.stringify(cmd1) === JSON.stringify(cmd2);
async function updateCommands() {
try {
const existingCommands = await getExistingCommands();
console.log(`Existing Total: ${existingCommands.length}, New Total: ${commands.length}`);
const [toUpdate, toCreate] = commands.reduce((acc, newCmd) => {
const existingCmd = existingCommands.find(cmd => cmd.name === newCmd.name);
if (existingCmd && !commandsEqual(newCmd, existingCmd)) {
// Update Existing
acc[0].push({ ...newCmd, id: existingCmd.id });
} else if (!existingCmd) {
// Create New
acc[1].push(newCmd);
}
return acc;
}, [[], []]);
console.log(`To update: ${toUpdate.length}, To create: ${toCreate.length}`);
if (toCreate.length) {
console.log('Creating New CMDs.');
try {
rest.put(Routes.applicationGuildCommands(CLIENT_ID, SERVER_ID), { body: toCreate })
.then(() => console.log('slash commands registered'))
.catch(console.error);
} catch (err) {
console.error(err)
}
console.log('New CMDs created successfully.');
}
if (toUpdate.length) {
console.log('Updating Old CMDs.');
await Promise.all(toUpdate.map(cmd =>{
return rest.patch(Routes.applicationGuildCommand(CLIENT_ID, SERVER_ID, cmd.id), { body: cmd })
}));
console.log('Old CMDs updated successfully.');
}
console.log('Command update completed.');
} catch (error) {
console.error('Error updating commands:', error);
}
}
// Use this if you're running this script directly
updateCommands().then(() => console.log('All operations completed.'));
*/