Discord.js命令选项类型报错:Invalid Form Body问题求助
问题排查与解决
这个错误的核心原因是你在定义命令选项类型时,使用了字符串'User'而非Discord.js要求的枚举类型值。Discord API需要明确的类型标识,直接传字符串会被判定为缺失合法的type字段。
解决步骤:
- 首先导入Discord.js提供的
ApplicationCommandOptionType枚举(针对v14及以上版本):const { ApplicationCommandOptionType } = require('discord.js'); // ES模块写法 import { ApplicationCommandOptionType } from 'discord.js'; - 将选项配置里的字符串类型替换为枚举值:
// 错误写法 { name: 'target', description: '目标用户', type: 'User', // 字符串类型不符合API要求 required: true } // 正确写法 { name: 'target', description: '目标用户', type: ApplicationCommandOptionType.User, // 使用官方枚举值 required: true }
额外检查点:
- 确认你的Discord.js版本为v14或更新(v13及更早版本的类型写法不同,但需遵循对应版本的类型规范)
- 检查命令注册的整体结构,确保选项数组没有语法错误,比如逗号遗漏、括号不匹配等
内容的提问来源于stack exchange,提问作者Cooper
相关产品推荐
相关产品推荐

