From 75da7a4b51f5af065d0bc082cb8b13fdda344581 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 18:04:15 +0100 Subject: [PATCH] a lil cleanup and add role perms when adding and removing a role to the log --- .../java/cc/fascinated/bat/common/RoleUtils.java | 15 +++++++++++++++ .../features/logging/listeners/RoleListener.java | 11 +++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/common/RoleUtils.java b/src/main/java/cc/fascinated/bat/common/RoleUtils.java index 8d69b62..ef6c87e 100644 --- a/src/main/java/cc/fascinated/bat/common/RoleUtils.java +++ b/src/main/java/cc/fascinated/bat/common/RoleUtils.java @@ -2,6 +2,7 @@ package cc.fascinated.bat.common; import cc.fascinated.bat.model.BatGuild; import lombok.experimental.UtilityClass; +import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; @@ -21,4 +22,18 @@ public class RoleUtils { public static boolean hasPermissionToGiveRole(BatGuild guild, Member member, Role role) { return member.getRoles().stream().anyMatch(r -> r.getPosition() > role.getPosition()); } + + /** + * Gets the formatted permissions of a role + * + * @param role the role to get the formatted permissions of + * @return the formatted permissions + */ + public String getFormattedPermissions(Role role) { + StringBuilder formattedPermissions = new StringBuilder(); + for (Permission permission : role.getPermissions()) { + formattedPermissions.append("`").append(permission.getName()).append("`, "); + } + return formattedPermissions.substring(0, formattedPermissions.length() - 2); + } } diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/RoleListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/RoleListener.java index 77bcb1c..bcbf553 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/RoleListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/RoleListener.java @@ -3,6 +3,7 @@ package cc.fascinated.bat.features.logging.listeners; import cc.fascinated.bat.common.EmbedDescriptionBuilder; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.HexColorUtils; +import cc.fascinated.bat.common.RoleUtils; import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.features.logging.LogFeature; import cc.fascinated.bat.features.logging.LogType; @@ -71,26 +72,28 @@ public class RoleListener implements EventListener { 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" : HexColorUtils.colorToHex(role.getColor()) ), true) + .appendLine("Permissions: %s".formatted(RoleUtils.getFormattedPermissions(role)), 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()); + Role role = event.getRole(); + log.info("Role \"{}\" was deleted in guild \"{}\"", role.getName(), guild.getName()); logFeature.sendLog(guild, LogType.ROLE_DELETE, EmbedUtils.successEmbed() .setDescription(new EmbedDescriptionBuilder("Role Deleted") - .appendLine("Role: `%s`".formatted(event.getRole().getName()), true) + .appendLine("Role: `%s`".formatted(role.getName()), true) .appendLine("Color: `%s`".formatted( - event.getRole().getColor() == null ? "Default" : HexColorUtils.colorToHex(event.getRole().getColor()) + role.getColor() == null ? "Default" : HexColorUtils.colorToHex(role.getColor()) ), true) + .appendLine("Permissions: %s".formatted(RoleUtils.getFormattedPermissions(role)), true) .build()) .build()); }