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

运行Discord.js+Node.js机器人时出现DiscordAPIError[50035]求助

Discord.js机器人创建Slash命令时DiscordAPIError[50035]问题解决

错误信息

node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:293
        throw new DiscordAPIError.DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
              ^

DiscordAPIError[50035]: Invalid Form Body
options[0].type[NUMBER_TYPE_COERCE]: Value "string" is not int.
options[1].type[NUMBER_TYPE_COERCE]: Value "string" is not int.
    at SequentialHandler.runRequest (D:\Programming\GitHub\void-bot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:293:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (D:\Programming\GitHub\void-bot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14)
    at async REST.request (D:\Programming\GitHub\void-bot\node_modules\@discordjs\rest\dist\lib\REST.cjs:52:22)
    at async GuildApplicationCommandManager.create (D:\Programming\GitHub\void-bot\node_modules\discord.js\src\managers\ApplicationCommandManager.js:144:18) {
  rawError: {
    code: 50035,
    errors: {
      options: {
        '0': { type: { _errors: [Array] } },
        '1': { type: { _errors: [Array] } }
      }
    },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'POST',
  url: 'https://discord.com/api/v10/applications/1016623217399570512/guilds/915541815225118740/commands',
  requestBody: {
    files: undefined,
    json: {
      name: 'poll',
      name_localizations: undefined,
      description: 'Sets up a poll.',
      description_localizations: undefined,
      type: undefined,
      options: [
        {
          type: 'string',
          name: 'title',
          name_localizations: undefined,
          name_localized: undefined,
          description: 'The title of the poll.',
          description_localizations: undefined,
          description_localized: undefined,
          required: true,
          autocomplete: undefined,
          choices: undefined,
          options: undefined,
          channel_types: undefined,
          min_value: undefined,
          max_value: undefined,
          min_length: undefined,
          max_length: undefined
        },
        {
          type: 'string',
          name: 'content',
          name_localizations: undefined,
          name_localized: undefined,
          description: 'The content of the poll.',
          description_localizations: undefined,
          description_localized: undefined,
          required: true,
          autocomplete: undefined,
          choices: undefined,
          options: undefined,
          channel_types: undefined,
          min_value: undefined,
          max_value: undefined,
          min_length: undefined,
          max_length: undefined
        }
      ],
      default_member_permissions: undefined,
      dm_permission: undefined
    }
  }
}

问题原因

Discord API要求Slash命令的选项类型必须传入整数类型的类型码,你在代码里给type字段传了字符串"string",不符合API参数要求,导致请求被拒绝。

另外代码还有几处语法和逻辑错误:

  • EmbedBuilder的addFields方法中,每个字段对象缺少name属性
  • interaction.reply的参数格式不正确,需包裹在对象里
  • 存在未闭合的大括号,会触发语法错误

解决方案

  1. 导入Discord.js提供的ApplicationCommandOptionType枚举,用它指定选项类型,避免手动输入数值出错
  2. 替换所有选项中的type: "string"为type: ApplicationCommandOptionType.String
  3. 修复EmbedBuilder和interaction.reply中的错误

修改后的完整代码

require("dotenv").config();
const { GatewayIntentBits, ApplicationCommandOptionType, EmbedBuilder } = require("discord.js");
const Discord = require("discord.js");

const client = new Discord.Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildBans,
        GatewayIntentBits.GuildEmojisAndStickers,
        GatewayIntentBits.GuildIntegrations,
        GatewayIntentBits.GuildWebhooks,
        GatewayIntentBits.GuildInvites,
        GatewayIntentBits.GuildVoiceStates,
        GatewayIntentBits.GuildPresences,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.GuildMessageReactions,
        GatewayIntentBits.GuildMessageTyping,
        GatewayIntentBits.DirectMessages,
        GatewayIntentBits.DirectMessageReactions,
        GatewayIntentBits.DirectMessageTyping,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildScheduledEvents
    ]
});

client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}!`);

    const guildID = "915541815225118740";
    const guild = client.guilds.cache.get(guildID);
    let commands;

    if (guild) {
        commands = guild.commands;
    } else {
        commands = client.application?.commands;
    }

    commands?.create({
        name: "poll",
        description: "Sets up a poll.",
        options: [
            {
                name: "title",
                description: "The title of the poll.",
                required: true,
                type: ApplicationCommandOptionType.String
            },
            {
                name: "content",
                description: "The content of the poll.",
                required: true,
                type: ApplicationCommandOptionType.String
            }
        ]
    });
});

client.on("interactionCreate", async (interaction) => {
    if (!interaction.isCommand()) {
        return;
    }

    const { commandName, options } = interaction;

    if (commandName === "poll") {
        const embed = new EmbedBuilder()
            .setColor(0x0099FF)
            .setTitle(options.getString("title"))
            .addFields(
                {
                    name: "Poll Content",
                    value: options.getString("content"),
                    inline: false
                },
                {
                    name: "Vote Options",
                    value: ":thumbsup: - Yes",
                    inline: true
                },
                {
                    name: "\u200B", // 空名称用于对齐
                    value: ":thumbsdown: - No",
                    inline: true
                }
            );

        await interaction.reply({ embeds: [embed] });
    }
});

client.login(process.env.TOKEN);

额外说明:

  • 使用options.getString("title")替代options.get("title"),可更明确获取字符串类型的选项值
  • EmbedBuilder的字段必须包含name和value,空名称\u200B用来让第二个和第三个字段保持对齐
  • interaction.reply需要传入包含embeds数组的对象,不能直接传Embed实例

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 08:15:34