TimeToLearn
TimeToLearn

Reputation: 63

Discord.js How to mention message author?

I searched a lot for the answer, but I didn't find and/or understand it. How can I mention the message author in this code?

It just says:

${user.username} pong

But I want:

@(msg-author-username) pong

Upvotes: 5

Views: 16881

Answers (1)

PerplexingParadox
PerplexingParadox

Reputation: 1196

There's actually a really simple function that's already in the discord.js module that provides that to you.

Instead of writing ${user.username} pong, you can do message.reply('pong')... however this formats it into @user, pong. So it adds a comma.

An alternate method is to simply do message.channel.send('<@' + message.author.id + '> pong'); in order to not use the comma. But I prefer message.reply simply because it's a lot less typing to deal with lol.

To align the alt method closer to your code, you could consider setting user to message.author rather than message.author.name since that's just the nickname object instead. Then, from that, you can simply do message.channel.send(`<@${user.id}> pong`);

So it should look like:

const user = message.author;
return message.channel.send(`<@${user.id}> pong`);

Another method:

const user = message.author;
return message.channel.send(`${user} pong`);

Hope this helps!

Upvotes: 6

Related Questions