|
1 | 1 | const fs = require("node:fs");
|
2 | 2 | const path = require("node:path");
|
3 |
| -const { REST } = require("@discordjs/rest"); |
4 |
| -const { Routes } = require("discord.js"); |
| 3 | +const { REST, Routes } = require("discord.js"); |
5 | 4 | require("dotenv").config();
|
6 | 5 |
|
7 | 6 | const commands = [];
|
8 | 7 | 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")); |
12 | 9 |
|
13 | 10 | const clientId = process.env.CLIENTID;
|
14 | 11 | const guildId = process.env.GUILDID;
|
15 | 12 |
|
16 | 13 | for (const file of commandFiles) {
|
17 | 14 | 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 | + } |
20 | 21 | }
|
21 | 22 |
|
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); |
23 | 27 |
|
| 28 | +// Deploy guild commands |
24 | 29 | (async () => {
|
25 | 30 | try {
|
26 |
| - console.log("Started reloading guild application (/) commands."); |
| 31 | + console.log(`Started reloading ${commands.length} guild application (/) commands.`); |
27 | 32 |
|
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 | + ); |
31 | 37 |
|
32 |
| - console.log("Successfully reloaded guild application (/) commands."); |
| 38 | + console.log(`Successfully reloaded ${data.length} guild application (/) commands.`); |
33 | 39 | } catch (error) {
|
34 | 40 | console.error(error);
|
35 | 41 | }
|
36 | 42 | })();
|
37 | 43 |
|
| 44 | +// Deploy global commands |
38 | 45 | (async () => {
|
39 | 46 | try {
|
40 |
| - console.log("Started reloading global application (/) commands."); |
| 47 | + console.log(`Started reloading ${commands.length} global application (/) commands.`); |
41 | 48 |
|
42 |
| - await rest.put(Routes.applicationCommands(clientId), { body: commands }); |
| 49 | + const data = await rest.put( |
| 50 | + Routes.applicationCommands(clientId, guildId), |
| 51 | + { body: commands }, |
| 52 | + ); |
43 | 53 |
|
44 |
| - console.log("Successfully reloaded global application (/) commands."); |
| 54 | + console.log(`Successfully reloaded ${data.length} global application (/) commands.`); |
45 | 55 | } catch (error) {
|
46 | 56 | console.error(error);
|
47 | 57 | }
|
|
0 commit comments