How do I mention a role with Discord.js?
Asked Answered
A

4

11

I am making a bot and I am trying to Ping a certain role. Here is the relevant code:

let msga = msg.author;
msg.channel.send("@NES Found one!! " + msga);

@NES is the role I am trying to ping/mention.

Arsenopyrite answered 7/11, 2019 at 13:3 Comment(0)
E
5

Just add an opening and closing angle bracket and use the role id to make the mention.

msg.channel.send("<@id> Found one!! " + msga);

This answer was valid for older versions of discord.js (I believe v11 and under) but is now invalid for v12+ use yummypasta's solution for the newer versions.

Elissa answered 7/11, 2019 at 13:13 Comment(0)
M
42

The currently accepted answer is incorrect. You ping a user with <@id>, not a role.

As stated in this Github issue, for roles, you have to use <@&id> and the role has to be pingable.

So, the correct code for the question would be something like:

msg.channel.send("<@&" + roleId + "> Found one!! " + msga);

Or, using fancy formatted strings:

msg.channel.send(`<@&${roleId}> Found one!! ${msga}`);
Michalemichalski answered 30/5, 2020 at 5:0 Comment(2)
Correct answer but you have a space after the & and before the " which shouldn't be there in your first block of code. It should be: msg.channel.send("<@&" + roleId + "> Found one!! " + msga);Bugeye
My answer was correct at the time of posting, before v12 was released, updated my answer to point to this one.Elissa
E
5

Just add an opening and closing angle bracket and use the role id to make the mention.

msg.channel.send("<@id> Found one!! " + msga);

This answer was valid for older versions of discord.js (I believe v11 and under) but is now invalid for v12+ use yummypasta's solution for the newer versions.

Elissa answered 7/11, 2019 at 13:13 Comment(0)
A
2

It is:

 message.channel.send(`<@& id >`); 
Acme answered 2/7, 2020 at 23:11 Comment(0)
A
2

try this: msg.channel.send(`<@&${'roleId'}> Found one!! ${msga}`);

Angelo answered 29/1, 2021 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.