Javascript 如何在discord.js中获取您在消息中ping的人的用户名

Javascript 如何在discord.js中获取您在消息中ping的人的用户名,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,我正在编写一个代码,其中发送一个嵌入discord.js的拥抱 这是我的代码: (前缀设置为-->` if(message.content==前缀+拥抱){ 返回 } }); client.on(“message”,message=>{ const user=message.author.username; const bid=(message.content.replace(前缀+“拥抱”,“拥抱”); const rex=bid.replace(“@!”,”); 常量rl=rex.repla

我正在编写一个代码,其中发送一个嵌入discord.js的拥抱

这是我的代码: (前缀设置为-->`

if(message.content==前缀+拥抱){
返回
}
});
client.on(“message”,message=>{
const user=message.author.username;
const bid=(message.content.replace(前缀+“拥抱”,“拥抱”);
const rex=bid.replace(“@!”,”);
常量rl=rex.replace(“,”);
const idtoname=client.users.cache.get(rr);
if(message.author.bot)返回;
if(message.content==前缀+拥抱)返回;
if(message.content.startsWith(前缀+“拥抱”)){
const hugaembed=new Discord.MessageEmbed()
.setTitle(用户+“hugs”+idtoname.username)
.setColor(0xbc13fe)
.setImage(“https://www.dropbox.com/s/450lk7apiuf0uk0/huggoooo.gif?dl=1");
message.channel.send(hugembed);
}
});
代码基本上是发送一个带有拥抱gif的嵌入,标题是(message.author.username)“hugs”(我ping的人)

问题是,我正在使用发件人的消息,并替换除id之外的所有内容,并将id转换为用户名

但是当有人发送消息前缀+拥抱(没有ping,只有文本)前缀+拥抱(超过1 ping)时,问题就会出现,在这两种情况下,bot都会崩溃


是否有任何方法可以将我使用“拥抱”命令ping的人的用户名存储在变量中,并使用该用户名而不是替换消息本身?

定义提及,无需检查消息内容,已经有
消息。提及
(提及=pings)

只检查文本,没有提及

if (!mentionedUser) {
   return message.channel.send('Please mention a user')
}
使用数据

const hugembed = new Discord.MessageEmbed()
      .setTitle(`${user} hugs ${mentionedUser.username}`)
      .setColor(0xbc13fe)
      .setImage("https://www.dropbox.com/s/450lk7apiuf0uk0/huggoooo.gif?dl=1");
      message.channel.send(hugembed);
试试这个:

client.on ("message", message => {
  if(message.author.bot) return;
  const user = message.author.username;

  if (message.content.startsWith(prefix + "hug")) {
    const mentioneduser = message.mentions.users.first();
    if(!mentionuser) return message.channel.send(`You need to mention a user to give a hug to!`)
  
      const hugembed = new Discord.MessageEmbed()
      .setTitle(`${user} hugs ${mentioneduser.user.username}`)
      .setColor(0xbc13fe)
      .setImage("https://www.dropbox.com/s/450lk7apiuf0uk0/huggoooo.gif?dl=1");
      message.channel.send(hugembed);  
  }
});

这解决了当两个用户ping时bot崩溃的问题,但在编写文本而不是提及时也会遇到同样的问题。我已经编辑了我的答案以包含文本处理。如果您还想在服务器上显示该用户的昵称,您可以执行
${sindeduser.displayName}
const hugembed = new Discord.MessageEmbed()
      .setTitle(`${user} hugs ${mentionedUser.username}`)
      .setColor(0xbc13fe)
      .setImage("https://www.dropbox.com/s/450lk7apiuf0uk0/huggoooo.gif?dl=1");
      message.channel.send(hugembed);
client.on ("message", message => {
  if(message.author.bot) return;
  const user = message.author.username;

  if (message.content.startsWith(prefix + "hug")) {
    const mentioneduser = message.mentions.users.first();
    if(!mentionuser) return message.channel.send(`You need to mention a user to give a hug to!`)
  
      const hugembed = new Discord.MessageEmbed()
      .setTitle(`${user} hugs ${mentioneduser.user.username}`)
      .setColor(0xbc13fe)
      .setImage("https://www.dropbox.com/s/450lk7apiuf0uk0/huggoooo.gif?dl=1");
      message.channel.send(hugembed);  
  }
});