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

1

3 Answers 3

2

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

`Hello ${message.author}!`
Sign up to request clarification or add additional context in comments.

Comments

1

Change this:

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

with this:

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

Comments

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

1 Comment

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? Is one more reliable? Does this fix a bug? Or is it just a stylistic preference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.