2
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)很陌生

4

6 回答 6

3

您可以使用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`);
    }
}); 
于 2020-01-24T07:36:29.343 回答
2

Discord.js使用许多习惯toString()User返回他的提及。因此,如果您想user在消息中提及一个,您可以这样做

这 :

`<@${user.id}>`

但更快的方法是:

`${user}`

user如果函数在您的字符串上运行 a ,您也可以简单地放置而不使用任何.toString()字符串。

像这样 :

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

会提到用户。

注意:它在 v13 中不再起作用。

于 2020-01-25T00:48:43.703 回答
1

如果您使用像这样的命令处理程序,这就是您将如何使其工作

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);  
    }
}
于 2021-07-28T16:26:22.570 回答
0

在这里,您可以使用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

希望这有帮助!

于 2020-09-21T12:44:44.670 回答
0

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

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`);
  }
})
于 2021-06-14T11:18:49.720 回答
0

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

'<@&' + user_id + '>'

注意@和&。

于 2022-02-25T09:25:22.327 回答