Search code examples
javascriptdiscorddiscord.js

ValidationError: Expected the value to be an object


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.


Solution

  • 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.