add role create, delete, rename and update permissions logs

This commit is contained in:
Lee 2024-07-04 09:56:06 +01:00
parent c284bb8a7f
commit 6ba802912f
5 changed files with 141 additions and 3 deletions

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

@ -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

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

@ -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<Permission> oldPermissions = event.getOldValue();
EnumSet<Permission> 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());
}
}

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