|
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; |
5 | 7 |
|
6 | 8 | const commands = []; |
7 | 9 | // 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"); |
9 | 11 | const commandFolders = fs.readdirSync(foldersPath); |
10 | 12 |
|
11 | 13 | 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 | + } |
26 | 31 | } |
27 | 32 |
|
28 | 33 | // 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); |
30 | 35 |
|
31 | 36 | // and deploy your commands! |
32 | 37 | (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.`)); |
35 | 54 |
|
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 | + } |
49 | 65 | })(); |
0 commit comments