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

DiscordJS v14命令处理器报错DiscordAPIError[50035],求排查

DiscordJS v14 命令处理器报错DiscordAPIError[50035] 修复方案

问题描述

开发DiscordJS v14命令处理器时,控制台抛出DiscordAPIError[50035]错误,提示"无效表单体,0.name字段为必填项";偶尔重启机器人后错误消失,但斜杠命令无法正常显示。

错误原因

命令处理器代码中,向commandArray添加元素时错误同时推入了整个命令对象和命令的JSON结构:

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

Discord API仅接受toJSON()转换后的命令配置对象,直接推入的command对象不符合API格式要求,缺少必填的name字段结构,导致接口校验失败。

修复后的代码

将上述错误行修改为仅推入命令的JSON结构:

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

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

      const { commands, commandArray } = client;
      for (const file of commandFiles) {
        const command = require(`../../commands/${folder}/${file}`);
        commands.set(command.data.name, command);
        // 仅推入符合API要求的命令JSON结构
        commandArray.push(command.data.toJSON());
        console.log(
          chalk.white("Command: ") +
            chalk.cyan.bold`${command.data.name} ` +
            chalk.white("has successfully loaded.")
        );
      }
    }

    const clientId = "(Client_ID)";
    const rest = new REST({ version: "10" }).setToken(
      process.env.DISCORD_DEV_TOKEN
    );

    try {
      console.log("Started refreshing application (/) commands.");

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

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

验证说明

修改后,commandArray仅包含符合Discord API要求的命令配置JSON,执行命令刷新时不会触发字段缺失错误,斜杠命令可正常注册并显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 00:10:36