我试图列出船的名称和它的价格,所以用户可以挑选和选择他们想买的船,不太确定为什么它似乎不工作,我尝试了几种不同的方法来修复它,但它仍然输出相同的确切错误。
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' }
]
}
]
]
}
尝试替换
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'}])),
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()
的参数上删除了周围的[]
Discord.js API文档包含以下代码片段,其中显示了这样使用.addChoices()
函数:
const { SlashCommandBuilder } = require('discord.js');
const data = new SlashCommandBuilder()
.setName('gif')
.setDescription('Sends a random gif!')
.addStringOption(option =>
option.setName('category')
.setDescription('The gif category')
.setRequired(true)
.addChoices(
{ name: 'Funny', value: 'gif_funny' },
{ name: 'Meme', value: 'gif_meme' },
{ name: 'Movie', value: 'gif_movie' },
));