a lil cleanup and add role perms when adding and removing a role to the log
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 59s

This commit is contained in:
Lee 2024-07-04 18:04:15 +01:00
parent 313f43172d
commit 75da7a4b51
2 changed files with 22 additions and 4 deletions

View File

@ -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);
}
}

View File

@ -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());
}