From ff23ea1d6c540fabaa3911b344f364938dcc85cf Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 2 Jul 2024 21:03:40 +0100 Subject: [PATCH] impl global name and username update logging --- .../fascinated/bat/event/EventListener.java | 21 +++++++++ .../bat/features/logging/LogType.java | 2 + .../logging/listeners/MemberListener.java | 43 ++++++++++++++++++- .../bat/service/DiscordService.java | 4 +- .../fascinated/bat/service/EventService.java | 13 ++++++ 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index 3421944..d88e48b 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -10,6 +10,7 @@ import lombok.NonNull; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; +import net.dv8tion.jda.api.events.channel.update.GenericChannelUpdateEvent; import net.dv8tion.jda.api.events.guild.GuildBanEvent; import net.dv8tion.jda.api.events.guild.GuildUnbanEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; @@ -25,6 +26,7 @@ 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.UserUpdateGlobalNameEvent; +import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent; import java.util.List; @@ -200,6 +202,25 @@ public interface EventListener { default void onGuildMemberTimeout(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberUpdateTimeOutEvent event) { } + /** + * Called when a channels state is updated + * + * @param guild the guild that the channel was updated in + * @param event the event that was fired + */ + default void onGenericChannelUpdate(@NonNull BatGuild guild, @NonNull GenericChannelUpdateEvent event) { + } + + /** + * Called when a user updates their username + * + * @param user the user that updated their name + * @param oldName the old username + * @param newName the new username + */ + default void onUserUpdateName(@NonNull BatUser user, String oldName, String newName, @NonNull UserUpdateNameEvent 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 10282b0..3b4d655 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -23,6 +23,8 @@ public enum LogType { MEMBER_JOIN(LogCategory.MEMBER, "Member Join"), MEMBER_LEAVE(LogCategory.MEMBER, "Member Leave"), 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_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 a708136..5df508e 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 @@ -7,7 +7,10 @@ import cc.fascinated.bat.features.logging.LogFeature; import cc.fascinated.bat.features.logging.LogType; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.service.DiscordService; +import cc.fascinated.bat.service.GuildService; import lombok.NonNull; +import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.guild.GuildBanEvent; import net.dv8tion.jda.api.events.guild.GuildUnbanEvent; @@ -17,6 +20,8 @@ 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.UserUpdateGlobalNameEvent; +import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @@ -30,10 +35,12 @@ import java.util.List; @Component public class MemberListener implements EventListener { private final LogFeature logFeature; + private final GuildService guildService; @Autowired - public MemberListener(@NonNull ApplicationContext context) { + public MemberListener(@NonNull ApplicationContext context, @NonNull GuildService guildService) { this.logFeature = context.getBean(LogFeature.class); + this.guildService = guildService; } @Override @@ -75,6 +82,40 @@ public class MemberListener implements EventListener { .build()); } + @Override + public void onUserUpdateGlobalName(@NonNull BatUser user, String oldName, String newName, @NonNull UserUpdateGlobalNameEvent 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_GLOBAL_NAME_UPDATE, EmbedUtils.genericEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Name Updated") + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Old Name: `%s`".formatted(oldName == null ? user.getName() : oldName), true) + .appendLine("New Name: `%s`".formatted(newName == null ? "Removed Name" : newName), true) + .build()) + .build()); + } + } + + @Override + public void onUserUpdateName(@NonNull BatUser user, String oldName, String newName, @NonNull UserUpdateNameEvent 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 Username Updated") + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Old Username: `%s`".formatted(oldName), true) + .appendLine("New Username: `%s`".formatted(newName), 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/DiscordService.java b/src/main/java/cc/fascinated/bat/service/DiscordService.java index bfa0c00..9cd702f 100644 --- a/src/main/java/cc/fascinated/bat/service/DiscordService.java +++ b/src/main/java/cc/fascinated/bat/service/DiscordService.java @@ -51,11 +51,11 @@ public class DiscordService { GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS_AND_STICKERS, - GatewayIntent.GUILD_PRESENCES + GatewayIntent.GUILD_PRESENCES, + GatewayIntent.GUILD_VOICE_STATES )) .disableCache( CacheFlag.ACTIVITY, - CacheFlag.VOICE_STATE, CacheFlag.CLIENT_STATUS, CacheFlag.SCHEDULED_EVENTS ).build() diff --git a/src/main/java/cc/fascinated/bat/service/EventService.java b/src/main/java/cc/fascinated/bat/service/EventService.java index c3c6ee8..2232368 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -22,6 +22,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.user.update.UserUpdateGlobalNameEvent; +import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; @@ -264,4 +265,16 @@ public class EventService extends ListenerAdapter { listener.onGuildMemberTimeout(guild, user, event); } } + + @Override + public void onUserUpdateName(@NotNull UserUpdateNameEvent event) { + if (event.getUser().isBot()) { + return; + } + BatUser user = userService.getUser(event.getUser().getId()); + + for (EventListener listener : LISTENERS) { + listener.onUserUpdateName(user, event.getOldName(), event.getNewName(), event); + } + } }