From deb93e442c329828aea0002ef8f8afb22f534d29 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 2 Jul 2024 19:05:21 +0100 Subject: [PATCH] impl role add/remove logging --- .../fascinated/bat/event/EventListener.java | 25 +++++++++++ .../bat/features/logging/LogType.java | 4 +- .../logging/listeners/MemberListener.java | 41 +++++++++++++++++++ .../fascinated/bat/service/EventService.java | 27 ++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index 66184ed..fda7455 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -7,8 +7,11 @@ import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberLeaderboardT import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import lombok.NonNull; +import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; 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.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; @@ -18,6 +21,8 @@ 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 java.util.List; + /** * @author Fascinated (fascinated7) */ @@ -127,6 +132,26 @@ public interface EventListener { default void onGuildMemberUpdateNickname(@NonNull BatGuild guild, @NonNull BatUser user, String oldName, String newName, @NonNull GuildMemberUpdateNicknameEvent event) { } + /** + * Called when a user gets roles added to them + * + * @param guild the guild that the user added the role in + * @param user the user that added the role + * @param rolesAdded the roles that were added + */ + default void onGuildMemberRoleAdd(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull List rolesAdded, @NonNull GuildMemberRoleAddEvent event) { + } + + /** + * Called when a user gets roles removed from them + * + * @param guild the guild that the user removed the role in + * @param user the user that removed the role + * @param rolesAdded the roles that were removed + */ + default void onGuildMemberRoleRemove(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull List rolesAdded, @NonNull GuildMemberRoleRemoveEvent 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 78555f2..f1fbd0b 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -22,7 +22,9 @@ public enum LogType { */ MEMBER_JOIN(LogCategory.MEMBER, "Member Join"), MEMBER_LEAVE(LogCategory.MEMBER, "Member Leave"), - MEMBER_NICKNAME_CHANGE(LogCategory.MEMBER, "Member Nickname Change"); + MEMBER_NICKNAME_CHANGE(LogCategory.MEMBER, "Member Nickname Change"), + MEMBER_ROLE_ADD(LogCategory.MEMBER, "Member Role Add"), + MEMBER_ROLE_REMOVE(LogCategory.MEMBER, "Member Role Remove"); /** * The category of the log type 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 d0da725..2f9f1f3 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 @@ -8,13 +8,18 @@ import cc.fascinated.bat.features.logging.LogType; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import lombok.NonNull; +import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; 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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; +import java.util.List; + /** * @author Fascinated (fascinated7) */ @@ -65,4 +70,40 @@ public class MemberListener implements EventListener { .build()) .build()); } + + @Override + public void onGuildMemberRoleAdd(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull List rolesAdded, @NonNull GuildMemberRoleAddEvent event) { + if (user.getDiscordUser().isBot()) return; + + StringBuilder roles = new StringBuilder(); + for (Role role : rolesAdded) { + roles.append(role.getAsMention()).append(", "); + } + + String s = rolesAdded.size() > 1 ? "s" : ""; + logFeature.sendLog(guild, LogType.MEMBER_ROLE_ADD, EmbedUtils.successEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Role%s Added".formatted(s)) + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Role%s Added: %s".formatted(s, roles.substring(0, roles.length() - 2)), true) + .build()) + .build()); + } + + @Override + public void onGuildMemberRoleRemove(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull List rolesAdded, @NonNull GuildMemberRoleRemoveEvent event) { + if (user.getDiscordUser().isBot()) return; + + StringBuilder roles = new StringBuilder(); + for (Role role : rolesAdded) { + roles.append(role.getAsMention()).append(", "); + } + + String s = rolesAdded.size() > 1 ? "s" : ""; + logFeature.sendLog(guild, LogType.MEMBER_ROLE_REMOVE, EmbedUtils.errorEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Role%s Removed".formatted(s)) + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Role%s Removed: %s".formatted(s, roles.substring(0, roles.length() - 2)), 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 9f7bc67..5e5567d 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -8,6 +8,8 @@ import lombok.NonNull; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; 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.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; @@ -174,4 +176,29 @@ public class EventService extends ListenerAdapter { listener.onGuildMemberUpdateNickname(guild, user, event.getOldNickname(), event.getNewNickname(), event); } } + + @Override + public void onGuildMemberRoleAdd(@NotNull GuildMemberRoleAddEvent 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.onGuildMemberRoleAdd(guild, user, event.getRoles(), event); + } + } + + public void onGuildMemberRoleRemove(@NotNull GuildMemberRoleRemoveEvent 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.onGuildMemberRoleRemove(guild, user, event.getRoles(), event); + } + } }