Discord.js私聊发送Embed异常求助:普通消息正常但Embed显示[object Object]
Discord机器人私信发送Embed失败问题解决
问题根因
你得到[object Object]返回的核心原因是错误地将Embed对象用模板字符串包裹,强制将对象转为了普通字符串,导致Discord无法识别Embed结构。Discord.js的消息发送接口支持直接传入Embed对象,不需要额外做字符串格式化处理。
修正方案
仅需要修改发送Embed的对应行代码即可:
原错误代码
dUser.send(`${embed}`);
替换为正确写法
根据你使用的discord.js版本选择对应写法:
- 适用于v12版本:
dUser.send(embed);
- 适用于v13及以上版本:
dUser.send({ embeds: [embed] });
额外优化建议
可以添加错误捕获逻辑,避免目标用户关闭了私信权限时机器人崩溃:
dUser.send(embed) .catch(() => message.channel.send(`无法向该用户发送私信,请检查对方隐私设置`))
完整修正后代码(v12版本示例)
module.exports = { name: 'costietare', description: "这是一个帮助命令", async execute(client, message, args, Discord) { message.delete({timeout: 10}); let dUser = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]); if (!dUser) return message.channel.send("无法找到指定用户!"); if (!message.member.hasPermission('ADMINISTRATOR')) return message.reply("权限不足!"); let embed = new Discord.MessageEmbed() .setTitle('通用信息') .setColor('YELLOW') .addFields( {name: '开发者', value: '[m1](https://steamcommunity.com/id/catshvh)', inline:false}, {name: '!commands', value: '查看更多机器人命令及用法', inline:true}, {name: '购买/支持', value: "[点击购买](https://discord.gg/4Qt3238jCy)", inline:true}, ) .setTimestamp() dUser.send(`Sunt tare, stiu`); // 修改后的发送代码 dUser.send(embed) .catch(() => message.channel.send(`无法向该用户发送私信,请检查对方隐私设置`)); message.author.send( `${message.author} 你已成功向 ${dUser} 发送消息` ); } }
内容的提问来源于stack exchange,提问作者Ecstasyyy.
相关产品推荐
相关产品推荐

