Skip to content

Commit 56b5db9

Browse files
committed
update some usage
commands deploying files seems to be outdated on api usage
1 parent f0cc543 commit 56b5db9

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

deploy.js

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,57 @@
11
const fs = require("node:fs");
22
const path = require("node:path");
3-
const { REST } = require("@discordjs/rest");
4-
const { Routes } = require("discord.js");
3+
const { REST, Routes } = require("discord.js");
54
require("dotenv").config();
65

76
const commands = [];
87
const commandsPath = path.join(__dirname, "commands");
9-
const commandFiles = fs
10-
.readdirSync("./commands")
11-
.filter((file) => file.endsWith(".js"));
8+
const commandFiles = fs.readdirSync("./commands").filter((file) => file.endsWith(".js"));
129

1310
const clientId = process.env.CLIENTID;
1411
const guildId = process.env.GUILDID;
1512

1613
for (const file of commandFiles) {
1714
const filePath = path.join(commandsPath, file);
18-
const command = require(filePath);
19-
commands.push(command.data.toJSON());
15+
const command = require(filePath);
16+
if ("data" in command && "execute" in command) {
17+
commands.push(command.data.toJSON());
18+
} else {
19+
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
20+
}
2021
}
2122

22-
const rest = new REST({ version: "10" }).setToken(process.env.DISCORD);
23+
// This file will deploy both guild and global commands by default.
24+
25+
// Construct and prepare an instance of the REST module
26+
const rest = new REST().setToken(process.env.DISCORD);
2327

28+
// Deploy guild commands
2429
(async () => {
2530
try {
26-
console.log("Started reloading guild application (/) commands.");
31+
console.log(`Started reloading ${commands.length} guild application (/) commands.`);
2732

28-
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
29-
body: commands,
30-
});
33+
const data = await rest.put(
34+
Routes.applicationGuildCommands(clientId, guildId),
35+
{ body: commands },
36+
);
3137

32-
console.log("Successfully reloaded guild application (/) commands.");
38+
console.log(`Successfully reloaded ${data.length} guild application (/) commands.`);
3339
} catch (error) {
3440
console.error(error);
3541
}
3642
})();
3743

44+
// Deploy global commands
3845
(async () => {
3946
try {
40-
console.log("Started reloading global application (/) commands.");
47+
console.log(`Started reloading ${commands.length} global application (/) commands.`);
4148

42-
await rest.put(Routes.applicationCommands(clientId), { body: commands });
49+
const data = await rest.put(
50+
Routes.applicationCommands(clientId, guildId),
51+
{ body: commands },
52+
);
4353

44-
console.log("Successfully reloaded global application (/) commands.");
54+
console.log(`Successfully reloaded ${data.length} global application (/) commands.`);
4555
} catch (error) {
4656
console.error(error);
4757
}

undeploy.js

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
const fs = require("node:fs");
2-
const { REST } = require("@discordjs/rest");
3-
const { Routes } = require("discord-api-types/v9");
2+
const { REST, Routes } = require("discord.js");
43
require("dotenv").config();
54

65
const commands = [];
7-
const commandFiles = fs
8-
.readdirSync("./commands")
9-
.filter((file) => file.endsWith(".js"));
6+
const commandFiles = fs.readdirSync("./commands").filter((file) => file.endsWith(".js"));
107

118
const clientId = process.env.CLIENTID;
129
const guildId = process.env.GUILDID;
@@ -16,27 +13,22 @@ for (const file of commandFiles) {
1613
commands.push(command.data.toJSON());
1714
}
1815

19-
const rest = new REST({ version: "9" }).setToken(process.env.DISCORD);
16+
const rest = new REST().setToken(process.env.DISCORD);
2017

2118
// This will only remove guild commands, since guild commands can create duplicated commands in the specified guild.
2219

2320
(async () => {
2421
try {
2522
console.log("Started removing guild application (/) commands.");
2623

27-
await rest
28-
.get(Routes.applicationGuildCommands(clientId, guildId))
29-
.then((data) => {
30-
const promises = [];
31-
for (const command of data) {
32-
const deleteUrl = `${Routes.applicationGuildCommands(
33-
clientId,
34-
guildId
35-
)}/${command.id}`;
36-
promises.push(rest.delete(deleteUrl));
37-
}
38-
return Promise.all(promises);
39-
});
24+
await rest.get(Routes.applicationGuildCommands(clientId, guildId)).then((data) => {
25+
const promises = [];
26+
for (const command of data) {
27+
const deleteUrl = `${Routes.applicationGuildCommands(clientId, guildId)}/${command.id}`;
28+
promises.push(rest.delete(deleteUrl));
29+
}
30+
return Promise.all(promises);
31+
});
4032

4133
console.log("Successfully removed guild application (/) commands.");
4234
} catch (error) {

0 commit comments

Comments
 (0)