英文:
How do I trigger an event listener in Java Discord-JDA specifically for MEMBER_VOICE_KICK action?
问题
我有一个具有以下问题的事件。
- 每当创建新的审计日志时,它都会触发。
- 如果用户进入语音聊天,它也会触发。
这是事件监听器:
@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.
- It gets triggered whenever a new audit log gets created.
- 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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论