Discord API无法正确检索服务器中的成员数量。

huangapple go评论70阅读模式
英文:

Discord API not able to retrieve correct amount of members in Guild

问题

Method #1:

public static List<Member> getMembers() {
    return Arrays.stream(WebClient.builder().build()
        .get()
        .uri(URL)
        .header("Authorization", "Bot " + Credentials.TOKEN)
        .retrieve()
        .bodyToMono(Member[].class)
        .block()).collect(Collectors.toList());
}

This method only retrieves the last user that joined the server.

Method #2:

Guild guild = event.getJDA().getGuildById(GUILD_ID);

event.getChannel().sendMessage("Users: " + guild.getMembers().size()).queue();

This method retrieves only 2 users. While debugging, it was found that it's retrieving the bot itself and you.

Method #3:

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    String messageSent = event.getMessage().getContentRaw();

    if (messageSent.equalsIgnoreCase("members")) {
        Guild guild = event.getJDA().getGuildById(GUILD_ID);

        event.getChannel().sendMessage("Users: " +
            guild.getMembers().size()).queue();            

        event.getChannel().sendMessage("Thats all for now").queue();
    }
}

This method behaves the same as Method #2.

Method #4:

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    String messageSent = event.getMessage().getContentRaw();

    event.getChannel().getMembers().forEach(member -> {
        event.getChannel().sendMessage("[Channel - " + event.getChannel().getName() + " ]").queue();

        event.getChannel().sendMessage("---- Members -> " + member.getUser().getName()).queue();
    });
}

This method behaves the same as Method #2 and #3.

It seems that in Methods #2, #3, and #4, you are encountering issues where it only retrieves the bot and yourself. This might be due to cached members. The documentation suggests that Guild.getMembers() will only check cached members.

To get all the users in a Guild or a channel, you might want to consider fetching members explicitly to ensure you get the complete list of members, even those not currently cached. You can use the retrieve() method to fetch members as follows:

Guild guild = event.getJDA().getGuildById(GUILD_ID);
guild.loadMembers().onSuccess(members -> {
    event.getChannel().sendMessage("Users: " + members.size()).queue();
});

This code will load all the members in the guild, and then you can access them without encountering the caching issue.

英文:

I've tried to retrieve all users in a Guild or a channel. I've tried different ways but without any desired result.

Method #1.

public static List&lt;Member&gt; getMembers() {
    return Arrays.stream(WebClient.builder().build()
        .get()
        .uri(URL)
        .header(&quot;Authorization&quot;, &quot;Bot &quot; + Credentials.TOKEN)
        .retrieve()
        .bodyToMono(Member[].class)
        .block()).collect(Collectors.toList());
}

This only retrieves the last user that joined the server.

Method #2

Guild guild = event.getJDA().getGuildById(GUILD_ID);

event.getChannel().sendMessage(&quot;Users: &quot; + guild.getMembers().size()).queue();

This retrieves only 2 users. While debugging I found that it's retrieving the bot itself and me.

Method #3

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    String messageSent = event.getMessage().getContentRaw();
    
    if (messageSent.equalsIgnoreCase(&quot;members&quot;)) {
        Guild guild = event.getJDA().getGuildById(GUILD_ID);

        event.getChannel().sendMessage(&quot;Users: &quot; +
            guild.getMembers().size()).queue();            
    
        event.getChannel().sendMessage(&quot;Thats all for now&quot;).queue();
    }
}

Same as #2.

Method #4

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    String messageSent = event.getMessage().getContentRaw();
        
    event.getChannel().getMembers().forEach(member -&gt; {
        event.getChannel().sendMessage(&quot;[Channel - &quot; + event.getChannel().getName() + &quot; ]&quot;).queue();
        
        event.getChannel().sendMessage(&quot;---- Members -&gt; &quot; + member.getUser().getName()).queue();
    });
}

Same as #2 and #3

When debugging I found that like #2 in #3 and #4 it only retrieves the bot and me.

Sometimes if another users executes the command, the bot retrieves that user and the bot itself.

> According to the documentation GuildChannel.getMembers() returns all
> Members with the {@link net.dv8tion.jda.api.Permission#MESSAGE_READ}
> Permission

So, I have give all users the permission to read messages in my test server.

> The documentation also says that Guild.getMembers() will only check
> cached members!

What am I doing wrong?
What should I do to be able to get all the users in a Guild or a channel?

Thanks in advance.

答案1

得分: 1

event.getJDA().getGuildById(GUILD_ID).getMemberCount();

这个回答 中所述,你可以启用特权的服务器成员终端点并缓存所有成员。然而,如果未经验证,你的机器人最多只能加入 100 个服务器。

这个数量是精确的,但是缓存所有公会中的所有成员也会占用大量 RAM。

一个更好的解决方案是使用 公会终端点 中的成员数量。你可以向 /guilds/<id>?with_counts=true 发送 GET 请求,并使用 approximate_member_count

甚至 JDA 也使用了这个终端点。
Guild 有一个方法 getMemberCount 可以获取该公会(近似的)成员数量。


<details>
<summary>英文:</summary>

# TL;DR

    event.getJDA().getGuildById(GUILD_ID).getMemberCount();

---

As described in [this answer](https://stackoverflow.com/a/61229594/10871900), you could enable the privileged server members endpoint and cache all members. Your bot can not join more than 100 servers without being verified then, however.

This amount is exact but caching all members in all guilds will also use up much RAM.

A way better solution is to use the count from [the guild endpoint](https://discord.com/developers/docs/resources/guild). You can send a GET request to `/guilds/&lt;id&gt;?with_counts=true` and use `approximate_member_count`.

JDA even uses this endpoint.
`Guild` has a method [`getMemberCount`](https://javadoc.io/static/net.dv8tion/JDA/4.2.0_203/net/dv8tion/jda/api/entities/Guild.html#getMemberCount()) that gives you the (approximate) number of members in this guild.

</details>



huangapple
  • 本文由 发表于 2020年9月6日 02:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63757065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定