Discord.js提到

Discord.js mentioning

client.on('message', message => {
     if (message.content === `L!hug`) {
        if (!message.mentions.users.size) {
            return message.reply('you need to tag a user in order to hug them!!');

            const taggeduser = message.mentions.users.first();
        }
        // message goes below!
         message.channel.send(userID + ` you just got a hug  https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
    }
}); 

我尝试了一些想法,我对这个语言库还很陌生(discord.js)

您可以使用message.mentions.members.first()

client.on('message', message => {
     if (message.content.startsWith('L!hug')) { 
    let targetMember = message.mentions.members.first();
    if(!targetMember) return message.reply('you need to tag a user in order to hug them!!');
        // message goes below!
         message.channel.send(`<@${targetMember.user.id}> you just got a hug  https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
    }
}); 

Discord.js 使用了很多习惯 toString()User return 他的提及。 因此,如果您想在消息中提及 user,您可以执行

这个:

`<@${user.id}>`

但更快的方法是:

`${user}`

你可以简单地把 user 放在没有任何字符串的情况下,如果函数在你的字符串上运行 .toString(),它也可以工作。

像这样:

message.channel.send(user + " has made something");

会提到用户。

Note : It won't work anymore in v13.

这里可以使用message.mentions.users.first():

let user = message.mentions.users.first(); // As a shortcut to it

// Check if there is actually a mention
if(!user) {
  return message.reply("You need to tag a user in order to hug them!");
}

// If there is a user, do this code
message.channel.send(`${user.toString()}, you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
// user.toString() will convert the user object to a mention

希望对您有所帮助!

这是代码的简单版本。未定义 userID,这意味着您需要使用目标成员。在这里,这是应该的。

client.on('message', message => {
  if (message.content === "L!hug") {
    const targetmember = message.mentions.members.first()
    if (!targetmember) return message.reply("you need to tag a user in order to hug them!!");
    message.channel.send(`${targetmember} you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
  }
})

如果您使用这样的命令处理程序 这就是你让它工作的方式

module.exports = {
    name: "hug",
    description: "@ someone to hug them through the bot command.",
    nsfw: false,
    execute(message, args){
        const targetmember = message.mentions.members.first()
        if (!targetmember) return message.reply("you need to tag a user in order to hug them.");
        var huggifs = [`${targetmember} Recieved a hug https://imgur.com/r9aU2xv`, `${targetmember} Recieved a hug https://tenor.com/LUqw.gif`, `${targetmember}  Recieved a hug https://media.giphy.com/media/3ZnBrkqoaI2hq/giphy.gif`, `${targetmember} Recieved a hug https://tenor.com/1jRF.gif`, `${targetmember} Recieved a hug https://media.giphy.com/media/lrr9rHuoJOE0w/giphy.gif`]
        var hugrandomform = huggifs[Math.floor(Math.random()*huggifs.length)];
        message.channel.send(hugrandomform).then().catch(console.error);  
    }
}

您可以使用它来创建提及(请注意,它在 pre 中不起作用,因为其中没有提及)

'<@&' + user_id + '>'

注意@和&。