【问题标题】:Guild.commands is undefined in discord.js v12.5.3Guild.commands 在 discord.js v12.5.3 中未定义
【发布时间】:2021-10-30 05:41:06
【问题描述】:

所以我正在使用 discord.js 版本 12.5.3 来重建我不久前做的音乐机器人。我正在尝试使用斜杠命令,但是当我使用 guild.commands.set([commands]) 时,它说 guild.commands 未定义。这是我的那部分代码。

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);
    });
});

【问题讨论】:

  • Guild.commands 在 v12 中不存在
  • @theusaf 那么为什么我会在 VS Code 中获得 Intellisense 以及如何在 v12 中使用斜杠命令呢?它也在文档中
  • 非常感谢您的工作!但是您也知道如何删除斜杠命令吗?
  • 没有。但我怀疑你必须以某种方式使用.delete 而不是.post

标签: javascript node.js discord.js


【解决方案1】:

Guild.commands 仅在 v13 中引入。在 v12 中,创建斜杠命令的方式是使用commands.post()

This answer may clarify a little

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

以上为发布公会指令(限1个公会)

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

以上是发布一个全局命令(每个公会都可以使用)

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2021-06-20
    • 2021-06-28
    • 2020-12-14
    • 2022-08-21
    相关资源
    最近更新 更多