【问题标题】:discord.js slash command api loading errordiscord.js 斜线命令 api 加载错误
【发布时间】:2022-01-14 04:58:13
【问题描述】:

我正在尝试为我的机器人创建一个/config <subcommand> 系统,但它给我一个斜杠命令体错误。 这是我当前的命令主体

          name: 'config',
          description: "The config system for the bot",
          userPermissions: ['MANAGE_GUILD'],
          options: [
              {
                  name: 'prefix',
                  description: "Set a different prefifor the server",
                  type: "SUB_COMMAND",
                  options: [
                      {name: "new-prefix", description: "The new prefix that you want to set", type: "STRING", required: true}
                  ]
              }
          ]

错误是

DiscordAPIError[50035]: Invalid Form Body
0.options[0].options[0].type[NUMBER_TYPE_COERCE]: Value "STRING" is not int.
0.options[0].type[NUMBER_TYPE_COERCE]: Value "SUB_COMMAND" is not int.

我从来没有遇到过这个错误

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    当您使用原始 json 来注册您的命令时,您的选项类型必须是整数,而不是字符串。
    为什么?因为 Discord API 接受整数,而不是字符串。
    每种类型都有一个关联的 id 整数,如文档中所述 here,这是您必须使用的,而不是字符串。
    在您的情况下,type: "SUB_COMMAND" 应该是 type: 1

    为什么选项类型在 discord.js 文档中记录为字符串?因为他们把它变成了字符串来提高代码的可读性。在请求 API 之前,lib 确实将这些字符串“转换”为 int。但在这种情况下,您使用的是原始 json。

    您可以在Discord Developer Documentation 中检查其他应用程序命令选项类型

    【讨论】:

    • 为什么要放两次文档链接?
    • 您好,抱歉,在您回答此问题之前,我已经修复了错误。不过谢谢你的回答!
    猜你喜欢
    • 2021-06-11
    • 2022-01-25
    • 2021-11-17
    • 2021-08-17
    • 2022-01-15
    • 2021-11-03
    • 2021-11-12
    • 2021-08-28
    • 2021-09-12
    相关资源
    最近更新 更多