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
相关产品推荐
相关产品推荐

