DiscordJS v13 SlashCommandBuilder调用addChoices报CombinedPropertyError
Discord.js v13 SlashCommandBuilder addChoices 触发 CombinedPropertyError 解决方案
在使用Discord.js v13的SlashCommandBuilder开发斜杠指令时,调用addChoices传入数组参数会触发CombinedPropertyError,错误提示明确指出Expected the value to not be an array。这是因为v13版本的addChoices不支持直接传入数组,需要将每个选项对象作为独立参数传递。
解决方案
有两种修正方式:
- 将每个choice对象作为单独参数传给
addChoices - 使用ES6展开运算符
...将数组展开为多个参数
修正后的代码示例
方式1:逐个传递选项对象
new SlashCommandBuilder() .setName('buy') .setDescription('Get the price of a minion') .addStringOption(option => option.setName('minion').setDescription('Choose a minion').setRequired(true)) .addStringOption(option => option.setName('tier') .setDescription('Choose a tier') .setRequired(true) .addChoices( { name: 'Tier 11', value: 'Tier 11' }, { name: 'Tier 12', value: 'Tier 12' } ) ), new SlashCommandBuilder() .setName('config') .setDescription('Set the price of a minion') .addStringOption(option => option.setName('minion').setDescription('Choose a minion').setRequired(true)) .addStringOption(option => option.setName('tier') .setDescription('Choose a tier') .setRequired(true) .addChoices( { name: 'Tier 11', value: 'Tier 11' }, { name: 'Tier 12', value: 'Tier 12' } ) ) .addNumberOption(option => option.setName('price').setDescription('Set the price').setRequired(true))
方式2:使用展开运算符(适合选项较多的场景)
// 先定义选项数组 const tierChoices = [ { name: 'Tier 11', value: 'Tier 11' }, { name: 'Tier 12', value: 'Tier 12' } ]; new SlashCommandBuilder() .setName('buy') .setDescription('Get the price of a minion') .addStringOption(option => option.setName('minion').setDescription('Choose a minion').setRequired(true)) .addStringOption(option => option.setName('tier') .setDescription('Choose a tier') .setRequired(true) .addChoices(...tierChoices) // 用展开运算符把数组拆成单个参数 ), new SlashCommandBuilder() .setName('config') .setDescription('Set the price of a minion') .addStringOption(option => option.setName('minion').setDescription('Choose a minion').setRequired(true)) .addStringOption(option => option.setName('tier') .setDescription('Choose a tier') .setRequired(true) .addChoices(...tierChoices) ) .addNumberOption(option => option.setName('price').setDescription('Set the price').setRequired(true))
注意事项
- Discord.js v14才支持直接传入数组给
addChoices,若不升级版本,必须遵循v13的参数格式 - 每个choice对象必须包含
name(用户可见的显示文本)和value(后台接收的实际值)两个属性
内容的提问来源于stack exchange,提问作者Varun
相关产品推荐
相关产品推荐

