简体   繁体   中英

How to tag users using Discord.JS?

I am looking to create a command in order to tag certain users automatically using their username eg. "@RYAN#9602" whenever a switch statement case is executed. Currently the problem that I'm experiencing in that whenever I do try to tag users, it just writes "@RYAN#9602" into the text channel and doesn't actually tag them.

This is what I have tried:

var players = [
"@RYAN#9602"
]



switch(args[0].toLowerCase()){

 case "play":
            message.channel.send(players.join('\n'));
            break;
}

So in summary, using Discord.JS, how do I make the bot actually tag the user so that they will get 'pinged' instead of just sending a message of their name into the text channel?

Thanks very much in advance!

You have two options.

You can either use the toString method on the User object, or form the mention yourself using the user's ID.

Here's an example using toString :

client.on("message", => {
    const channel = message.channel;
    channel.send(message.author.toString());
});

And here's an example using the ID

client.on("message", => {
    const channel = message.channel;
    channel.send("<@" + message.author.id + ">");
});

Try using the toString method.

client.on("message", => {
    const channel = message.channel;
    channel.send(message.author.toString());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM