Discord.js V14 SlashCommand报错50035:必填选项位置问题求助
解决Discord.js V14中Slash Command的DiscordAPIError[50035]错误
问题详情
使用Node.js V19.3.0开发Discord.js V14的announcement斜杠命令时,触发DiscordAPIError[50035],错误提示如下:
options[3][APPLICATION_COMMAND_OPTIONS_REQUIRED_INVALID]: Required options must be placed before non-required options
完整错误栈:
C:\Users\crazy\Codeing\CrazyBot\node_modules@discordjs\rest\dist\index.js:640 throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData); ^ DiscordAPIError[50035]: Invalid Form Body options[3][APPLICATION_COMMAND_OPTIONS_REQUIRED_INVALID]: Required options must be placed before non-required options at handleErrors (C:\Users\crazy\Codeing\CrazyBot\node_modules@discordjs\rest\dist\index.js:640:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async SequentialHandler.runRequest (C:\Users\crazy\Codeing\CrazyBot\node_modules@discordjs\rest\dist\index.js:1021:23) at async SequentialHandler.queueRequest (C:\Users\crazy\Codeing\CrazyBot\node_modules@discordjs\rest\dist\index.js:862:14) at async REST.request (C:\Users\crazy\Codeing\CrazyBot\node_modules@discordjs\rest\dist\index.js:1387:22) at async ApplicationCommandManager.set (C:\Users\crazy\Codeing\CrazyBot\node_modules\discord.js\src\managers\ApplicationCommandManager.js:171:18) { requestBody: { files: undefined, json: [ { name: 'announcement', name_localizations: undefined, description: 'Sends a embed message in a channel.', nsfw: undefined, description_localizations: undefined, type: undefined, options: [ { type: 8, name: 'ping-role', name_localizations: undefined, name_localized: undefined, description: 'Pings the role.', description_localizations: undefined, description_localized: undefined, required: true, autocomplete: undefined, choices: undefined, options: undefined, channel_types: undefined, min_value: undefined, max_value: undefined, min_length: undefined, max_length: undefined }, { type: 7, name: 'channel', name_localizations: undefined, name_localized: undefined, description: 'Channel that the embed message gets sent in.', description_localizations: undefined, description_localized: undefined, required: true, autocomplete: undefined, choices: undefined, options: undefined, channel_types: undefined, min_value: undefined, max_value: undefined, min_length: undefined, max_length: undefined }, { type: 3, name: 'title', name_localizations: undefined, name_localized: undefined, description: 'Title of the embed.', description_localizations: undefined, description_localized: undefined, required: false, autocomplete: undefined, choices: undefined, options: undefined, channel_types: undefined, min_value: undefined, max_value: undefined, min_length: undefined, max_length: undefined }, { type: 3, name: 'description', name_localizations: undefined, name_localized: undefined, description: 'Sets the description of the embed.', description_localizations: undefined, description_localized: undefined, required: true, autocomplete: undefined, choices: undefined, options: undefined, channel_types: undefined, min_value: undefined, max_value: undefined, min_length: undefined, max_length: undefined }, { type: 11, name: 'image', name_localizations: undefined, name_localized: undefined, description: 'Sends a image in the embed.', description_localizations: undefined, description_localized: undefined, required: false, autocomplete: undefined, choices: undefined, options: undefined, channel_types: undefined, min_value: undefined, max_value: undefined, min_length: undefined, max_length: undefined }, { type: 3, name: 'footer', name_localizations: undefined, name_localized: undefined, description: 'Set the footer of the embed', description_localizations: undefined, description_localized: undefined, required: false, autocomplete: undefined, choices: undefined, options: undefined, channel_types: undefined, min_value: undefined, max_value: undefined, min_length: undefined, max_length: undefined } ], default_member_permissions: '8', dm_permission: undefined } ] }, rawError: { code: 50035, errors: { options: { '3': { _errors: [ [Object] ] } } }, message: 'Invalid Form Body' }, code: 50035, status: 400, method: 'PUT', url: 'https://discord.com/api/v10/applications/1123448402450059436/commands' }
原始命令代码:
module.exports = { data: new SlashCommandBuilder() .setName("announcement") .setDescription("Sends a embed message in a channel.") .setDefaultMemberPermissions(PermissionFlagsBits.Administrator) // |#################################| // | Role Ping | // |#################################| .addRoleOption((option) => option .setName("ping-role") .setDescription("Pings the role.") .setRequired(true) ) // |#################################| // | Channel Option | // |#################################| .addChannelOption((option) => option .setName("channel") .setDescription("Channel that the embed message gets sent in.") .setRequired(true) ) // |#################################| // | Embed Options | // |#################################| // ##### Title ##### .addStringOption((option) => option .setName("title") .setDescription("Title of the embed.") .setRequired(false) ) // #### Description #### .addStringOption((option) => option .setName("description") .setDescription("Sets the description of the embed.") .setRequired(true) ) // #### Image #### .addAttachmentOption((option) => option .setName("image") .setDescription("Sends a image in the embed.") .setRequired(false) ) // #### Footer #### .addStringOption((option) => option .setName("footer") .setDescription("Set the footer of the embed") .setRequired(false) ) ,
错误原因
Discord API强制要求:所有必填(required: true)的命令选项必须放在非必填(required: false)选项的前面。
你的命令结构中,title是非必填选项,但后面紧跟的description是必填选项,违反了API的顺序规则,因此触发50035错误。
解决方案
调整选项顺序,将所有设置了setRequired(true)的选项移到非必填选项之前。具体来说,把description选项移到title选项的前面即可。
修改后的代码示例:
module.exports = { data: new SlashCommandBuilder() .setName("announcement") .setDescription("Sends a embed message in a channel.") .setDefaultMemberPermissions(PermissionFlagsBits.Administrator) // |#################################| // | Role Ping | // |#################################| .addRoleOption((option) => option .setName("ping-role") .setDescription("Pings the role.") .setRequired(true) ) // |#################################| // | Channel Option | // |#################################| .addChannelOption((option) => option .setName("channel") .setDescription("Channel that the embed message gets sent in.") .setRequired(true) ) // |#################################| // | Embed Options | // |#################################| // #### Description #### .addStringOption((option) => option .setName("description") .setDescription("Sets the description of the embed.") .setRequired(true) ) // ##### Title ##### .addStringOption((option) => option .setName("title") .setDescription("Title of the embed.") .setRequired(false) ) // #### Image #### .addAttachmentOption((option) => option .setName("image") .setDescription("Sends a image in the embed.") .setRequired(false) ) // #### Footer #### .addStringOption((option) => option .setName("footer") .setDescription("Set the footer of the embed") .setRequired(false) ) ,
内容的提问来源于stack exchange,提问作者CrazyNation
相关产品推荐
相关产品推荐

