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

Discord.JS注册斜杠命令报DiscordAPIError[50035] Invalid Form Body

问题说明

参照discord.js官方指南提供的基础命令处理器编写suggest建议命令时,每次部署命令都会抛出如下报错,此前尝试修改embed嵌入内容排查问题,始终未解决。

DiscordAPIError[50035]: Invalid Form Body
guild_id[NUMBER_TYPE_COERCE]: Value "undefined" is not snowflake.
    at SequentialHandler.runRequest (/home/container/node_modules/@discordjs/rest/dist/index.js:753:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (/home/container/node_modules/@discordjs/rest/dist/index.js:565:14)
    at async REST.request (/home/container/node_modules/@discordjs/rest/dist/index.js:999:22) {
  rawError: {
    code: 50035,
    errors: { guild_id: [Object] },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'PUT',
  url: 'https://discord.com/api/v9/applications/991898175478714409/guilds/undefined/commands',
  requestBody: { files: undefined, json: [ [Object], [Object], [Object] ] }
}

报错请求的API路径中guilds参数值为undefined,不符合Discord API要求的snowflake ID格式。

问题根因

这个报错和你修改embed内容的操作完全无关,之前的排查方向错了:错误触发在斜杠命令部署阶段,还没走到命令执行的逻辑。
从报错URL可以看到,你调用的是服务器(guild)级别的命令注册接口,但拼接在路径里的guild_id变量值是undefined,一般是两种情况导致:

  • 部署脚本里没有正确导入/定义目标服务器的ID,变量为空就直接传入了注册方法
  • 本来想注册全局命令,错用了需要传入guildID的guild命令注册路由

另外你贴的suggest命令代码本身存在几个会导致运行时报错的问题,也需要一并修正。

解决方案

1. 修复部署脚本的guildID配置问题

找到你用来部署斜杠命令的脚本文件(通常命名为deploy-commands.js),按以下步骤检查:

  • 如果你是从配置文件(比如config.json)读取guildID,先确认配置文件里存在对应字段、ID值正确,导入变量时名称和配置字段完全一致,没有拼写错误
  • 注册命令时不要手动拼接API路径,使用官方提供的路由方法,参考正确写法:
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');
// 省略收集commands数组的逻辑
const rest = new REST({ version: '9' }).setToken(token);
// 注册guild级命令(即时生效),确保传入的guildId不为undefined
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
	.then(() => console.log('服务器斜杠命令注册成功'))
	.catch(console.error);

// 如果要注册全局命令(最长1小时生效),替换成下面的路由,不需要传guildId
// rest.put(Routes.applicationCommands(clientId), { body: commands })

修正后重新运行部署脚本,50035报错就会消失。

2. 修复suggest命令的潜在bug

你写的命令逻辑有三个容易触发报错的点,按如下方式修正:

  • 斜杠命令选项名大小写敏感,你定义选项时用的名称是全小写的suggestion,取值时误写为首字母大写的Suggestion,会取不到用户输入的内容
  • 发送消息、回复交互都是异步操作,必须加await,否则容易触发交互超时、消息发送失败的未捕获错误
  • 读取建议频道时增加空值判断,避免配置的频道ID错误导致代码崩溃

修正后的完整命令代码:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed, MessageActionRow, MessageButton } = require('discord.js');
const { suggestChannelID, suggestLogChannelID, suggestManageRoleID } = require('/home/container/config.json');

module.exports = {
  data : new SlashCommandBuilder()
    .setName('suggest')
    .addStringOption(option => option.setName('suggestion').setDescription('The feature you want to suggest').setRequired(true))
    .setDescription('Suggest a feature to be added to our platform(s)'),
  async execute(interaction) {
    const d = new Date();
    const suggestEmbed = new MessageEmbed()
      .setTitle('New Suggestion')
      // 修正选项名大小写匹配
      .setDescription(interaction.options.getString('suggestion'))
      .setFooter({ text: `Suggested by <@${interaction.member.id}> @ ${d.toLocaleTimeString()} GMT` })
    const row = new MessageActionRow()
      .addComponents(
        new MessageButton()
          .setCustomId('upvote')
          .setStyle('SUCCESS')
          .setLabel('Upvote'),
        new MessageButton()
          .setCustomId('downvote')
          .setStyle('DANGER')
          .setLabel('downvote'),
        new MessageButton()
          .setCustomId('accept')
          .setStyle('SUCCESS')
          .setLabel('Accept'),
        new MessageButton()
          .setCustomId('deny')
          .setStyle('DANGER')
          .setLabel('Deny'),
        new MessageButton()
          .setCustomId('remove')
          .setStyle('SECONDARY')
          .setLabel('Remove'),
      )
    // 增加频道空值校验
    const suggestChannel = interaction.guild.channels.cache.get(suggestChannelID);
    if (!suggestChannel) {
      return interaction.reply({ content: '建议频道配置错误,请联系管理员处理', ephemeral: true });
    }
    await suggestChannel.send({ embeds: [suggestEmbed], components: [row] });
    // 回复设为仅自己可见,避免刷屏,加await防止超时
    await interaction.reply({ content: 'Suggestion successfully sent!', ephemeral: true });
  }
}

提示:discord.js v13及以上版本要求交互必须在触发后3秒内给出响应,加await可以规避网络延迟导致的超时报错,ephemeral类型的回复只有触发命令的用户能看到,不会污染公频。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 05:18:54