javadiscordbotsdiscord-jda

How do I trigger an event listener in Java Discord-JDA specifically for MEMBER_VOICE_KICK action?


I have this event that has these problems.

  1. It gets triggered whenever a new audit log gets created.
  2. If a user enters voice-chat it gets triggered too

this the event listener

   @Override
    public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) {
        JDA jda=event.getJDA();
        Guild gld=jda.getGuilds().get(0);

        AuditLogPaginationAction auditLogs = event.getGuild().retrieveAuditLogs();
        auditLogs.type(ActionType.MEMBER_VOICE_KICK);
        auditLogs.limit(1);
        auditLogs.queue( (entrys) ->
        {

            if (entrys.isEmpty()) return;
            AuditLogEntry entry = entrys.get(0);
            System.out.println("The User "+ entry.getUser().getId());
            Member member=gld.getMemberById(entry.getUser().getId());
            gld.modifyNickname(member,"User Voice kicker").queue();

        });
    }

What i am aiming at:

What i tried:


Solution

  • The event already provides you the created entry, you can just look at that one specifically.

    public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) {
      AuditLogEntry entry = event.getEntry();
      if (entry.getType() != ActionType.MEMBER_VOICE_KICK) return; // guard case
      String userId = entry.getUserId();
      guild.retrieveMemberById(userId).flatMap(member ->
        member.modifyNickname("User Voice kicker")
      ).queue();
    }