0
votes

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}!

3
Possible duplicate of How can I do string interpolation in JavaScript?user12251171

3 Answers

2
votes

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

`Hello ${message.author}!`
1
votes

Change this:

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

with this:

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

1
votes
message.channel.send(`Hello <@${message.author.tag}>!`)