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

推送Discord斜杠命令时出现Invalid Form Body错误求助

Discord全局推送斜杠命令失败:Invalid Form Body错误解决

问题详情

多方查找解决方案无果,尝试全局推送Discord斜杠命令时失败,执行代码后返回如下错误:

Started refreshing 13 application (/) commands.
DiscordAPIError[50035]: Invalid Form Body
0.name[BASE_TYPE_REQUIRED]: This field is required
1.name[BASE_TYPE_REQUIRED]: This field is required
2.name[BASE_TYPE_REQUIRED]: This field is required
3.name[BASE_TYPE_REQUIRED]: This field is required
4.name[BASE_TYPE_REQUIRED]: This field is required
5.name[BASE_TYPE_REQUIRED]: This field is required
6.name[BASE_TYPE_REQUIRED]: This field is required
7.name[BASE_TYPE_REQUIRED]: This field is required
8.name[BASE_TYPE_REQUIRED]: This field is required
9.name[BASE_TYPE_REQUIRED]: This field is required
10.name[BASE_TYPE_REQUIRED]: This field is required
11.name[BASE_TYPE_REQUIRED]: This field is required
12.name[BASE_TYPE_REQUIRED]: This field is required
    at SequentialHandler.runRequest (/home/ayden/microwave.js/node_modules/@discordjs/rest/dist/index.js:753:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async SequentialHandler.queueRequest (/home/ayden/microwave.js/node_modules/@discordjs/rest/dist/index.js:565:14)
    at async REST.request (/home/ayden/microwave.js/node_modules/@discordjs/rest/dist/index.js:999:22)
    at async /home/ayden/microwave.js/deploy.js:25:16 {
  rawError: {
    code: 50035,
    errors: {
      '0': [Object],
      '1': [Object],
      '2': [Object],
      '3': [Object],
      '4': [Object],
      '5': [Object],
      '6': [Object],
      '7': [Object],
      '8': [Object],
      '9': [Object],
      '10': [Object],
      '11': [Object],
      '12': [Object]
    },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'PUT',
  url: 'https://discord.com/api/v10/applications/867964961417203743/commands',
  requestBody: {
    files: undefined,
    json: [
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined
    ]
  }
}

当前使用的deploy.js代码:

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs');

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.ts'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(JSON.stringify(command.data));
}

const rest = new REST({ version: '10' }).setToken(token);

(async () => {
    try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);

        const data = await rest.put(
            Routes.applicationCommands(clientId),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    } catch (error) {
        console.error(error);
    }
})();

错误原因分析

  1. TypeScript文件加载失败:Node.js默认无法直接加载.ts后缀的文件,直接用require加载会导致command对象异常,command.data变为undefined。
  2. 错误序列化请求体:Discord API要求接收JavaScript对象格式的命令数据,但代码中使用JSON.stringify()将数据转为字符串,导致请求体不符合API要求。

解决方案

修正步骤1:处理TypeScript文件

  • 方案A:先将所有.ts命令文件编译为.js文件,然后修改deploy.js中读取的文件后缀为.js。
  • 方案B:使用ts-node直接运行部署脚本,无需提前编译,此时可以保留.ts后缀。

修正步骤2:修复请求体格式

移除JSON.stringify(),直接将SlashCommandBuilder实例转为API所需的JSON对象(调用toJSON()方法)。

修正后的代码

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const { clientId, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
// 根据实际情况选择后缀:编译后用.js,用ts-node则用.ts
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join('./commands', file);
    const command = require(filePath);
    // 将SlashCommandBuilder实例转为API兼容的JSON对象
    commands.push(command.data.toJSON());
}

const rest = new REST({ version: '10' }).setToken(token);

(async () => {
    try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);

        const data = await rest.put(
            Routes.applicationCommands(clientId),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    } catch (error) {
        console.error(error);
    }
})();

额外注意事项

确保每个命令文件中的data是正确的SlashCommandBuilder实例,示例命令文件:

const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('测试命令'),
    async execute(interaction) {
        await interaction.reply('测试成功!');
    },
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 16:40:15