From 68a9a6dc48d1df3bb3b5e36579d08165ae194d83 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 2 Jul 2024 21:05:20 +0100 Subject: [PATCH] impl member avatar update logging --- .../cc/fascinated/bat/event/EventListener.java | 11 +++++++++++ .../bat/features/logging/LogType.java | 1 + .../logging/listeners/MemberListener.java | 18 ++++++++++++++++++ .../fascinated/bat/service/EventService.java | 13 +++++++++++++ 4 files changed, 43 insertions(+) diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index d88e48b..80f8951 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -25,6 +25,7 @@ import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionE import net.dv8tion.jda.api.events.message.MessageDeleteEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageUpdateEvent; +import net.dv8tion.jda.api.events.user.update.UserUpdateAvatarEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent; @@ -221,6 +222,16 @@ public interface EventListener { default void onUserUpdateName(@NonNull BatUser user, String oldName, String newName, @NonNull UserUpdateNameEvent event) { } + /** + * Called when a user updates their avatar + * + * @param user the user that updated their avatar + * @param oldAvatarUrl the old avatar url + * @param newAvatarUrl the new avatar url + */ + default void onUserUpdateAvatar(@NonNull BatUser user, String oldAvatarUrl, String newAvatarUrl, @NonNull UserUpdateAvatarEvent 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 3b4d655..d780b58 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -25,6 +25,7 @@ public enum LogType { MEMBER_NICKNAME_UPDATE(LogCategory.MEMBER, "Member Nickname Update"), MEMBER_GLOBAL_NAME_UPDATE(LogCategory.MEMBER, "Member Global Name Update"), MEMBER_USERNAME_UPDATE(LogCategory.MEMBER, "Member Username Update"), + MEMBER_AVATAR_UPDATE(LogCategory.MEMBER, "Member Avatar Update"), MEMBER_ROLE_UPDATE(LogCategory.MEMBER, "Member Role Update"), MEMBER_BAN(LogCategory.MEMBER, "Member Ban"), MEMBER_UNBAN(LogCategory.MEMBER, "Member Unban"), 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 5df508e..0522791 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 @@ -20,6 +20,7 @@ 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.user.update.UserUpdateAvatarEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent; import org.springframework.beans.factory.annotation.Autowired; @@ -116,6 +117,23 @@ public class MemberListener implements EventListener { } } + @Override + public void onUserUpdateAvatar(@NonNull BatUser user, String oldAvatarUrl, String newAvatarUrl, @NonNull UserUpdateAvatarEvent event) { + if (user.getDiscordUser().isBot()) return; + for (Guild guild : DiscordService.JDA.getGuilds()) { + BatGuild batGuild = guildService.getGuild(guild.getId()); + if (batGuild == null) continue; + + logFeature.sendLog(batGuild, LogType.MEMBER_USERNAME_UPDATE, EmbedUtils.genericEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Avatar Updated") + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Old Avatar: [avatar](%s)".formatted(oldAvatarUrl), true) + .appendLine("New Avatar: [avatar](%s)".formatted(newAvatarUrl), true) + .build()) + .build()); + } + } + @Override public void onGuildMemberRoleAdd(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull List rolesAdded, @NonNull GuildMemberRoleAddEvent event) { if (user.getDiscordUser().isBot()) return; diff --git a/src/main/java/cc/fascinated/bat/service/EventService.java b/src/main/java/cc/fascinated/bat/service/EventService.java index 2232368..7a672e9 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -21,6 +21,7 @@ import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; import net.dv8tion.jda.api.events.message.MessageDeleteEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.user.update.UserUpdateAvatarEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -277,4 +278,16 @@ public class EventService extends ListenerAdapter { listener.onUserUpdateName(user, event.getOldName(), event.getNewName(), event); } } + + @Override + public void onUserUpdateAvatar(@NotNull UserUpdateAvatarEvent event) { + if (event.getUser().isBot()) { + return; + } + BatUser user = userService.getUser(event.getUser().getId()); + + for (EventListener listener : LISTENERS) { + listener.onUserUpdateAvatar(user, event.getOldAvatarUrl(), event.getNewAvatarUrl(), event); + } + } }