【问题标题】:DiscordAPIError: Invalid Form Body options[2]: Required options must be placed before non-required optionsDiscordAPIError:无效的表单正文选项[2]:必需选项必须放在非必需选项之前
【发布时间】:2022-01-26 20:21:06
【问题描述】:

好吧,伙计们,在我发布第一个问题并没有得到答案之后,我几乎在整个网络上冲浪,格式化了代码,但仍然找不到修复。

当我使用commands.set() 方法注册大约 13-14 个斜杠命令时,我不断收到此错误:

      throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Invalid Form Body
options[2]: Required options must be placed before non-required options
    at RequestHandler.execute (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
    at async GuildApplicationCommandManager.set (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\managers\ApplicationCommandManager.js:146:18) {
  method: 'put',
  path: '/applications/922152514420363285/guilds/933052184432636014/commands',
  code: 50035,
  httpStatus: 400,

这是我注册这些命令的ready.js文件的源代码:

const { Constants } = require("discord.js")
const {promisify} = require('util')
const {glob} = require('glob')

const globPromise = promisify(glob);

//const slashregister = require('../../slashregistering')
module.exports = async(Discord , client) => {
    console.log('Bot is ready')
    const guild = client.guilds.cache.get('933052184432636014')
    const commands = guild.commands
    var arrayofCommands = new Array()
    var commands1 = [
        {
            name : 'add',
            description : 'Adds two numbers.',
            options : [{
                name : 'num1',
                description : 'Number1',
                required : true,
                type : Constants.ApplicationCommandOptionTypes.INTEGER
            },
            {
                name : 'num2',
                description : 'Number2',
                required : true,
                type : Constants.ApplicationCommandOptionTypes.INTEGER
            }
        ]
        }
    ]
   
    const slashcommands = await globPromise(`${process.cwd()}/slashcommands/**/*.js`)
    slashcommands.map((file) => {
        const value = require(file)
        //delete value.execute
        console.log(value)
        
        arrayofCommands.push(value)
    })
     commands.set(
        arrayofCommands
     )
}

但是,代码应该可以工作,因为“文件”变量包含所有必填字段:namedescriptiontypedefaultPermissionoptions

所以我输出了对象file,这是它的迭代之一:

令人惊讶的是,commands1 数组虽然具有与arrayOfCommands 数组相同的属性,但仍然有效!那么有人可以帮帮我吗?

这是完整的错误:

Server is online!
Bot is ready
C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:350
      throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Invalid Form Body
options[2]: Required options must be placed before non-required options
    at RequestHandler.execute (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
    at async GuildApplicationCommandManager.set (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\managers\ApplicationCommandManager.js:146:18) {
  method: 'put',
  path: '/applications/922152514420363285/guilds/933052184432636014/commands',
  code: 50035,
  httpStatus: 400,
  requestData: {
    json: [
      {
        name: 'addrole',
        description: 'Gives the member a role.',
        type: 1,
        options: [
          {
            type: 8,
            name: 'role',
            description: 'Role Name',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 6,
            name: 'member',
            description: 'Member ',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'ban',
        description: 'Ban the specified member.',
        type: 1,
        options: [
          {
            type: 6,
            name: 'member',
            description: 'Member ',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 3,
            name: 'reason',
            description: 'Reason ',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'changenick',
        description: 'Changes the nickname of a server member.',
        type: 1,
        options: [
          {
            type: 6,
            name: 'member',
            description: 'Member',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 3,
            name: 'nickname',
            description: 'Nickname',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'kick',
        description: 'Kicks the specified member.',
        type: 1,
        options: [
          {
            type: 6,
            name: 'member',
            description: 'Member',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 3,
            name: 'reason',
            description: 'Reason',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'lock',
        description: 'Locks the specified channel for the specified role.',
        type: 1,
        options: [
          {
            type: 7,
            name: 'channel',
            description: 'Channel',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 8,
            name: 'role',
            description: 'Role ',
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'lockdown',
        description: 'Enforces a complete lockdown on the server.',
        type: 1,
        options: [
          {
            type: 3,
            name: 'toggle',
            description: 'on/off',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 8,
            name: 'role',
            description: 'Role ',
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'removerole',
        description: 'Removes role from a member.',
        type: 1,
        options: [
          {
            type: 6,
            name: 'member',
            description: 'Member ',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 8,
            name: 'role',
            description: 'Role ',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'timeout',
        description: 'Gives a member a timeout.',
        type: 1,
        options: [
          {
            type: 6,
            name: 'member',
            description: 'Member ',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 3,
            name: 'reason',
            description: 'Reason',
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 3,
            name: 'time',
            description: 'Duration',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'unlock',
        description: 'Locks the specified channel for the specified role.',
        type: 1,
        options: [
          {
            type: 7,
            name: 'channel',
            description: 'Channel',
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 8,
            name: 'role',
            description: 'Role',
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'embedcreate',
        description: 'Creates an embed.',
        type: 1,
        options: [
          {
            type: 7,
            name: 'channel',
            description: 'Channel ',
            required: false,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 3,
            name: 'color',
            description: 'Hex-Code',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 3,
            name: 'title',
            description: 'Title',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'ping',
        description: 'Responds with a pong!',
        type: 1,
        options: undefined,
        default_permission: false
      },
      {
        name: 'setactivity',
        description: 'Sets the Activity of the bot.',
        type: 1,
        options: [
          {
            type: 3,
            name: 'activity',
            description: 'Activity',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          },
          {
            type: 3,
            name: 'type',
            description: 'Type ',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'setavatar',
        description: "Sets the bot's avatar.",
        type: 1,
        options: [
          {
            type: 3,
            name: 'link',
            description: 'Image Link',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      },
      {
        name: 'setstatus',
        description: 'Sets the status of the bot.',
        type: 1,
        options: [
          {
            type: 3,
            name: 'status',
            description: 'Status ',
            required: true,
            autocomplete: undefined,
            choices: undefined,
            options: undefined,
            channel_types: undefined,
            min_value: undefined,
            max_value: undefined
          }
        ],
        default_permission: false
      }
    ],
    files: []
  }
}
[nodemon] app crashed - waiting for file changes before starting...

【问题讨论】:

  • 我认为错误消息和记录的文件不匹配。错误是关于一个(至少)三个选项的命令,而你的 lock 命令只有两个。
  • @ZsoltMeszaros 哦,好吧,但只有名称和描述是强制性的 pastebin.pl/view/48cff071 这是完整错误代码的链接.....

标签: javascript node.js discord.js


【解决方案1】:

该错误试图告诉您的是,在非必需选项之后您不能拥有必需选项。在您的requestData 中,您可以看到所有options 数组。查看您的timeout 命令:

{
  name: 'timeout',
  description: 'Gives a member a timeout.',
  type: 1,
  options: [
    {
      type: 6,
      name: 'member',
      description: 'Member ',
      required: true,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    },
    {
      type: 3,
      name: 'reason',
      description: 'Reason',
      required: false,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    },
    {
      type: 3,
      name: 'time',
      description: 'Duration',
      required: true,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    }
  ],
  default_permission: false
}

你能看到第一个选项(member)是必需的,第二个(reason)不是必需的,最后一个(time)又是必需的吗?

应该这样排序;首先是两个必填字段,最后是非必填字段:

{
  name: 'timeout',
  description: 'Gives a member a timeout.',
  type: 1,
  options: [
    {
      type: 6,
      name: 'member',
      description: 'Member',
      required: true,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    },
    {
      type: 3,
      name: 'time',
      description: 'Duration',
      required: true,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    },
    {
      type: 3,
      name: 'reason',
      description: 'Reason',
      required: false,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    }
  ],
  default_permission: false
}

另外,您的embedcreate 命令的顺序错误。在那里也更改选项的顺序:

{
  name: 'embedcreate',
  description: 'Creates an embed.',
  type: 1,
  options: [
    {
      type: 3,
      name: 'color',
      description: 'Hex-Code',
      required: true,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    },
    {
      type: 3,
      name: 'title',
      description: 'Title',
      required: true,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    },
    {
      type: 7,
      name: 'channel',
      description: 'Channel ',
      required: false,
      autocomplete: undefined,
      choices: undefined,
      options: undefined,
      channel_types: undefined,
      min_value: undefined,
      max_value: undefined
    }
  ],
  default_permission: false
}

【讨论】:

  • 非常感谢您的帮助!它起作用了:))我之前没有注意到错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多