Search code examples
javascriptdiscorddiscord.js

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?


Solution

  • 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 + ">");
    });