You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发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字符)。

排查与修复步骤

  1. 检查命令结构完整性
    确保每个添加到client.commandArray的命令对象都包含name和description两个必填字段,示例正确结构:

    // 正确的斜杠命令结构
    const validCommand = {
      name: "ping", // 必填:命令名称
      description: "返回Pong响应", // 必填:命令描述
      // 可选:命令参数options等
    };
    
  2. 校验命令数组生成逻辑
    如果你是通过遍历命令文件生成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);
    }
    
  3. 重新执行命令刷新
    确认client.commandArray中所有命令都符合要求后,重新运行机器人代码,即可正常完成斜杠命令的刷新。

内容的提问来源于stack exchange,提问作者Blinken 77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 13:25:32