-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdeploy-commands.js
More file actions
59 lines (51 loc) · 1.91 KB
/
Copy pathdeploy-commands.js
File metadata and controls
59 lines (51 loc) · 1.91 KB
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
const fs = require('node:fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
// Read environment variables
const config = require('./env-var');
const clientId = config.getConfig().clientId;
const guildId = config.getConfig().guildId;
const token = config.getConfig().token;
// Get arguments from command input to determine environment state
const args = process.argv.slice(2);
const env_state = args[0];
const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}
// Read development commands from commands-dev/ folder
if (env_state === 'dev') {
const commandFilesDev = fs.readdirSync('./commands-dev').filter(file => file.endsWith('.js'));
for (const file of commandFilesDev) {
const command = require(`./commands-dev/${file}`);
commands.push(command.data.toJSON());
}
}
const numCommands = commands.length;
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log(`Started refreshing ${env_state} application (/) commands with ${numCommands} command(s).`);
if (env_state === 'dev') {
// Deploy commands to the development guild immediately
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log(`Successfully reloaded ${numCommands} application (/) command(s) to guild ${guildId}.`);
} else if (env_state === 'prod') {
// Deploy commands globally to production
await rest.put(
Routes.applicationCommands(clientId),
{ body: commands },
);
console.log(`Successfully reloaded ${numCommands} application (/) command(s) globally.`);
} else {
throw new Error(`Invalid environment state: ${env_state}. Must be 'dev' or 'prod'.`);
}
} catch (error) {
console.error(error);
}
})();