Discord音乐机器人Lyrics命令触发CombinedError报错求助
问题:Discord音乐机器人Lyrics命令触发CombinedError
运行Lyrics歌词查询命令时,出现以下错误:
return Result.err(new CombinedError(errors)); ^ CombinedError: Received one or more errors at UnionValidator.handle (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:1051:23) at UnionValidator.parse (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:106:88) at EmbedBuilder.setColor (/home/shifty/bot-3/node_modules/@discordjs/builders/dist/index.js:228:20) at EmbedBuilder.setColor (/home/shifty/bot-3/node_modules/discord.js/src/structures/EmbedBuilder.js:22:18) at Object.run (/home/shifty/bot-3/commands/lyrics.js:33:12) at /home/shifty/bot-3/events/interactionCreate.js:62:26 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { errors: [ ExpectedValidationError: Expected values to be equals at LiteralValidator.handle (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:692:76) at LiteralValidator.run (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:92:23) at UnionValidator.handle (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:1046:32) at UnionValidator.parse (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:106:88) at EmbedBuilder.setColor (/home/shifty/bot-3/node_modules/@discordjs/builders/dist/index.js:228:20) at EmbedBuilder.setColor (/home/shifty/bot-3/node_modules/discord.js/src/structures/EmbedBuilder.js:22:18) at Object.run (/home/shifty/bot-3/commands/lyrics.js:33:12) at /home/shifty/bot-3/events/interactionCreate.js:62:26 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { validator: 's.literal(V)', given: undefined, expected: null }, ValidationError: Expected a number primitive at NumberValidator.handle (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:859:70) at NumberValidator.run (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:92:23) at UnionValidator.handle (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:1046:32) at UnionValidator.parse (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:106:88) at EmbedBuilder.setColor (/home/shifty/bot-3/node_modules/@discordjs/builders/dist/index.js:228:20) at EmbedBuilder.setColor (/home/shifty/bot-3/node_modules/discord.js/src/structures/EmbedBuilder.js:22:18) at Object.run (/home/shifty/bot-3/commands/lyrics.js:33:12) at /home/shifty/bot-3/events/interactionCreate.js:62:26 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { validator: 's.number', given: undefined }, ValidationError: Expected an array at TupleValidator.handle (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:1682:25) at TupleValidator.run (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:92:23) at UnionValidator.handle (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:1046:32) at UnionValidator.parse (/home/shifty/bot-3/node_modules/@sapphire/shapeshift/dist/index.js:106:88) at EmbedBuilder.setColor (/home/shifty/bot-3/node_modules/@discordjs/builders/dist/index.js:228:20) at EmbedBuilder.setColor (/home/shifty/bot-3/node_modules/discord.js/src/structures/EmbedBuilder.js:22:18) at Object.run (/home/shifty/bot-3/commands/lyrics.js:33:12) at /home/shifty/bot-3/events/interactionCreate.js:62:26 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { validator: 's.tuple(T)', given: undefined } ] }
Lyrics命令代码如下:
const { EmbedBuilder, ApplicationCommandOptionType } = require("discord.js"); const { get } = require("node-superfetch"); const config = require("../config.js"); const db = require("../mongoDB"); module.exports = { name: "lyrics", description: "Gets the lyrics of a song.", userPerms: [], player: true, dj: false, inVoiceChannel: true, sameVoiceChannel: true, options: [ { name: "song", description: "Song name to return lyrics for.", type: ApplicationCommandOptionType.String, required: false, }, ], /** * * @param {Client} client * @param {CommandInteraction} interaction */ run: async (client, interaction) => { await interaction.reply({ embeds: [ new EmbedBuilder() .setColor(client.embedColor) .setDescription("🔎 **Searching...**"), ], }); let player; if (client.manager) { player = client.manager.players.get(interaction.guild.id); } else { return interaction.reply({ embeds: [ new EmbedBuilder() .setColor("Red") .setDescription("Node not connected"), ], }); } const args = interaction.options.getString("song"); if (!args && !player) { return interaction.editReply({ embeds: [ new EmbedBuilder() .setColor("Red") .setDescription("There's no music playing."), ], }); } let search = args ? args : player.queue.current.title; let url = `https://api.darrennathanael.com/lyrics?song=${search}`; let lyrics = await get(url); if (!lyrics || lyrics.status !== 200) { return interaction.editReply({ embeds: [ new EmbedBuilder() .setColor("Red") .setDescription( `❌ | No lyrics found for ${search}!\nMake sure you entered your search correctly.` ), ], }); } let text = lyrics.body.lyrics; let lyricsEmbed = new EmbedBuilder() .setColor(client.embedColor) .setTitle(`${lyrics.body.full_title}`) .setURL(lyrics.url) .setThumbnail(lyrics.body.thumbnail) .setDescription(text); if (text.length > 4096) { text = text.substring(0, 4090) + "[...]"; lyricsEmbed .setDescription(text) .setFooter({ text: "Truncated, the lyrics were too long." }); } return await interaction.editReply({ embeds: [lyricsEmbed] }); }, };
已尝试给new EmbedBuilder添加const关键字,问题未解决。
解决方法
错误核心是client.embedColor的值为undefined,而EmbedBuilder.setColor()要求传入合法的颜色值(十六进制字符串、十进制数字、RGB数组或预定义颜色名),传入undefined会触发参数验证失败。
可以通过以下两种方式解决:
方法一:确保client.embedColor已正确配置
在机器人主文件或配置文件中,给client.embedColor赋值合法颜色值,示例:// 初始化client后设置 client.embedColor = "#2F3136"; // 十六进制颜色 // 或 client.embedColor = 0x2F3136; // 十进制颜色方法二:添加默认值兜底
修改代码中两处setColor(client.embedColor)为:.setColor(client.embedColor || "#2F3136")这样当
client.embedColor不存在时,会自动使用预设的默认颜色。
内容的提问来源于stack exchange,提问作者DarkThirsty
相关产品推荐
相关产品推荐

