如何在Java Discord-JDA中特定于MEMBER_VOICE_KICK操作触发事件侦听器?

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

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

问题

我有一个具有以下问题的事件。

  1. 每当创建新的审计日志时,它都会触发。
  2. 如果用户进入语音聊天,它也会触发。

这是事件监听器:

   @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();

        });
    }

我的目标是:

  • 我希望它只在发生MEMBER_VOICE_KICK操作时触发。只有这个。

我尝试过的:

  • 我尝试使用onGuildVoiceUpdate()事件...但我遇到了相同的问题。
英文:

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:

  • I want it to specifically be triggered whenever a MEMBER_VOICE_KICK action happens. Only That

What i tried:

  • I tried using the onGuildVoiceUpdate() event.... i got the same problem.

答案1

得分: 0

事件已经为您提供了已创建的条目,您可以专门查看该条目。

public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) {
  AuditLogEntry entry = event.getEntry();
  if (entry.getType() != ActionType.MEMBER_VOICE_KICK) return; // 保护条件
  String userId = entry.getUserId();
  guild.retrieveMemberById(userId).flatMap(member ->
    member.modifyNickname("用户语音踢出者")
  ).queue();
}
英文:

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();
}

huangapple
  • 本文由 发表于 2023年5月29日 02:02:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352913.html
匿名

发表评论

匿名网友

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

确定