Discord JDA - Invalid Member List

拈花ヽ惹草 提交于 2020-05-16 19:10:27

问题


I am creating a Discord bot and have encountered a strange problem. I need to go through each user on the server and perform a conditional action. But when receiving a list of all Members, it contains only me and the bot itself.

public class Bot extends ListenerAdapter {
    public void onGuildMessageReceived(GuildMessageReceivedEvent Event) {
        String Message = Event.getMessage().getContentRaw();

        if(Message.charAt(0) == Globals.BOT_PREFIX) {
            String[] Args = Message.split("\\s+");

        if(Args[0].equalsIgnoreCase(CommandType.COMMAND_DEV_TEST)) {
            List<Member> MemberList = Event.getGuild().getMembers();
            for(int i = 0; i < MemberList.size(); i++)
                System.out.println(MemberList.get(i));
        }
    }
}

If another person writes, then only me and the bot are still displayed.


回答1:


I assume you're using a development version of the 4.2.0 release (4.1.1_102 and above)

In these versions, the new factory methods have been introduced to make people aware of the new discord API design. In the future, bots will be limited to cache members who connected to voice channels by default.

The createDefault/createLight will only cache members connected to voice channels or owners of guilds (on first sight). To cache more members, you will have to enable the GUILD_MEMBERS intent in both the application dashboard for your bot and in JDA.

Now you can do something like this:

JDA api = JDABuilder.createDefault(token)
                    .setMemberCachePolicy(MemberCachePolicy.ALL)
                    .enableIntents(GatewayIntent.GUILD_MEMBERS)
                    .build();

The GUILD_MEMBERS intent is needed because it enables the GUILD_MEMBER_REMOVE dispatch to tell the library to remove a member from the cache when they are kicked/banned/leave.

This setup will perform lazy loading, which means it will start with only voice members and add more members to the cache once they become active.

To load all members on startup you have to additionally enable member chunking:

JDABuilder.createDefault(token)
          .setChunkingFilter(ChunkingFilter.ALL) // enable member chunking for all guilds
          .setMemberCachePolicy(MemberCachePolicy.ALL) // ignored if chunking enabled
          .enableIntents(GatewayIntent.GUILD_MEMBERS)
          .build();

See also:

  • MemberCachePolicy
  • ChunkingFilter
  • JDABuilder


来源:https://stackoverflow.com/questions/61226721/discord-jda-invalid-member-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!