开发Discord机器人遇Invalid Form Body错误,求解决方案
解决Discord机器人命令刷新时的DiscordAPIError[50035]错误
错误原因
DiscordAPIError[50035]: Invalid Form Body 0.name[BASE_TYPE_REQUIRED]: This field is required 这个错误明确说明:你发送给Discord API的斜杠命令数组(client.commandArray)中,至少有一个命令对象缺少必填的name字段。Discord要求所有斜杠命令必须包含唯一、符合规范的名称(仅允许小写字母、数字、连字符,长度1-32字符)。
排查与修复步骤
检查命令结构完整性
确保每个添加到client.commandArray的命令对象都包含name和description两个必填字段,示例正确结构:// 正确的斜杠命令结构 const validCommand = { name: "ping", // 必填:命令名称 description: "返回Pong响应", // 必填:命令描述 // 可选:命令参数options等 };校验命令数组生成逻辑
如果你是通过遍历命令文件生成client.commandArray,要添加字段校验,避免遗漏必填项:const fs = require('fs'); const path = require('path'); client.commandArray = []; const commandsDir = path.join(__dirname, 'commands'); const commandFiles = fs.readdirSync(commandsDir).filter(f => f.endsWith('.js')); for (const file of commandFiles) { const command = require(path.join(commandsDir, file)); // 检查必填字段是否存在且有效 if (!command.name || typeof command.name !== 'string' || command.name.length === 0) { console.error(`命令文件 ${file} 缺少有效name字段`); continue; } if (!command.description || typeof command.description !== 'string') { console.error(`命令文件 ${file} 缺少有效description字段`); continue; } client.commandArray.push(command); }重新执行命令刷新
确认client.commandArray中所有命令都符合要求后,重新运行机器人代码,即可正常完成斜杠命令的刷新。
内容的提问来源于stack exchange,提问作者Blinken 77
相关产品推荐
相关产品推荐

