Search code examples
javalistenerdiscord-jda

Discord jda bot || UserUpdateActivitiesEvent does not trigger


I tried tracking if people on the bots guild change activities (like starting to play a game)

After reading the javadoc I found out:

  • GatewayIntent.GUILD_PRESENCES
  • CacheFlag.ACTIVITY
  • MemberCachePolicy.ONLINE (therefore GatewayIntent.GUILD_MEMBERS)

must be active.

so thats my Main:

        JDABuilder builder = JDABuilder.createDefault(token);
        builder.enableIntents(GatewayIntent.GUILD_PRESENCES);
        builder.enableIntents(GatewayIntent.GUILD_MEMBERS);
        builder.enableCache(CacheFlag.ACTIVITY);
        builder.setMemberCachePolicy(MemberCachePolicy.ONLINE);
        builder.setChunkingFilter(ChunkingFilter.ALL);
        this.jda = builder.build();
        jda.addEventListener( new ActivityListener(jda));

And this my Listener:

public class ActivityListener extends ListenerAdapter {
    private final JDA jda;

    public ActivityListener(JDA jda) {
        this.jda = jda;
    }

    @Override
    public void onUserUpdateActivities(@NotNull UserUpdateActivitiesEvent event) {
        super.onUserUpdateActivities(event);
        System.out.println(event.getUser().getAsTag() + " " + event.getUser().getIdLong());
    }
}

Sadly when I or someone else starts a game or smt else it never triggers.

Edit:
I used jda.getUserCache(); to check if the caching worked and I am i the cache, but it still doesn't work.


Solution

  • The members for the associated users have to be cached before the event fires. Since you use lazy loading this might take a while to happen since members are added to cache through messages or voice states.

    You can use setChunkingFilter(ChunkingFilter.ALL) to eagerly load all members on startup.


    There was a bug which caused these events to not fire which has been fixed in 4.2.1_264.