lost
lost

Reputation: 19

How to send a private message to members of a specific role not in a voice channel?

This is the code I use to count members with a specific role whom are in a voice channel. I want to send a private message to members with the role and not in a voice channel. How can I do so?

var takaci = 0;
let a = client.guilds
    .get(sunucuid)
    .roles.get(rolid)
    .members.filter(o => o.voiceChannel).size;
let ses = a;

Upvotes: 0

Views: 60

Answers (1)

slothiful
slothiful

Reputation: 5623

  1. Inverse your predicate function in Collection.filter() to acquire only those members with the role and not in a voice channel. Use the ! character, or logical NOT operator.

  2. Iterate over your Collection.

  3. Use GuildMember.send() to send a direct message to each member individually.

const membersToMsg = client.guilds.get(sunucuid).roles.get(rolid).members.filter(m => !m.voiceChannel);

for (const [, member] of membersToMsg) {
  member.send('Hello, world!')
    .catch(console.error); // 'Cannot send messages to this user' is most likely due to privacy settings
}

I'm using a for...of loop in this code rather than Map.forEach() because the latter will simply fire promises and move on, possibly resulting in uncaught rejections.

Upvotes: 1

Related Questions