简体   繁体   中英

The JDA#getGuilds() method is returning an empty list

So I am experiencing things with the discord JDA api. I tried to get my bot's servers but it doesn't work at all.

public static void main(String[] args) {
        JDABuilder jdab = JDABuilder.create("token",GatewayIntent.DIRECT_MESSAGES);
        JDA jda = null;
        try { jda = jdab.build();
        } catch (LoginException e) { e.printStackTrace(); }
        assert jda != null;
        System.out.println(jda.getGuilds().size());
        for(Guild g : jda.getGuilds()) {
            System.out.println(g.getName());
        }
   }

But in fact it doesn't show anything but 0, meaning the getGuilds() method's list is empty.

Any help would be appreciated, and thanks for any answer

The documentation for JDABuilder#build states:

The login process runs in a different thread, so while this will return immediately, JDA has not finished loading, thus many JDA methods have the chance to return incorrect information. For example JDA.getGuilds() might return an empty list or JDA.getUserById(long) might return null for arbitrary user IDs.

If you wish to be sure that the JDA information is correct, please use JDA.awaitReady() or register an EventListener to listen for the ReadyEvent.

So all you gotta do is call awaitReady() to block the main thread until JDA is ready. Only then can you reliably access the JDA cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM