Discord.js发送embed报embeds[0].description必填错误如何解决
问题原因
- 你定义的
utils.embed是异步函数,但调用时没有添加await关键字,导致传入embeds数组的是一个未完成状态的Promise对象,而非实际构造完成的MessageEmbed实例,Discord API无法识别其中的字段,就会抛出描述字段必填的错误。 - 你的embed工具函数本身没有返回值:你仅在
then()回调内返回了构造好的embed对象,但外层的异步函数没有将这个结果向外返回,即便后续添加了await,拿到的也会是undefined。
修复代码
第一步:修正embed工具函数
embed: async (title, thumbnail, desc, footer, color) => { const { MessageEmbed } = require("discord.js") // 向外返回fetch执行后的Promise结果 return client.user.fetch() .then(({ hexAccentColor }) => { const embed = new MessageEmbed() .setAuthor("GND Bot", client.user.avatarURL()) .setTitle(title) .setColor(color || hexAccentColor) .setDescription(desc) .setThumbnail(thumbnail || client.user.avatarURL()) .setFooter(footer) return embed }) }
第二步:修正命令内的调用逻辑
module.exports = { name: "test", desc: "A test command", cmd: async (interaction, utils) => { const { hexAccentColor } = await interaction.user.fetch() // 调用异步embed函数时添加await,拿到实际的embed实例 const testEmbed = await utils.embed( "Hello!", interaction.member.displayAvatarURL(), `Your name is <@${interaction.member.id}> and you're now in the special list! (jk)`, "", hexAccentColor ) await interaction.reply({ embeds: [testEmbed] }) } }
额外优化建议
你当前的代码混合了async/await和then回调写法,统一用async/await的写法可读性更高,维护起来也更方便。
内容的提问来源于stack exchange,提问作者Keilorus
相关产品推荐
相关产品推荐

