Reputation: 13
I want to get information about the number of all members of my discord. Example:
Guild guild= jda.getGuildById(GUILD_ID);
List<Member> members = guild.loadMembers().get();
I enabled GatewayIntent.GUILD_MEMBERS:
jda = JDABuilder.createDefault(botToken)
.enableIntents(GatewayIntent.GUILD_MEMBERS)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.build();
In application dashboard under the Privileged Gateway Intents section, i set enable SERVER MEMBERS INTENT and MESSAGE CONTENT INTENT.
Unfortunately it still doesn't work. Error message:
[main] INFO net.dv8tion.jda.api.JDA - Login Successful!
[JDA MainWS-ReadThread] INFO net.dv8tion.jda.internal.requests.WebSocketClient - Connected to WebSocket
[JDA MainWS-ReadThread] INFO net.dv8tion.jda.api.JDA - Finished Loading!
java.lang.UnsupportedOperationException: Blocking operations are not permitted on the gateway thread
at net.dv8tion.jda.internal.utils.concurrent.task.GatewayTask.get(GatewayTask.java:96)
at Main.onMessageReceived(Main.java:277)
I found in the documentation:
You MUST NOT use blocking operations such as Task.get()! The response handling happens on the event thread by default.
But how do I check it and apply it?
What am I doing wrong?
Upvotes: 0
Views: 647
Reputation: 6131
You have to use a callback:
guild.loadMembers().onSuccess(members -> {
// run your code inside here
for (Member member : members) {
System.out.println(member.getEffectiveName());
}
});
You can also use loadMembers(...)
as an async for-loop:
guild.loadMembers(member -> System.out.println(member.getEffectiveName()));
If you enable guild member chunking (not recommended for larger bots of more than 100 guilds), you can use getMembers()
instead:
jda = JDABuilder.createDefault(botToken)
.enableIntents(GatewayIntent.GUILD_MEMBERS)
.setChunkingFilter(ChunkingFilter.ALL)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.build();
List<Member> members = guild.getMembers();
Upvotes: 0