ValidationError: 预期值应为对象

huangapple go评论67阅读模式
英文:

ValidationError: Expected the value to be an object

问题

这是您提供的代码的翻译:

我是discord.js中的新程序员我有这段代码它应该根据用户输入的数量计算箱子的价值但是代码存在问题

```javascript
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('箱子类型')
                .setRequired(true)
                .addChoice('普通', 'normal')
                .addChoice('大型', 'large')
        )
        .addStringOption(option =>
            option.setName('item')
                .setDescription('要计算的物品')
                .setRequired(true)
                .addChoice("甜瓜", "melon")
                .addChoice("菠萝", "pineapple")
        )
        .addIntegerOption(option =>
            option.setName('amount')
                .setDescription('要计算的物品数量')
                .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(`我已经计算出 ${amount} 个${item} 来自${crateSize}箱子总价值为${totalValue}$`)
	},
};

您提到代码中存在错误,但是由于错误消息似乎涉及到具体的代码执行环境和错误的来源,我无法提供直接的解决方案。您可能需要检查代码以解决错误,或者提供更多关于错误的上下文,以便我可以帮助您更好地解决问题。

英文:

I'm a new programmer in discord.js and i have this code its supposed to calculate the value of the crate by the amount the user inserts but there is an issue with the code.

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}$`)
	},
};

I tried fixing it but i doesn't work and it always gives me the following error.

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'
      }
    ]
  ]
}

I thought it would work without any errors and make the calculate command.

答案1

得分: 0

你正在尝试使用字符串而不是预期的对象来添加命令选择项。

.addChoices() 函数期望的输入格式如下:

.addChoices({name: "普通", value: "normal"})

(你可以一次向 addChoices 函数提供多个选择对象,无需逐个传递它们。)

name 属性将是用户在命令菜单中看到的内容,而 value 是在通过交互选项对象检索命令时获得的值。

英文:

You are attempting to add command choices using strings instead of the expected object.

The .addChoices() function expects input like this:

.addChoices({name: "Normal", value: "normal"})

(You can give the addChoices function multiple choices objects at once, you dont need to pass it them one by one.)

The name property will be the thing the user sees in the command menu and the value is the value you will get when retrieving the command through the interaction options object.

huangapple
  • 本文由 发表于 2023年4月17日 15:39:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定