-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-commands.js
55 lines (43 loc) · 1.68 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
// Slash command deployment script, so that the new commands we've added in the commands folder can be shown in the Discord interface.
const { REST, Routes } = require("discord.js");
const { clientId, guildId, token } = require("./config.json");
const fs = require("node:fs");
const path = require("node:path");
// Fetch all commands from the commands folder.
const commands = [];
const foldersPath = path.join(__dirname, "commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set new item in Collection with key as the command name and value as the exported module
// NOTE: EVERY COMMAND MUST HAVE A DATA OR EXECUTE FIELD WITHIN ITS EXPORT MODULE! IF IT DOESN'T, IT WON'T WORK HERE
if ("data" in command && "execute" in command) {
commands.push(command.data.toJSON());
} else {
console.warn(
`The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
}
// New instance of the REST module
const rest = new REST().setToken(token);
// Deploy commands
(async () => {
try {
console.log(`Started refreshing ${commands.length} slash commands.`);
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands }
);
console.log(`Successfully reloaded ${data.length} slash commands.`);
} catch (error) {
console.error(error);
}
})();