Discord.js v13发送Embed时报Cannot send an empty message错误如何解决
错误触发原因
- Discord.js v13版本已经正式废弃了原有
message事件,替换为messageCreate事件。你仍在监听已废弃的message事件时,事件回调无法拿到完整有效的消息上下文对象,导致调用channel.send方法时传入的embed参数未被正确序列化传递,Discord API接收不到有效消息内容,就会返回50006空消息错误。 - 补充:你粘贴的代码片段存在缺失,缺少事件回调的闭合括号、Bot登录逻辑也会导致上下文异常,触发同样的报错。
修复方案
- 替换监听的事件名称,将
client.on("message", function (message) {修改为client.on("messageCreate", function (message) { - 登录Discord开发者后台,进入你的Bot应用设置页,在「Privileged Gateway Intents」分类下开启
Message Content Intent权限,否则Bot无法正常读取服务器消息内容,触发逻辑异常。 - 补全代码缺失的闭合符号和登录逻辑,确保代码可正常执行。
修正后的参考代码如下:
const { Client, Intents, MessageEmbed } = require("discord.js"); const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.MESSAGE_CONTENT // 新增消息内容意图 ] }); client.on("messageCreate", function (message) { // 可添加判断避免Bot自己触发自己的消息 if (message.author.bot) return; const exampleEmbed = new MessageEmbed() .setColor('#0099ff') .setTitle('Some title') .setURL('https://discord.js.org/') .setAuthor('Some name', 'https://i.imgur.com/AfFp7pu.png', 'https://discord.js.org') .setDescription('Some description here') .setThumbnail('https://i.imgur.com/AfFp7pu.png') .addFields( { name: 'Regular field title', value: 'Some value here' }, { name: '\u200B', value: '\u200B' }, { name: 'Inline field title', value: 'Some value here', inline: true }, { name: 'Inline field title', value: 'Some value here', inline: true }, ) .addField('Inline field title', 'Some value here', true) .setImage('https://i.imgur.com/AfFp7pu.png') .setTimestamp() .setFooter('Some footer text here', 'https://i.imgur.com/AfFp7pu.png'); message.channel.send({ embeds: [exampleEmbed] }); }); // 替换为你自己的Bot Token完成登录 client.login('你的Bot Token');
内容的提问来源于stack exchange,提问作者XXfedeXX
相关产品推荐
相关产品推荐

