如何在Discord.js的Embed消息中@提及消息发送者与被指定用户
解决方案
问题出在两处:
- 你在拼接标题时仅调用了被抱抱用户的
username属性,只会输出纯文本用户名,没有生成提及格式 - 如果需要提及能触发用户的ping通知,需要额外配置发送参数
修改后的代码如下(适配Discord.js v13及以上版本):
const personHugged = message.mentions.users.first(); if(personHugged){ let hugEmbed = new Discord.MessageEmbed() // 把personHugged.username改为直接用personHugged对象,自动转成可渲染的提及格式 .setTitle(`${message.author} hugged ${personHugged} :heart:`) .setImage(images[Math.floor(Math.random()*images.length)]) .setTimestamp() // 发送时添加allowedMentions配置,允许提及embed里的用户,才会触发ping通知 message.channel.send({ embeds: [hugEmbed], allowedMentions: { parse: ['users'] } }); } else{ message.channel.send(`Sorry ${message.author} that user is not in this server!`); }
如果你使用的是Discord.js v12版本,发送参数的写法改为:
message.channel.send(hugEmbed, { allowedMentions: { parse: ['users'] } });
如果不需要触发ping通知,仅需要Embed里显示可点击的@提及样式,只修改setTitle那一行即可。
内容的提问来源于stack exchange,提问作者user15855756
相关产品推荐
相关产品推荐

