I'm coding a discord Bot with the discord.js library on a nodeJs server. Here is my question: is that possible when an event occure (like someone sending a message) that the bot answers to members of a role, or to everyone. The message.reply("my reply") method only answer to the author of the message as I use it for now...
-
You can find all the members of an role and make a foreach to all members in that role – Cursed Oct 09 '19 at 16:50
-
Possible duplicate of [Sending private message to a group of user (Discord.js)](https://stackoverflow.com/q/56263758/1092820) – Ruzihm Oct 09 '19 at 16:52
-
I understand how to do that but what I don't get is how to send the message to the role member through the reply method. – LucasF Oct 09 '19 at 16:53
-
on a foreach you will have the member promise and you can make member.reply – Cursed Oct 09 '19 at 16:55
-
You're asking an [XY question](https://meta.stackexchange.com/a/66378/405359). Why bother using `msg.reply` here if it's not going to be of any use to you? [The accepted answer at the proposed duplicate](https://stackoverflow.com/a/56264694/1092820) does exactly what you need, you just need to put it in a `client.on("message", msg => {...});` call, and get the guild from `message.guild` or whatever. – Ruzihm Oct 09 '19 at 17:00
-
You teach me something ( I didn't know what is a XY Question), I saw the previous answer you posted and it half answer to my question. I now know how to target users with a private message but not how to send a message on a channel, mentionning to every member of the role. – LucasF Oct 09 '19 at 17:23
-
`message.channel.send(member+"Your message")` – Cursed Oct 10 '19 at 09:21
1 Answers
Your problem is that message.reply()
is a very limited method: it always mentions the author of the message, and you can't override that.
You need to use a more generic method, channel.send()
, and construct the mention yourself. .reply()
is just a shortcut for a frequently used form of it, but you need something custom.
Presumably, you want it to happen in the same channel as the message, so you need message.channel.send("Whatever content you want")
.
Now, to add a role, you need to decide how to pick one. Is it fixed? Then you could hard-code a role mention by role ID: <@&134362454976102401>
(of course, it needs to be the role ID you require).
If you want to find a role, for example by name, you need to do that via searching through the guild in question. You can access it though message.guild
, but be warned that it will be undefined for DMs.
Then you can do something like
const role = message.guild.roles.find(role => role.name === "NameYouWant");
message.channel.send(`${role} something something`);
because Role objects become mentions when converted to strings.

- 74,770
- 16
- 179
- 206
-
Thanks Xan, this perfectly answers to my problem. I have already retrieved the role through the guild.I think I would have hardly find by myself that roles convertion turns them into strings so the last part of code is of great use. – LucasF Oct 12 '19 at 21:56
-
@Xan please see [my question](https://stackoverflow.com/q/58541634/11253551) – Codey Oct 27 '19 at 07:46