【问题标题】:Why is Discord API returning error 405 for adding a slash command to a guild?为什么 Discord API 返回错误 405 以向公会添加斜杠命令?
【发布时间】:2022-01-20 18:35:19
【问题描述】:

我目前正在 discord.js 中制作一个机器人,并且我正在尝试构建一个命令处理程序(专门用于斜杠命令)。不幸的是,每当我运行代码时,它都会从 discord API 返回 405 错误。这是错误:

DiscordAPIError[0]: 405: Method Not Allowed
    at SequentialHandler.runRequest (/home/ayman/Documents/Projects/image-bot/node_modules/@discordjs/rest/dist/lib/handlers/SequentialHandler.js:198:23)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (/home/ayman/Documents/Projects/image-bot/node_modules/@discordjs/rest/dist/lib/handlers/SequentialHandler.js:99:20)
    at async /home/ayman/Documents/Projects/image-bot/src/main.js:35:5 {
  rawError: { message: '405: Method Not Allowed', code: 0 },
  code: 0,
  status: 405,
  method: 'put',
  url: 'https://discord.com/api/v9/applications/921468165576347658/guilds/545334770368774146/commands/undefined'
}

这是有问题的代码:

(async () => {
    try {
        if (process.env.ENV === "production") {
            await rest.put(Routes.applicationCommand(CLIENT_ID), {
                body: commands,
            });
            console.log("Commands successfully registered globally.");
        } else {
            await rest.put(Routes.applicationGuildCommand(CLIENT_ID, process.env.GUILD_ID), {
                body: commands,
            });
            console.log("Commands successfully registered locally.");
        }
    } catch (err) {
        if (err) console.error(err);
    }
})();

发生这种情况有什么原因吗?感谢您的帮助!

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    你用错了方法,是applicationGuildCommands :)

    参考source code

    【讨论】:

      猜你喜欢
      • 2022-06-13
      • 2020-05-17
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多