Search code examples
javascriptdiscorddiscord.js

How to get Bot to respond with the username?


This is my first time coding seriously and I'm trying to set up my own discord bot. I'm trying to set up a simple response system but where it will answer with the username of the author but everything I've tried has not worked. For example, if i try ${message.author} in the response it will say "hello ${message.author}! instead of giving the username. I've included what I have written so far, I'm hoping someone more skilled than me can tell me what im doing wrong. Basic discord bot code

I've tried ${message.author}, ${user}, ${msg.author.tag}, {msg.author.tag}, {msgauthortag}, ${user.id}, ${user.username}, {user.id}, {message.author.name}. I want the bot to respond with the name or username of the person who wrote the message.


Solution

  • You must switch the apostrophes (') around the string to backticks (`). Also according to the DJS docs, to reply with the username of the user it should be message.author.username. Your code should look something like this:

    message.reply(`hello ${message.author.username}!`)
    

    For more information about template literals, you can read the Mozilla docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals