discord.js crate计算器命令报错:ValidationError期望对象类型值
Discord.js Crate计算器斜杠命令报错修复
问题概述
我是Discord.js新手,编写了一个计算不同类型crate物品总价值的斜杠命令,命令接收crate类型、物品类型和数量参数来计算总价值,但运行时出现错误。
原代码
const { SlashCommandBuilder } = require('discord.js'); const normalMelonValue = 9600; const normalPineappleValue = 11520; const LargeMelonValue = 27000; const LargePineappleValue = 31050; module.exports = { data: new SlashCommandBuilder() .setName('crate') .setDescription('Crate Calculator') .addStringOption(option => option.setName('type') .setDescription('The Type of the crate') .setRequired(true) .addChoice('Normal', 'normal') .addChoice('Large', 'large') ) .addStringOption(option => option.setName('item') .setDescription('The item to calculate') .setRequired(true) .addChoice("Melon", "melon") .addChoice("Pineapple", "pineapple") ) .addIntegerOption(option => option.setName('amount') .setDescription('The amount of the item to calculate') .setRequired(true), ), async execute(interaction) { const crateSize = interaction.options.getString('type'); const item = interaction.options.getString('item'); const amount = interaction.options.getInteger('amount'); let totalValue = 0; if (crateSize === 'normal') { if (item === 'melon') { totalValue = normalMelonValue * amount; } else if (item === 'pineapple') { totalValue = normalPineappleValue * amount; } } else if (crateSize === 'large') { if (item === 'melon') { totalValue = LargeMelonValue * amount; } else if (item === 'pineapple') { totalValue = LargePineappleValue * amount; } } await interaction.reply(`I have calculated ${amount} ${item} from ${crateSize} crate, and the total value is ${totalValue}$`) }, };
报错信息
C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:47 throw this.error; ^ CombinedPropertyError: Received one or more errors at ArrayValidator.handle (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:468:70) at ArrayValidator.parse (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:207:88) at MixedClass.addChoices (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:1702:22) at C:\Users\Dell\Desktop\bot\commands\islands\crates.js:16:18 at MixedClass._sharedAddOptionMethod (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:2008:50) at MixedClass.addStringOption (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:1987:17) at Object.<anonymous> (C:\Users\Dell\Desktop\bot\commands\islands\crates.js:12:10) at Module._compile (node:internal/modules/cjs/loader:1275:14) at Module._extensions..js (node:internal/modules/cjs/loader:1329:10) at Module.load (node:internal/modules/cjs/loader:1133:32) { errors: [ [ 0, ValidationError: Expected the value to be an object, but received string instead at ObjectValidator.handle (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:1202:25) at ObjectValidator.run (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:193:23) at ArrayValidator.handle (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:462:37) at ArrayValidator.parse (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:207:88) at MixedClass.addChoices (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:1702:22) at C:\Users\Dell\Desktop\bot\commands\islands\crates.js:16:18 at MixedClass._sharedAddOptionMethod (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:2008:50) at MixedClass.addStringOption (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:1987:17) at Object.<anonymous> (C:\Users\Dell\Desktop\bot\commands\islands\crates.js:12:10) at Module._compile (node:internal/modules/cjs/loader:1275:14) { validator: 's.object(T)', given: 'Normal' } ], [ 1, ValidationError: Expected the value to be an object, but received string instead at ObjectValidator.handle (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:1202:25) at ObjectValidator.run (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:193:23) at ArrayValidator.handle (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:462:37) at ArrayValidator.parse (C:\Users\Dell\Desktop\bot\node_modules\@sapphire\shapeshift\dist\index.js:207:88) at MixedClass.addChoices (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:1702:22) at C:\Users\Dell\Desktop\bot\commands\islands\crates.js:16:18 at MixedClass._sharedAddOptionMethod (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:2008:50) at MixedClass.addStringOption (C:\Users\Dell\Desktop\bot\node_modules\@discordjs\builders\dist\index.js:1987:17) at Object.<anonymous> (C:\Users\Dell\Desktop\bot\commands\islands\crates.js:12:10) at Module._compile (node:internal/modules/cjs/loader:1275:14) { validator: 's.object(T)', given: 'normal' } ] ] }
错误原因
报错核心是addChoice的API使用错误。Discord.js v14及后续版本中,@discordjs/builders包要求addChoice方法接收一个包含name(用户可见的选项文本)和value(后台获取的实际值)的对象,不再支持直接传入两个字符串参数。
修复后的代码
const { SlashCommandBuilder } = require('discord.js'); const normalMelonValue = 9600; const normalPineappleValue = 11520; const LargeMelonValue = 27000; const LargePineappleValue = 31050; module.exports = { data: new SlashCommandBuilder() .setName('crate') .setDescription('Crate Calculator') .addStringOption(option => option.setName('type') .setDescription('The Type of the crate') .setRequired(true) .addChoice({ name: 'Normal', value: 'normal' }) .addChoice({ name: 'Large', value: 'large' }) ) .addStringOption(option => option.setName('item') .setDescription('The item to calculate') .setRequired(true) .addChoice({ name: "Melon", value: "melon" }) .addChoice({ name: "Pineapple", value: "pineapple" }) ) .addIntegerOption(option => option.setName('amount') .setDescription('The amount of the item to calculate') .setRequired(true) ), async execute(interaction) { const crateSize = interaction.options.getString('type'); const item = interaction.options.getString('item'); const amount = interaction.options.getInteger('amount'); let totalValue = 0; if (crateSize === 'normal') { totalValue = item === 'melon' ? normalMelonValue * amount : normalPineappleValue * amount; } else if (crateSize === 'large') { totalValue = item === 'melon' ? LargeMelonValue * amount : LargePineappleValue * amount; } await interaction.reply(`已计算 ${amount} 个 ${item}(来自${crateSize} crate),总价值为 ${totalValue}$`) }, };
额外优化说明
- 将嵌套的if-else简化为三元表达式,让代码更简洁
- 把回复文本改成中文,更符合使用习惯(如果不需要可以改回英文)
内容的提问来源于stack exchange,提问作者game
相关产品推荐
相关产品推荐

