From 120afee73b4eb4dea976da42ea1112fbaa506022 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 2 Jul 2024 19:55:50 +0100 Subject: [PATCH] impl member timeout logging --- .../fascinated/bat/event/EventListener.java | 10 +++++++++ .../bat/features/logging/LogType.java | 1 + .../logging/listeners/MemberListener.java | 21 +++++++++++++++---- .../fascinated/bat/service/EventService.java | 14 +++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index a256cbe..3421944 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -17,6 +17,7 @@ import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; +import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; @@ -190,6 +191,15 @@ public interface EventListener { default void onGuildMemberUnban(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildUnbanEvent event) { } + /** + * Called when a user gets timed out in a guild (gets muted) + * + * @param guild the guild that the user timed out in + * @param user the user that timed out + */ + default void onGuildMemberTimeout(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberUpdateTimeOutEvent event) { + } + /** * Called when Spring is shutting down */ diff --git a/src/main/java/cc/fascinated/bat/features/logging/LogType.java b/src/main/java/cc/fascinated/bat/features/logging/LogType.java index 05a44a5..10282b0 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -26,6 +26,7 @@ public enum LogType { MEMBER_ROLE_UPDATE(LogCategory.MEMBER, "Member Role Update"), MEMBER_BAN(LogCategory.MEMBER, "Member Ban"), MEMBER_UNBAN(LogCategory.MEMBER, "Member Unban"), + MEMBER_TIMEOUT(LogCategory.MEMBER, "Member Timeout"), /** * Channel Events diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java index 053123e..bf5e8b9 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java @@ -16,10 +16,12 @@ import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; +import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; +import java.time.OffsetDateTime; import java.util.List; /** @@ -116,8 +118,6 @@ public class MemberListener implements EventListener { logFeature.sendLog(guild, LogType.MEMBER_BAN, EmbedUtils.errorEmbed() .setDescription(new EmbedDescriptionBuilder("Member Banned") .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) - .appendLine("Username: %s".formatted(user.getDiscordUser().getName()), true) - .appendLine("Snowflake: %s".formatted(user.getId()), true) .build()) .build()); } @@ -129,8 +129,21 @@ public class MemberListener implements EventListener { logFeature.sendLog(guild, LogType.MEMBER_UNBAN, EmbedUtils.successEmbed() .setDescription(new EmbedDescriptionBuilder("Member Unbanned") .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) - .appendLine("Username: %s".formatted(user.getDiscordUser().getName()), true) - .appendLine("Snowflake: %s".formatted(user.getId()), true) + .build()) + .build()); + } + + @Override + public void onGuildMemberTimeout(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberUpdateTimeOutEvent event) { + OffsetDateTime timeoutEnd = event.getNewTimeOutEnd(); + if (user.getDiscordUser().isBot() || timeoutEnd == null) return; + + long millis = timeoutEnd.toInstant().toEpochMilli(); + logFeature.sendLog(guild, LogType.MEMBER_TIMEOUT, EmbedUtils.errorEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Timed Out") + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Timeout End: ".formatted(millis), true) + .appendLine("Relative: ".formatted(millis), true) .build()) .build()); } diff --git a/src/main/java/cc/fascinated/bat/service/EventService.java b/src/main/java/cc/fascinated/bat/service/EventService.java index 581b3b1..c3c6ee8 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -15,6 +15,7 @@ import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; +import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; @@ -250,4 +251,17 @@ public class EventService extends ListenerAdapter { listener.onGuildMemberUnban(guild, user, event); } } + + @Override + public void onGuildMemberUpdateTimeOut(@NotNull GuildMemberUpdateTimeOutEvent event) { + if (event.getUser().isBot()) { + return; + } + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + BatUser user = userService.getUser(event.getUser().getId()); + + for (EventListener listener : LISTENERS) { + listener.onGuildMemberTimeout(guild, user, event); + } + } }