Discord.js子命令报错:子命令与其他选项类型互斥问题咨询
解决Discord.js子命令选项报错问题
你遇到的错误并非因为不能给子命令添加选项,而是代码存在括号闭合错误,导致本该属于add子命令的evidence选项被误加到了主命令层面。Discord的Slash命令规则明确要求:如果命令包含子命令/子命令组,主命令就不能再添加任何普通选项(比如直接调用.addStringOption这类方法),两者互斥,这就是触发APPLICATION_COMMAND_OPTIONS_TYPE_INVALID错误的核心原因。
问题代码的错误点
看你代码中add子命令的实现:
.addSubcommand((subcommand) => subcommand.setName('add') .setDescription('add a warning to an user') .addUserOption((option) => option.setName('user') .setDescription('user to warn') .setRequired(true)) .addStringOption((option) => option.setName('reason') .setDescription('reason to warn') .setRequired(false))) // 这里提前闭合了回调函数的括号 .addStringOption((option) => // 这个方法被调用在了主命令上,而非子命令 option.setName('evidence') .setDescription('evidence to warn') .setRequired(false))
在添加完reason选项后,你提前闭合了addSubcommand的回调括号,导致后续的.addStringOption作用于主命令的SlashCommandBuilder实例,而非add子命令,违反了Discord的命令结构规则。
修正后的代码
把evidence选项的添加逻辑移到add子命令的链式调用中,调整括号位置即可:
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } = require('discord.js'); const { warningSchema } = require('../models/warnings.js') module.exports = { data: new SlashCommandBuilder() .setName('warnings') .setDescription('complete warning system') .setDefaultMemberPermissions(PermissionFlagsBits.KickMembers) .addSubcommand((subcommand) => subcommand.setName('add') .setDescription('add a warning to an user') .addUserOption((option) => option.setName('user') .setDescription('user to warn') .setRequired(true)) .addStringOption((option) => option.setName('reason') .setDescription('reason to warn') .setRequired(false)) // 将evidence选项移到subcommand的链式调用内,修正括号位置 .addStringOption((option) => option.setName('evidence') .setDescription('evidence to warn') .setRequired(false))) .addSubcommand((subcommand) => subcommand.setName('check') .setDescription('check warnings of an user') .addUserOption((option) => option.setName('user') .setDescription('user to check') .setRequired(true))) .addSubcommand((subcommand) => subcommand.setName('remove') .setDescription('remove a specific warning of an user') .addUserOption((option) => option.setName('user') .setDescription('user to remove warning from') .setRequired(true)) .addStringOption((option) => option.setName('id') .setDescription('target warning') .setRequired(true))) .addSubcommand((subcommand) => subcommand.setName('clear') .setDescription('clear all warnings of an user') .addUserOption((option) => option.setName('user') .setDescription('user to clear warnings from') .setRequired(true))), };
另外顺便修正了几个子命令中user选项的描述,避免语义混淆。
内容的提问来源于stack exchange,提问作者kagent263
相关产品推荐
相关产品推荐

