Count members with role #948
-
Hey ! I'm looking for a while a method on Role "getMemberCount", for know the count of member with this role. I don't know if I didn't saw the method or there is no method for this :/ Thanks for help me ! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I would recommend joining the support server and asking in the help channel dedicated to using the library as its much quicker to get a response. However, to answer your question, you would have to to through all the members of the guild and check how many people have the role. |
Beta Was this translation helpful? Give feedback.
-
As @NovaFox161 said, you'll have to do this manually. Discord does not provide a way to do it. We could potentially consider a utility method for it, but it seems a little niche. Anyways, here's an example of how it might look. Mono<Long> countMembersWithRole(Guild guild, Snowflake roleId) {
return guild.getMembers()
.filter(member -> member.getRoleIds().contains(roleId))
.count();
} It's worth noting that if you want to do something like you showed in that picture, it would be inefficient to just loop over all of the roles and call the above method (especially if the members need to be requested and/or the guild is very large). Instead, you can get all of the counts at once, like so: Mono<Map<Snowflake, Long>> getRoleCounts(Guild guild) {
return guild.getMembers()
.flatMap(member -> Flux.fromIterable(member.getRoleIds()))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
} Also note that for either of the above solutions, you will need the |
Beta Was this translation helpful? Give feedback.
I would recommend joining the support server and asking in the help channel dedicated to using the library as its much quicker to get a response.
However, to answer your question, you would have to to through all the members of the guild and check how many people have the role.