Slash command setting not working

Trying to set slash commands for my bot and I'm not getting any errors, but my slash commands are not being updated properly. I'm using DJS 14.14.1. Am I doing something wrong?
require('dotenv').config();
const fs = require('fs');
const { Client, GatewayIntentBits, PermissionsBitField } = require('discord.js');
const { Logger, Level } = require('./util/logger.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// Initialize commands
const commands = new Map();

// Builds the list of commands to set
const commandList = [];

const commandPaths = fs.readdirSync('./commands');
for(const commandPath of commandPaths) {
const command = require(`./commands/${ commandPath }`);
commandList.push({
name: command.name,
description: command.description,
options: command.options,
defaultMemberPermissions: PermissionsBitField.Default
});
commands.set(command.name, command.obj);
Logger.log(Level.INFO, `Command initialized successfully: ${ command.name }`);
}

client.on('ready', async () => {
// Set the commands for the bot
console.log(await client.application.commands.set(commandList));
Logger.log(Level.INFO, 'Commands successfully sent to Discord.');

Logger.log(Level.INFO, 'Bot started successfully!');
});
require('dotenv').config();
const fs = require('fs');
const { Client, GatewayIntentBits, PermissionsBitField } = require('discord.js');
const { Logger, Level } = require('./util/logger.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// Initialize commands
const commands = new Map();

// Builds the list of commands to set
const commandList = [];

const commandPaths = fs.readdirSync('./commands');
for(const commandPath of commandPaths) {
const command = require(`./commands/${ commandPath }`);
commandList.push({
name: command.name,
description: command.description,
options: command.options,
defaultMemberPermissions: PermissionsBitField.Default
});
commands.set(command.name, command.obj);
Logger.log(Level.INFO, `Command initialized successfully: ${ command.name }`);
}

client.on('ready', async () => {
// Set the commands for the bot
console.log(await client.application.commands.set(commandList));
Logger.log(Level.INFO, 'Commands successfully sent to Discord.');

Logger.log(Level.INFO, 'Bot started successfully!');
});
12 Replies
d.js toolkit
d.js toolkit10mo 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!
d.js docs
d.js docs10mo ago
:guide: Creating Your Bot: Registering slash commands - Command registration > Global commands read more
SpecialSauce
SpecialSauce10mo ago
Registering commands on ready isn’t recommended. You only need to deploy commands if you change their signature
Ryan
RyanOP10mo ago
I tried this before too, but I didn't get it work And is the client ID my bot's application ID?
SpecialSauce
SpecialSauce10mo ago
Yep
Ryan
RyanOP10mo ago
One second. Let me change my code back to register before. Also, thanks for the help! :)
SpecialSauce
SpecialSauce10mo ago
Global commands should be available in dms be default
Ryan
RyanOP10mo ago
Should it register in guilds/dms right away? Or do I have to wait a certain time?
require('dotenv').config();
const fs = require('fs');
const { Client, GatewayIntentBits, PermissionsBitField, REST, Routes } = require('discord.js');
const { Logger, Level } = require('./util/logger.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// Initialize commands
const commands = new Map();

// Builds the list of commands to set
const commandList = [];

const commandPaths = fs.readdirSync('./commands');
for(const commandPath of commandPaths) {
const command = require(`./commands/${ commandPath }`);
commandList.push({
name: command.name,
description: command.description,
options: command.options
//defaultMemberPermissions: PermissionsBitField.Default
});
commands.set(command.name, command.obj);
Logger.log(Level.INFO, `Command initialized successfully: ${ command.name }`);
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(process.env.DISCORD_TOKEN);

// and deploy your commands!
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationCommands(process.env.DISCORD_APP_ID),
{ body: commandList },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();

client.on('ready', async () => {
// Set the commands for the bot
//console.log(await client.application.commands.set(commandList));
//Logger.log(Level.INFO, 'Commands successfully sent to Discord.');

Logger.log(Level.INFO, 'Bot started successfully!');
});
require('dotenv').config();
const fs = require('fs');
const { Client, GatewayIntentBits, PermissionsBitField, REST, Routes } = require('discord.js');
const { Logger, Level } = require('./util/logger.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// Initialize commands
const commands = new Map();

// Builds the list of commands to set
const commandList = [];

const commandPaths = fs.readdirSync('./commands');
for(const commandPath of commandPaths) {
const command = require(`./commands/${ commandPath }`);
commandList.push({
name: command.name,
description: command.description,
options: command.options
//defaultMemberPermissions: PermissionsBitField.Default
});
commands.set(command.name, command.obj);
Logger.log(Level.INFO, `Command initialized successfully: ${ command.name }`);
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(process.env.DISCORD_TOKEN);

// and deploy your commands!
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationCommands(process.env.DISCORD_APP_ID),
{ body: commandList },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();

client.on('ready', async () => {
// Set the commands for the bot
//console.log(await client.application.commands.set(commandList));
//Logger.log(Level.INFO, 'Commands successfully sent to Discord.');

Logger.log(Level.INFO, 'Bot started successfully!');
});
That's the new code. I removed the permissions as well. Should I keep it in?
Ryan
RyanOP10mo ago
It hasn't updated though: https://fyi.cx/MYPqGbG.png
Ryan
RyanOP10mo ago
What I was using to previously register commands worked, but when I updated to 14.14.1, it busted (that screenshot is in a server btw, not DMs) And I clicked off and back on Ahh. In a last-ditch effort, I just CTRL+R to refresh discord and I see the updated commands. I have to refresh every time to see the updates? I guess I'm just stupid then lmao
SpecialSauce
SpecialSauce10mo ago
Yeah sometimes you need to refresh the client to reload commands. The portion that registers commands should be in a separate file so you’re not hitting the command endpoint every time you start the bot, you’re likely to hit a rate limit while testing.
Want results from more Discord servers?
Add your server