You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js buyship命令出现ValidationError:期望值不为数组

问题:Discord.js buyship命令报错ValidationError: Expected the value to not be an array

我尝试实现一个功能,列出船只名称及其价格供用户选择购买,但对应的Discord.js buyship命令无法正常运行,始终输出错误:ValidationError: Expected the value to not be an array。

相关代码:

const { SlashCommandBuilder } = require('discord.js');
const { getUserById, updateUserById } = require('../utils/user');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('buyship')
        .setDescription('Buy a ship from the shop')
        .addStringOption(option =>
            option.setName('ship')
                .setDescription('Select the ship you want to buy')
                .setRequired(true)
                .addChoices([{ name: 'Sloop', value: 'sloop' }, { name: 'Frigate', value: 'frigate' }, { name: 'Galleon', value: 'galleon' }, { name: "Man o' war", value: 'manowar'}])),
   
    async execute(interaction) {
        const ship = interaction.options.getString('ship');
        const shipPrices = {
            sloop: 100,
            frigate: 250,
            galleon: 500,
            manowar: 1000
        };
        const user = await getUserById(interaction.user.id);
        const balance = user.balance;
        const price = shipPrices[ship];
        if (!price) {
            return interaction.reply({ content: 'That is not a valid ship.', ephemeral: true });
        }
        if (balance < price) {
            return interaction.reply({ content: 'You do not have enough money to buy this ship.', ephemeral: true });
        }
        const newBalance = balance - price;
        await updateUserById(interaction.user.id, { balance: newBalance, ship: ship });
        return interaction.reply(`You bought a ${ship} for ${price} coins.`);
    },
};

错误栈信息:

ValidationError: Expected the value to not be an array
          at ObjectValidator.handle (/Users/humza/Desktop/Humza/pirate/node_modules/@sapphire/shapeshift/dist/index.js:1208:25)
          at ObjectValidator.run (/Users/humza/Desktop/Humza/pirate/node_modules/@sapphire/shapeshift/dist/index.js:193:23)
          at ArrayValidator.handle (/Users/humza/Desktop/Humza/pirate/node_modules/@sapphire/shapeshift/dist/index.js:462:37)
          at ArrayValidator.parse (/Users/humza/Desktop/Humza/pirate/node_modules/@sapphire/shapeshift/dist/index.js:207:88)
          at MixedClass.addChoices (/Users/humza/Desktop/Humza/pirate/node_modules/@discordjs/builders/dist/index.js:1702:22)
          at /Users/humza/Desktop/Humza/pirate/commands/buyship.js:12:18
          at MixedClass._sharedAddOptionMethod (/Users/humza/Desktop/Humza/pirate/node_modules/@discordjs/builders/dist/index.js:2008:50)
          at MixedClass.addStringOption (/Users/humza/Desktop/Humza/pirate/node_modules/@discordjs/builders/dist/index.js:1987:17)
          at Object.<anonymous> (/Users/humza/Desktop/Humza/pirate/commands/buyship.js:8:10)
          at Module._compile (node:internal/modules/cjs/loader:1254:14) {
        validator: 's.object(T)',
        given: [
          { name: 'Sloop', value: 'sloop' },
          { name: 'Frigate', value: 'frigate' },
          { name: 'Galleon', value: 'galleon' },
          { name: "Man o' war", value: 'manowar' }
        ]
      }
解决方法

报错的核心原因是addChoices方法的调用方式错误。你当前是把包含多个选项的数组直接传给addChoices,但在Discord.js v14及以上版本中,addChoices需要逐个传入选项对象,或者使用扩展运算符展开数组,而不是直接传递整个数组。

修改后的代码(重点修改addChoices部分):

const { SlashCommandBuilder } = require('discord.js');
const { getUserById, updateUserById } = require('../utils/user');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('buyship')
        .setDescription('Buy a ship from the shop')
        .addStringOption(option =>
            option.setName('ship')
                .setDescription('Select the ship you want to buy')
                .setRequired(true)
                // 方法一:逐个传入选项对象
                .addChoices(
                    { name: 'Sloop', value: 'sloop' },
                    { name: 'Frigate', value: 'frigate' },
                    { name: 'Galleon', value: 'galleon' },
                    { name: "Man o' war", value: 'manowar' }
                )
                // 方法二:使用扩展运算符展开数组
                // .addChoices(...[{ name: 'Sloop', value: 'sloop' }, { name: 'Frigate', value: 'frigate' }, { name: 'Galleon', value: 'galleon' }, { name: "Man o' war", value: 'manowar'}])
        ),
   
    async execute(interaction) {
        const ship = interaction.options.getString('ship');
        const shipPrices = {
            sloop: 100,
            frigate: 250,
            galleon: 500,
            manowar: 1000
        };
        const user = await getUserById(interaction.user.id);
        const balance = user.balance;
        const price = shipPrices[ship];
        if (!price) {
            return interaction.reply({ content: 'That is not a valid ship.', ephemeral: true });
        }
        if (balance < price) {
            return interaction.reply({ content: 'You do not have enough money to buy this ship.', ephemeral: true });
        }
        const newBalance = balance - price;
        await updateUserById(interaction.user.id, { balance: newBalance, ship: ship });
        return interaction.reply(`You bought a ${ship} for ${price} coins.`);
    },
};

这样修改后,addChoices就能正确识别每个选项对象,不会把整个数组当成单个参数处理,也就不会触发"Expected the value to not be an array"的验证错误。

内容的提问来源于stack exchange,提问作者Humza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 01:22:03