0

I'm creating a discord bot in NodeJS using discord.js module and I want to send a predefined message in the same channel where a user sends a particular text command. eg.

const token = 'xyz';
const client = new Discord.Client();

client.on('message', (message) => {
    if (message.content === '!hi') {
        message.channel.send('Hello ${message.author}!');
    };
});

client.on('ready', () => {
    console.log('Bot is now connected');

    //  client.channels.find(x => x.name === 'test').send('Hello I\'m now connected.');
});

client.login(token);```

client.on('message', (message) => {
    if (message.content === '!hi') {
        message.channel.send('Hello ${message.author}!');    }});

I expect the output to be Hello @discordusername! but instead, I'm getting Hello ${message.author}!

Goran Stoyanov
  • 2,311
  • 1
  • 21
  • 31
user8903551
  • 15
  • 1
  • 6
  • 1
    Possible duplicate of [How can I do string interpolation in JavaScript?](https://stackoverflow.com/questions/1408289/how-can-i-do-string-interpolation-in-javascript) –  Nov 02 '19 at 14:22

3 Answers3

2

For string interpolation in javascript use backticks instead of single quotes. Ex:

`Hello ${message.author}!`
Ethan Kelley
  • 113
  • 1
  • 8
1

Change this:

message.channel.send('Hello ${message.author}!');

with this:

message.channel.send(`Hello ${message.author}!`);

Goran Stoyanov
  • 2,311
  • 1
  • 21
  • 31
1
message.channel.send(`Hello <@${message.author.tag}>!`)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Oliver
  • 11
  • 1
  • What's the benefit to including `.tag` property and `<@ … >` wrapper, as opposed to the otherwise identical syntax proposed in [@Gorgan-Stoyanov's answer from last year](https://stackoverflow.com/a/58672023/3025856)? Is one more reliable? Does this fix a bug? Or is it just a stylistic preference? – Jeremy Caney Nov 08 '20 at 20:34