Skip to content
Advertisement

Guild.commands is undefined in discord.js v12.5.3

So I’m using discord.js version 12.5.3 to rebuild a music bot I did a while back. I’m trying to use slash commands but when I use guild.commands.set([commands]) it says that guild.commands is undefined. Here’s that part of my code.

async function loadCommands(guild) {
    try {
        const commands = Array.from(client.commands).map(([name, command]) => {
            let optionsArr =
                command?.usage
                    ?.replaceAll(/[>|]]/g, " ")
                    .split(/ +/g)
                    .filter((option) => option !== "") || [];

            return {
                name,
                description: command.description,
                options: optionsArr.map((option) => {
                    let required = option.substring(1, option.length) === "<";
                    return {
                        name: option.substring(1, option.length),
                        type: "STRING",
                        description: option.substring(1, option.length),
                        required,
                    };
                }),
            };
        });
        await guild.commands.set(commands);
    } catch (e) {
        return e;
    }
}

client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}`);

    client.guilds.cache.forEach(async (guild) => {
        await loadCommands(guild);
    });
});

Advertisement

Answer

Guild.commands was only introduced in v13. In v12, the way to create slash commands is using commands.post().

This answer may clarify a little

client.api.applications(client.user.id).guilds("GUILD ID HERE").commands.post({/*slash cmd data*/})

The above is to post a guild command (limited to 1 guild)

client.api.applications(client.user.id).commands.post({*/slash cmd data*/})

The above is to post a global command (can be used in every guild)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement