#Register slash commands

1 messages · Page 1 of 1 (latest)

cosmic flower
#

Hello, I wanted to know what is the best way to register slash commands?

Via the Rest API directly as in the guide?
https://discordjs.guide/interactions/slash-commands.html#guild-commands

Directly with the DiscordJS API?

public async registerCommands() : Promise<void> {
    // Get command data :
    const commands = this.commands.map(command => command.slashCommand.setDefaultPermission(false).toJSON());

    // Get command manager :
    if(!client.application?.owner) await client.application?.fetch();

    const commandManager = (await client.guilds.fetch(guildId)).commands;

    // Set commands and permissions :
    const commandsInstances = await commandManager.set(commands);

    for(const command of commandsInstances.values()){
        const permissions = this.commands.get(command.name)?.permissions;

        if(permissions) command.permissions.set({ permissions: permissions });
    }

    Logger.info("Successfully registered application commands");
}

Or another solution?

weak helm
#

I use the discordJS API to do it, I create an array of commands that extends the class of the first image with a run method. Then, you have to register the commands, you can use the guild.commands.set(array) or client.application.commands.set(array) (The last may take up to an hour to register). To execute the commands you can do client.on('interactionCreate', callback) with the callback function of the third image

#

client.commands is my array of commands