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

Discord.js v14报错DiscordAPIError[50035]请求体无效求助

Discord.js V14 机器人命令注册错误修复方案

错误信息

DiscordAPIError[50035]: Invalid Form Body
0.name[BASE_TYPE_REQUIRED]: This field is required
at handleErrors (C:\Users\gims3\OneDrive\바탕 화면\discord.js V14\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\gims3\OneDrive\바탕 화면\discord.js V14\node_modules@discordjs\rest\dist\index.js:1021:23)
at async SequentialHandler.queueRequest (C:\Users\gims3\OneDrive\바탕 화면\discord.js V14\node_modules@discordjs\rest\dist\index.js:862:14)
at async REST.request (C:\Users\gims3\OneDrive\바탕 화면\discord.js V14\node_modules@discordjs\rest\dist\index.js:1387:22)
at async client.handleCommands (C:\Users\gims3\OneDrive\바탕 화면\discord.js V14\src\functions\handlers\handleCommands.js:27:7) {
requestBody: { files: undefined, json: [ [Object], [Object] ] },
rawError: {
code: 50035,
errors: { '0': [Object] },
message: 'Invalid Form Body'
},
code: 50035,
status: 400,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/1043561701905272915/commands'
}

问题代码(命令处理器)

const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v10");
const fs = require("fs");

module.exports = (client) => {
  client.handleCommands = async () => {
    const commandFolders = fs.readdirSync(`./src/commands`);
    for (const folder of commandFolders) {
      const commandsFiles = fs
        .readdirSync(`./src/commands/${folder}`)
        .filter((file) => file.endsWith(".js"));

      const { commands, commandArray } = client;
      for (const file of commandsFiles) {
        const command = require(`../../commands/${folder}/${file}`);
        commands.set(command.data.name, command);
        commandArray.push(command, command.data.toJSON());
      }
    }

    const clientId = "1043561701905272915";
    const guildId = "775692469315698739";
    const rest = new REST({ version: "10" }).setToken(process.env.token);
    try {
      console.log("Started refreshing application (/) commands.");

      await rest.put(Routes.applicationCommands(clientId, guildId), {
        body: client.commandArray,
      });

      console.log("Successfully reloaded  application (/) commands.");
    } catch (error) {
      console.error(error);
    }
  };
};

错误原因与修复方法

核心问题

错误提示0.name[BASE_TYPE_REQUIRED]: This field is required说明向Discord API发送的命令数组中,第一个元素缺少必填的name字段。问题出在代码的这一行:

commandArray.push(command, command.data.toJSON());

这里同时将整个command对象和command.data.toJSON()的结构对象推入数组,但Discord API只需要command.data.toJSON()返回的标准化命令结构,整个command对象并不包含API要求的顶级name字段,导致验证失败。

修复步骤

修改push逻辑,只添加API需要的命令结构:

// 原代码
commandArray.push(command, command.data.toJSON());

// 修改后
commandArray.push(command.data.toJSON());

commands.set(command.data.name, command)这行无需改动,它是用来在客户端缓存命令,方便后续处理交互请求。

额外检查点

  • 确保每个命令文件中的data结构完整,必须包含name和description(Slash命令必填字段)
  • 验证process.env.token是否正确加载,避免因权限问题引发其他错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 17:47:09