Search code examples
node.jsdiscord.jsreplit

How to mention the author of a message in another message


I am using repl.it to develop a bot. I am trying to make a command that makes the bot behave like this:

Someone: !slap @someoneelse
Bot: @Someone slapped @someoneelse

How can I get the bot to mention @someone without using ID? Multiple people will use the command and I can't just use ID since it will only work with one person. I haven't found anything that helped me, and the documentation was no help either. Hopefully, I can get help! Thank you.


Solution

  • Users and members have a .toString() method that is automatically called every time they are concatenated with a string: that means that if you type "Hey " + message.author you will get "Hey @author"

    That's how I would do the command:

    // First store the mentioned user (it will be undefiend if there's none, check that)
    let mentionedUser = message.mentions.users.first();
    // Reply by directly putting the User objects in the string: 
    message.channel.send(`${message.author} slapped ${mentionedUser}`);