提问者:小点点

不和谐的JDA加载成员(). get()不适合我:(


我想获得关于我的不和的所有成员的数量的信息。示例:

Guild guild= jda.getGuildById(GUILD_ID);
List<Member> members = guild.loadMembers().get();

我启用了GatewayIntent。GUILD_MEMBERS:

jda = JDABuilder.createDefault(botToken)
                    .enableIntents(GatewayIntent.GUILD_MEMBERS)
                    .setMemberCachePolicy(MemberCachePolicy.ALL)
                    .build();

[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)

我在留档发现:

您不得使用诸如Task. get()之类的阻塞操作!默认情况下,响应处理发生在事件线程上。

但是我如何检查和应用它呢?

我做错了什么?


共1个答案

匿名用户

你必须使用回调:

guild.loadMembers().onSuccess(members -> {
  // run your code inside here
  for (Member member : members) {
    System.out.println(member.getEffectiveName());
  }
});

您还可以使用load会员(…)作为异步for循环:

guild.loadMembers(member -> System.out.println(member.getEffectiveName()));

如果您启用公会成员分块(不建议用于超过100个公会的大型机器人),您可以使用get会员()代替:

jda = JDABuilder.createDefault(botToken)
                    .enableIntents(GatewayIntent.GUILD_MEMBERS)
                    .setChunkingFilter(ChunkingFilter.ALL)
                    .setMemberCachePolicy(MemberCachePolicy.ALL)
                    .build();
List<Member> members = guild.getMembers();