From 6ba802912f3cdacb06cd54511ca90dd95fc4d0f1 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 09:56:06 +0100 Subject: [PATCH] add role create, delete, rename and update permissions logs --- .../fascinated/bat/event/EventListener.java | 16 ++++ .../bat/features/logging/LogType.java | 6 +- .../logging/command/RemoveSubCommand.java | 1 - .../logging/listeners/GuildListener.java | 81 ++++++++++++++++++- .../fascinated/bat/service/EventService.java | 40 +++++++++ 5 files changed, 141 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 8cdb5d4..3cd8ba4 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -35,6 +35,10 @@ 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.role.RoleCreateEvent; +import net.dv8tion.jda.api.events.role.RoleDeleteEvent; +import net.dv8tion.jda.api.events.role.update.RoleUpdateNameEvent; +import net.dv8tion.jda.api.events.role.update.RoleUpdatePermissionsEvent; 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; @@ -140,6 +144,18 @@ public interface EventListener { default void onPermissionOverride(@NonNull BatGuild guild, @NonNull GenericPermissionOverrideEvent event) { } + default void onRoleUpdatePermissions(@NonNull BatGuild guild, @NonNull RoleUpdatePermissionsEvent event) { + } + + default void onRoleCreate(@NonNull BatGuild guild, @NonNull RoleCreateEvent event) { + } + + default void onRoleDelete(@NonNull BatGuild guild, @NonNull RoleDeleteEvent event) { + } + + default void onRoleUpdateName(@NonNull BatGuild guild, @NonNull RoleUpdateNameEvent event) { + } + default void onShutdown() { } } 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 76086f4..778eaf5 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -51,7 +51,11 @@ public enum LogType { INVITE_CREATE(LogCategory.GUILD), EMOJI_ADD(LogCategory.GUILD), EMOJI_REMOVE(LogCategory.GUILD), - EMOJI_RENAME(LogCategory.GUILD); + EMOJI_NAME_UPDATED(LogCategory.GUILD), + ROLE_PERMISSIONS_UPDATED(LogCategory.GUILD), + ROLE_CREATE(LogCategory.GUILD), + ROLE_DELETE(LogCategory.GUILD), + ROLE_NAME_UPDATED(LogCategory.GUILD); /** * The category of the log type diff --git a/src/main/java/cc/fascinated/bat/features/logging/command/RemoveSubCommand.java b/src/main/java/cc/fascinated/bat/features/logging/command/RemoveSubCommand.java index c159c54..14a8948 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/command/RemoveSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/logging/command/RemoveSubCommand.java @@ -12,7 +12,6 @@ import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/GuildListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/GuildListener.java index d38e4cf..2a258f9 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/GuildListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/GuildListener.java @@ -8,16 +8,24 @@ import cc.fascinated.bat.features.logging.LogType; import cc.fascinated.bat.model.BatGuild; import lombok.NonNull; import lombok.extern.log4j.Log4j2; +import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Invite; +import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.emoji.EmojiAddedEvent; import net.dv8tion.jda.api.events.emoji.EmojiRemovedEvent; import net.dv8tion.jda.api.events.emoji.update.EmojiUpdateNameEvent; import net.dv8tion.jda.api.events.guild.invite.GuildInviteCreateEvent; +import net.dv8tion.jda.api.events.role.RoleCreateEvent; +import net.dv8tion.jda.api.events.role.RoleDeleteEvent; +import net.dv8tion.jda.api.events.role.update.RoleUpdateNameEvent; +import net.dv8tion.jda.api.events.role.update.RoleUpdatePermissionsEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; +import java.util.EnumSet; + /** * @author Fascinated (fascinated7) */ @@ -76,7 +84,7 @@ public class GuildListener implements EventListener { @Override public void onEmojiRename(@NonNull BatGuild guild, @NonNull Emoji emoji, @NonNull String oldName, @NonNull String newName, @NonNull EmojiUpdateNameEvent event) { log.info("Emoji \"{}\" was renamed to \"{}\" in guild \"{}\"", oldName, newName, guild.getName()); - logFeature.sendLog(guild, LogType.EMOJI_RENAME, EmbedUtils.successEmbed() + logFeature.sendLog(guild, LogType.EMOJI_NAME_UPDATED, EmbedUtils.successEmbed() .setDescription(new EmbedDescriptionBuilder("Emoji Renamed") .appendLine("Emoji: %s".formatted(emoji.getFormatted()), true) .appendLine("Old Name: `%s`".formatted(oldName), true) @@ -84,4 +92,75 @@ public class GuildListener implements EventListener { .build()) .build()); } + + @Override + public void onRoleUpdatePermissions(@NonNull BatGuild guild, @NonNull RoleUpdatePermissionsEvent event) { + log.info("Role \"{}\" permissions were updated in guild \"{}\"", event.getRole().getName(), guild.getName()); + EnumSet oldPermissions = event.getOldValue(); + EnumSet newPermissions = event.getNewValue(); + + StringBuilder allowedPermissions = new StringBuilder(); + for (Permission permission : newPermissions) { + if (!oldPermissions.contains(permission)) { + allowedPermissions.append("`").append(permission.getName()).append("`, "); + } + } + StringBuilder deniedPermissions = new StringBuilder(); + for (Permission permission : oldPermissions) { + if (!newPermissions.contains(permission)) { + deniedPermissions.append("`").append(permission.getName()).append("`, "); + } + } + + EmbedDescriptionBuilder description = new EmbedDescriptionBuilder("Role Permissions Updated") + .appendLine("Role: %s".formatted(event.getRole().getAsMention()), true); + if (!allowedPermissions.isEmpty()) { + description.appendLine("Allowed Permissions: %s".formatted(allowedPermissions.substring(0, allowedPermissions.length() - 2)), true); + } + if (!deniedPermissions.isEmpty()) { + description.appendLine("Denied Permissions: %s".formatted(deniedPermissions.substring(0, deniedPermissions.length() - 2)), true); + } + + logFeature.sendLog(guild, LogType.ROLE_PERMISSIONS_UPDATED, EmbedUtils.successEmbed() + .setDescription(description.build()) + .build()); + } + + @Override + public void onRoleCreate(@NonNull BatGuild guild, @NonNull RoleCreateEvent event) { + Role role = event.getRole(); + log.info("Role \"{}\" was created in guild \"{}\"", role.getName(), guild.getName()); + + logFeature.sendLog(guild, LogType.ROLE_CREATE, EmbedUtils.successEmbed() + .setDescription(new EmbedDescriptionBuilder("Role Created") + .appendLine("Role: %s".formatted(role.getAsMention()), true) + .appendLine("Color: %s".formatted(role.getColor() == null ? "Default" : role.getColor()), true) + .appendLine("Position: `%s`".formatted(role.getPosition()), true) + .build()) + .build()); + } + + @Override + public void onRoleDelete(@NonNull BatGuild guild, @NonNull RoleDeleteEvent event) { + log.info("Role \"{}\" was deleted in guild \"{}\"", event.getRole().getName(), guild.getName()); + logFeature.sendLog(guild, LogType.ROLE_DELETE, EmbedUtils.successEmbed() + .setDescription(new EmbedDescriptionBuilder("Role Deleted") + .appendLine("Role: %s".formatted(event.getRole().getAsMention()), true) + .appendLine("Color: %s".formatted(event.getRole().getColor() == null ? "Default" : event.getRole().getColor()), true) + .appendLine("Position: `%s`".formatted(event.getRole().getPosition()), true) + .build()) + .build()); + } + + @Override + public void onRoleUpdateName(@NonNull BatGuild guild, @NonNull RoleUpdateNameEvent event) { + log.info("Role \"{}\" was renamed to \"{}\" in guild \"{}\"", event.getOldName(), event.getNewName(), guild.getName()); + logFeature.sendLog(guild, LogType.ROLE_NAME_UPDATED, EmbedUtils.successEmbed() + .setDescription(new EmbedDescriptionBuilder("Role Renamed") + .appendLine("Role: %s".formatted(event.getRole().getAsMention()), true) + .appendLine("Old Name: `%s`".formatted(event.getOldName()), true) + .appendLine("New Name: `%s`".formatted(event.getNewName()), 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 4cbc9c4..ef40299 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -31,6 +31,10 @@ 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.role.RoleCreateEvent; +import net.dv8tion.jda.api.events.role.RoleDeleteEvent; +import net.dv8tion.jda.api.events.role.update.RoleUpdateNameEvent; +import net.dv8tion.jda.api.events.role.update.RoleUpdatePermissionsEvent; 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; @@ -401,4 +405,40 @@ public class EventService extends ListenerAdapter { listener.onPermissionOverride(guild, event); } } + + @Override + public void onRoleUpdatePermissions(@NotNull RoleUpdatePermissionsEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + + for (EventListener listener : LISTENERS) { + listener.onRoleUpdatePermissions(guild, event); + } + } + + @Override + public void onRoleCreate(@NotNull RoleCreateEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + + for (EventListener listener : LISTENERS) { + listener.onRoleCreate(guild, event); + } + } + + @Override + public void onRoleDelete(@NotNull RoleDeleteEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + + for (EventListener listener : LISTENERS) { + listener.onRoleDelete(guild, event); + } + } + + @Override + public void onRoleUpdateName(@NotNull RoleUpdateNameEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + + for (EventListener listener : LISTENERS) { + listener.onRoleUpdateName(guild, event); + } + } }