slash command deploy

I think the function for deploying slash commands is not working properly. I want to know what the issue is.
14 Replies
d.js toolkit
d.js toolkit5d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
변상훈
변상훈OP5d ago
asd
treble/luna
treble/luna5d ago
* show your code
변상훈
변상훈OP5d ago
const { REST, Routes } = require('discord.js');
const dotenv = require('dotenv');
const fs = require('node:fs');
const path = require('node:path');

// Load .env file
dotenv.config();

/**
* Function to refresh (reload) all Discord Slash commands
* - Clears the require cache and reloads files to reflect code changes
*/
async function reloadCommands() {
// Array to store the commands
const commands = [];

// Set the path for the Commands folder
const foldersPath = path.join(__dirname, 'Commands');
const entries = fs.readdirSync(foldersPath, { withFileTypes: true });

// Iterate through each file in the folder and load commands
for (const entry of entries) {
const entryPath = path.join(foldersPath, entry.name);

// Only target JavaScript files
if (entry.isFile() && entry.name.endsWith('.js')) {
// (1) If the file is already loaded, clear its cache
delete require.cache[require.resolve(entryPath)];

// (2) Re-require the file to load the latest code
const command = require(entryPath);

// (3) Check if the required properties (data, execute, upload) are present
if ('data' in command && 'execute' in command && command.upload) {
commands.push(command.data.toJSON());
} else {
console.log(
`[WARNING] The command at ${entryPath} is missing a required "data" or "execute" property.`
);
}
}
}
const { REST, Routes } = require('discord.js');
const dotenv = require('dotenv');
const fs = require('node:fs');
const path = require('node:path');

// Load .env file
dotenv.config();

/**
* Function to refresh (reload) all Discord Slash commands
* - Clears the require cache and reloads files to reflect code changes
*/
async function reloadCommands() {
// Array to store the commands
const commands = [];

// Set the path for the Commands folder
const foldersPath = path.join(__dirname, 'Commands');
const entries = fs.readdirSync(foldersPath, { withFileTypes: true });

// Iterate through each file in the folder and load commands
for (const entry of entries) {
const entryPath = path.join(foldersPath, entry.name);

// Only target JavaScript files
if (entry.isFile() && entry.name.endsWith('.js')) {
// (1) If the file is already loaded, clear its cache
delete require.cache[require.resolve(entryPath)];

// (2) Re-require the file to load the latest code
const command = require(entryPath);

// (3) Check if the required properties (data, execute, upload) are present
if ('data' in command && 'execute' in command && command.upload) {
commands.push(command.data.toJSON());
} else {
console.log(
`[WARNING] The command at ${entryPath} is missing a required "data" or "execute" property.`
);
}
}
}
// Create a REST instance for API interaction (set token)
const rest = new REST().setToken(process.env.DISCORD_TOKEN);

try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

// Use Discord API to overwrite (register) application commands
const data = await rest.put(
Routes.applicationCommands(process.env.APP_ID),
{ body: commands }
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
}

// Export the function
module.exports = reloadCommands;
// Create a REST instance for API interaction (set token)
const rest = new REST().setToken(process.env.DISCORD_TOKEN);

try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

// Use Discord API to overwrite (register) application commands
const data = await rest.put(
Routes.applicationCommands(process.env.APP_ID),
{ body: commands }
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
}

// Export the function
module.exports = reloadCommands;
it was too long...
treble/luna
treble/luna5d ago
and what part about it does not work It also should not be exported Its supposed to be a single file you run once, not every time your bot starts
변상훈
변상훈OP5d ago
The logs indicate that the reload was successful, but in the guild, the new slash commands are not updated, and the existing commands are not refreshed.
treble/luna
treble/luna5d ago
first of all did you reload your client
변상훈
변상훈OP5d ago
it is before i uesed
변상훈
변상훈OP5d ago
What does it mean to reload the client? Are you asking if the bot has been restarted?
treble/luna
treble/luna5d ago
Your discord client
변상훈
변상훈OP5d ago
If you're asking whether I restarted main.js, yes, I did restart it.
treble/luna
treble/luna5d ago
No Your discord client The one you are using to send messages here
변상훈
변상훈OP5d ago
Ah, got it. I'll try it and get back to you. Everything is working perfectly, my friend. Thank you so much, bro! Your help shines brighter than a thousand suns. I truly appreciate it from the bottom of my heart—you’re a legend among legends!
treble/luna
treble/luna5d ago
You're welcome! Glad it works!

Did you find this page helpful?