Skip to content

Commit c3c75c4

Browse files
committed
feat: delete commands on reload
1 parent f2d4a03 commit c3c75c4

File tree

1 file changed

+51
-35
lines changed

1 file changed

+51
-35
lines changed

deploy-commands.js

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,65 @@
1-
const { REST, Routes } = require('discord.js');
2-
require('dotenv').config();
3-
const fs = require('node:fs');
4-
const path = require('node:path');
1+
const { REST, Routes } = require("discord.js");
2+
require("dotenv").config();
3+
const fs = require("node:fs");
4+
const path = require("node:path");
5+
6+
const ENV = process.env;
57

68
const commands = [];
79
// Grab all the command files from the commands directory you created earlier
8-
const foldersPath = path.join(__dirname, 'commands');
10+
const foldersPath = path.join(__dirname, "commands");
911
const commandFolders = fs.readdirSync(foldersPath);
1012

1113
for (const folder of commandFolders) {
12-
// Grab all the command files from the commands directory you created earlier
13-
const commandsPath = path.join(foldersPath, folder);
14-
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
15-
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
16-
for (const file of commandFiles) {
17-
const filePath = path.join(commandsPath, file);
18-
const command = require(filePath);
19-
if ('data' in command && 'execute' in command) {
20-
commands.push(command.data.toJSON());
21-
}
22-
else {
23-
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
24-
}
25-
}
14+
// Grab all the command files from the commands directory you created earlier
15+
const commandsPath = path.join(foldersPath, folder);
16+
const commandFiles = fs
17+
.readdirSync(commandsPath)
18+
.filter((file) => file.endsWith(".js"));
19+
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
20+
for (const file of commandFiles) {
21+
const filePath = path.join(commandsPath, file);
22+
const command = require(filePath);
23+
if ("data" in command && "execute" in command) {
24+
commands.push(command.data.toJSON());
25+
} else {
26+
console.log(
27+
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
28+
);
29+
}
30+
}
2631
}
2732

2833
// Construct and prepare an instance of the REST module
29-
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
34+
const rest = new REST().setToken(ENV.DISCORD_TOKEN);
3035

3136
// and deploy your commands!
3237
(async () => {
33-
try {
34-
console.log(`Started refreshing ${commands.length} application (/) commands.`);
38+
try {
39+
console.log(
40+
`Started refreshing ${commands.length} application (/) commands.`
41+
);
42+
43+
// The put method is used to fully refresh all commands in the guild with the current set
44+
const data = await
45+
rest.put(
46+
// If you want to deploy commands globally, use the following:
47+
// Routes.applicationCommands(ENV.DISCORD_CLIENT_ID),
48+
Routes.applicationGuildCommands(
49+
ENV.DISCORD_CLIENT_ID,
50+
ENV.DISCORD_GUILD_ID
51+
),
52+
{ body: commands }
53+
).then(() => console.log(`Successfully reloaded application (/) commands.`));
3554

36-
// The put method is used to fully refresh all commands in the guild with the current set
37-
const data = await rest.put(
38-
// If you want to deploy commands globally, use the following:
39-
// Routes.applicationCommands(process.env.DISCORD_CLIENT_ID),
40-
Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, process.env.DISCORD_GUILD_ID),
41-
{ body: commands },
42-
);
43-
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
44-
}
45-
catch (error) {
46-
// And of course, make sure you catch and log any errors!
47-
console.error(error);
48-
}
55+
// To delete all commands in a guild, use the following: (body is empty)
56+
// rest
57+
// .put(Routes.applicationGuildCommands(ENV.DISCORD_CLIENT_ID,
58+
// ENV.DISCORD_GUILD_ID), { body: [] })
59+
// .then(() => console.log("Successfully deleted all guild commands."))
60+
// .catch(console.error);
61+
} catch (error) {
62+
// And of course, make sure you catch and log any errors!
63+
console.error(error);
64+
}
4965
})();

0 commit comments

Comments
 (0)