cryofgaming
cryofgaming

Reputation: 23

JDA 4.2 how to get all users that have a specific role?

I'm looking to receive all discord users from a guild that have a specific role.

Since .getGuild().getMembersWithRoles([role]); only search cached users I don't know how to do it. Note, the discord server has multiple thousand users so caching everything would be bad.

I searched for something like: JDABot.retrieveUserByRole([role]); but didn't find anything.

JDA Version: 4.2.0_214-withDependencies

Upvotes: 2

Views: 2072

Answers (1)

MrBorder
MrBorder

Reputation: 369

You'll want to use Guild#findMembers(Predicate)

It'll look something like this:

List<Member> members = guild.findMembers(member -> {
    if (member.getRoles().contains(role)){
        return true;
    }
    return false;
}).get();

Read more about Guild#findMembers(Predicate) here: https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/entities/Guild.html#findMembers(java.util.function.Predicate)

Upvotes: 1

Related Questions