#Application commands are not updated in chat, but in "Manage Integration" are up-to-date.

26 messages · Page 1 of 1 (latest)

civic schooner
#

Here is my code that updates commands:
When the bot starts to run, it loads all commands

client.commands = new Map();
const commandPath = './commands';
const commandFiles = fs.readdirSync(commandPath).filter((file) => file.endsWith('.mjs'));
for (const file of commandFiles) {
    const filePath = './' + path.join(commandPath, file);
    let command = await import(filePath);
    command = command.default;

    if (command.ignore) continue;
    if (command.data && command.execute) {
        client.commands.set(command.data.name, command);
    } else {
        console.error(`Error: ${filePath} command is missing a required property "data" or "execute"`);
    }
}

After the bot is ready, it sets client.application.commands

import { Client, Events } from 'discord.js';

export default {
    once: true,
    name: Events.ClientReady,

    /**
     * execute
     * @param {Client} client 
     */
    async execute(client) {
        console.info('Client is Ready');

        // load commands
        const commands = Array.from(client.commands.values()).map(command => command.data.toJSON());
        client.application.commands.set(commands)
            .catch(console.error);
        
        const commandNames = commands.map(command => command.name);
        console.info(`Commands loaded: ${commandNames.join(', ')}`);
    },
};

Here's an example of my command file:

import { SlashCommandBuilder, CommandInteraction } from 'discord.js';

export default {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Reply with ms'),
    
    /**
     * execute
     * @param {CommandInteraction} interaction 
     */
    async execute(interaction) {
        await interaction.reply(`🏓 Ping! ${interaction.client.ws.ping}ms`);
    },
};

Discord.js Version: 14.7.1
Nodejs Version: 22.2.0

dire urchinBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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!
  • Marked as resolved by OP
civic schooner
#

this is what I mean that commands are not "updated"

foggy silo
#

Did you try refreshing the app?

foggy silo
civic schooner
#

Yes, but when I check "Manage Integration", it does change.

foggy silo
#

"Yes" to that you tried refreshing?

#

ctrl + r

civic schooner
#

You are totally right.

#

So I should refresh the app every time I update....
For users, that will be a hassle, I suppose.
Any other way?

candid sequoia
#

Your users will see them after some time too.. just for you as dev the reload is needed to see them fast after deployment

civic schooner
#

Or should I register commands, and it will change instantly?

#

I guess just refresh my discord
Thank for the response, I will note that.

rare sierra
#

hi guys do you know how to use discord.js

#

please say

thorny cairn
civic schooner
#

Isn't that registering commands?

#

I have no idea about the difference

thorny cairn
#

don't ask on random threads though, at least do it on #djs-help-v14

thorny cairn
civic schooner
#

oh, I see
What if I set guild.commands? will that be the same?

thorny cairn
#

that'd deploy/register your commands to only that guild

#

what I meant was, given commands don't change that regularly, you can try manually deploying them when you know it's needed, not on every start