0

I want to be able to check if anyone in the whole server has a specific role. Is this possible?

Mihir Garg
  • 55
  • 5

1 Answers1

0

Role.members returns a collection of every cached member who has the given role. Note: you will need to update the required intents to use this correctly.

// get the role
const role = guild.roles.cache.get('role-id');

role.members.each((member) => console.log(`${member.username} has the role!`));
Lioness100
  • 8,260
  • 6
  • 18
  • 49
  • I get the error, undefined has the role (the code works but the role search did not work too well) – Mihir Garg Oct 29 '20 at 17:27
  • Did you enable the `GUILD_MEMBERS` intent? – Lioness100 Oct 29 '20 at 17:43
  • Not Sure, What is that? My code is this: ``` var ms = require('ms'); var Discord = require('discord.js'); const config = require("../config.json"); module.exports ={ name: 'testing', description: `testing cmnd`, async execute(message, args, client) { var guild = message.guild; var role = message.guild.roles.cache.find(r => r.name === "Admin"); role.members.each((member) => console.log(`${member.username} has the role!`)); } }; ``` – Mihir Garg Oct 29 '20 at 17:50
  • Check the post I linked in my original answer. – Lioness100 Oct 29 '20 at 18:31