Discord音乐机器人运行lyrics命令报DiscordAPIError空消息错误如何解决
问题根因与修复方案
你代码里共有4处导致空消息报错的问题,按优先级排序如下:
- 首先是Discord.js版本兼容问题:v13及以上版本发送Embed不能直接传入Embed实例,必须包装为
{ embeds: [Embed实例] }格式,你直接传入Embed对象会被识别为要发送的纯文本内容,无法转为有效字符串就触发空消息报错。 - 第二是lyrics变量赋值语法错误:
lyrics =No lyrics found for ${title}., { title: title }这里用了逗号表达式,最终lyrics被赋值为后面的对象而非你需要的提示字符串,对象转字符串后会显示无效内容。 - 第三是title变量赋值错误:
const title = player.queue.current这里取的是完整的曲目对象而非曲目名字,直接插入字符串会得到[object Object]无效内容,应该取title属性。 - 第四是setTitle方法传参错误:
setTitle(${title} - Lyrics, { title: title })第二个参数是无效参数,无任何作用。
修复后的完整代码
const { MessageEmbed } = require("discord.js"); const lyricsFinder = require("lyrics-finder"); module.exports = { name: "lyrics", aliases: ['ly'], category: "Music", description: "View the lyrics of a song", args: false, usage: "", permission: [], owner: false, player: true, inVoiceChannel: true, sameVoiceChannel: true, execute: async (message, args, client, prefix) => { const player = message.client.manager.get(message.guild.id); if (!player.queue.current) { let thing = new MessageEmbed() .setColor("RED") .setDescription("There is no music playing."); return message.channel.send({embeds: [thing]}); } let lyrics = null; const title = player.queue.current.title; try { lyrics = await lyricsFinder(title, ""); if (!lyrics) lyrics = `No lyrics found for ${title}.`; } catch (error) { lyrics = `No lyrics found for ${title}.`; } let lyricsEmbed = new MessageEmbed() .setTitle(`${title} - Lyrics`) .setDescription(lyrics) .setColor("#F8AA2A") .setTimestamp(); if (lyricsEmbed.description.length >= 2048) { lyricsEmbed.description = `${lyricsEmbed.description.substr(0, 2045)}...`; } return message.channel.send({embeds: [lyricsEmbed]}).catch(console.error); } };
内容的提问来源于stack exchange,提问作者Anto. B
相关产品推荐
相关产品推荐

