升级discord.js v14后出现DiscordAPIError[50035]错误求助
问题描述
我下载了discord.js v14(非最新版本),运行时遇到DiscordAPIError[50035]错误。该功能在v13.1.0版本中正常工作,但无法定位问题原因。错误提示为无效表单体,具体是"CHAT_INPUT"、"USER"、"STRING"等值不是整数,错误指向代码await guild.commands.set(slashcmds);。完整错误栈如下:
DiscordAPIError[50035]: Invalid Form Body 0.type[NUMBER_TYPE_COERCE]: Value "CHAT_INPUT" is not int. 3.options[0].type[NUMBER_TYPE_COERCE]: Value "USER" is not int. 3.options[1].type[NUMBER_TYPE_COERCE]: Value "STRING" is not int. 4.options[0].type[NUMBER_TYPE_COERCE]: Value "USER" is not int. 4.options[1].type[NUMBER_TYPE_COERCE]: Value "STRING" is not int. at SequentialHandler.runRequest (H:\Projects\Discord\Handler\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:293:15) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async SequentialHandler.queueRequest (H:\Projects\Discord\Handler\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14) at async REST.request (H:\Projects\Discord\Handler\node_modules\@discordjs\rest\dist\lib\REST.cjs:52:22) at async GuildApplicationCommandManager.set (H:\Projects\Discord\Handler\node_modules\discord.js\src\managers\ApplicationCommandManager.js:173:18) at async H:\Projects\Discord\Handler\handler\index.js:47:13 {
解决方法
问题原因
discord.js v14起,命令类型和选项类型不再支持字符串字面量,必须使用官方提供的枚举值(整数类型),而v13版本允许使用字符串,这是版本升级后报错的核心原因。
修复步骤
- 导入枚举类型
在代码顶部引入discord.js中的对应枚举:
const { ApplicationCommandType, ApplicationCommandOptionType } = require('discord.js');
- 替换命令与选项的type字段
将所有字符串类型替换为枚举值:
- 把
type: "CHAT_INPUT"替换为type: ApplicationCommandType.ChatInput - 把选项中的
type: "USER"替换为type: ApplicationCommandOptionType.User - 把选项中的
type: "STRING"替换为type: ApplicationCommandOptionType.String
- 示例对比
修改前的命令定义:
const slashcmds = [ { name: 'example', type: "CHAT_INPUT", description: '示例命令', options: [ { name: 'target', type: "USER", description: '目标用户', required: true }, { name: 'content', type: "STRING", description: '输入内容', required: true } ] } ];
修改后的命令定义:
const slashcmds = [ { name: 'example', type: ApplicationCommandType.ChatInput, description: '示例命令', options: [ { name: 'target', type: ApplicationCommandOptionType.User, description: '目标用户', required: true }, { name: 'content', type: ApplicationCommandOptionType.String, description: '输入内容', required: true } ] } ];
完成以上修改后,重新运行代码即可解决该错误。
内容的提问来源于stack exchange,提问作者05_4
相关产品推荐
相关产品推荐

