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

DiscordJS v14子命令部署报错:Invalid Form Body求助

问题排查:Discord 斜线命令部署报错 DiscordAPIError[50035]

错误原因

报错提示Sub-command and sub-command group option types are mutually exclusive to all other types,核心问题是:当斜线命令包含子命令(subcommand)时,主命令不能直接添加其他类型的选项(如String、Channel等),所有选项必须嵌套在子命令内部。

你的代码中,addStringOption直接链式调用在SlashCommandBuilder主实例上,而非嵌套在subcommand的回调逻辑里,这违反了Discord的命令结构规则,导致部署失败。

修复后的代码

const { SlashCommandBuilder } = require('discord.js');

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

module.exports = {
    data: new SlashCommandBuilder()
        .setName('say')
        .setDescription('Make bot say anything the executor wants')
        .addSubcommand(subcommand =>
            subcommand
                .setName('channel')
                .setDescription('Send message to a specified channel')
                .addChannelOption(option =>
                    option
                        .setName('target_channel')
                        .setDescription('Channel to send message in.')
                        .setRequired(true))
                // 将字符串选项移至子命令内部
                .addStringOption(option =>
                    option
                        .setName('text')
                        .setDescription("Message to be sent in specified channel")
                        .setRequired(true))),
    async execute(interaction) {
        const channel = interaction.options.getChannel('target_channel');
        const text = interaction.options.getString('text');

        // 优化发送逻辑,直接使用获取到的channel对象
        await channel.send(text);
        // 添加交互回复,避免Discord判定命令无响应
        await interaction.reply({ content: 'Message sent successfully!', ephemeral: true });
    },
};

额外优化说明

  • 补充了子命令的描述文本,避免空描述导致的潜在问题
  • 简化消息发送逻辑,直接使用获取到的channel对象调用send,无需从缓存中二次查找
  • 添加了临时交互回复,符合Discord的命令响应要求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 08:25:21