You need to enable JavaScript to run this app. 如何在Discord.js的Embed消息中@提及消息发送者与被指定用户
优惠活动
大模型
产品
解决方案
定价
更多

如何在Discord.js的Embed消息中@提及消息发送者与被指定用户

解决方案

问题出在两处:

  1. 你在拼接标题时仅调用了被抱抱用户的username属性,只会输出纯文本用户名,没有生成提及格式
  2. 如果需要提及能触发用户的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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 07:39:03