Renan Rk
Renan Rk

Reputation: 57

Is there a command to send private message to all members of a group?

Is there any way to make a command send a private message to all members of the discord group using discord.js?

Exemple: /private TEST

This message is sent to everyone in the group in private chat instead of channel chat.

Upvotes: 2

Views: 26591

Answers (2)

Jack Crispy
Jack Crispy

Reputation: 105

The updated code for discord.js v12 is just adding cache to the forEach.

client.on('message', msg => {
  if (msg.guild && msg.content.startsWith('/private')) {
    let text = msg.content.slice('/private'.length); // cuts off the /private part
    msg.guild.members.cache.forEach(member => {
      if (member.id != client.user.id && !member.user.bot) member.send(text);
    });
  }
});

Upvotes: 4

Federico Grandi
Federico Grandi

Reputation: 6806

You can iterate through Guild.members.
When you receive a message that starts with /private, you take the rest and send it to every member of the guild by using Guild.members.forEach().
Here's a quick example:

client.on('message', msg => {
  if (msg.guild && msg.content.startsWith('/private')) {
    let text = msg.content.slice('/private'.length); // cuts off the /private part
    msg.guild.members.forEach(member => {
      if (member.id != client.user.id && !member.user.bot) member.send(text);
    });
  }
});

This is just a basic implementation, you can obviously use this concept with your command checks or modify that by adding additional text and so on.

Hope this solves the problem for you, let me know if you have any further questions :)

Upvotes: 5

Related Questions