diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index acd9c81..8cdb5d4 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -27,6 +27,7 @@ import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateBoostTimeEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent; +import net.dv8tion.jda.api.events.guild.override.GenericPermissionOverrideEvent; import net.dv8tion.jda.api.events.guild.voice.GenericGuildVoiceEvent; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; @@ -136,6 +137,9 @@ public interface EventListener { default void onChannelUpdateName(@NonNull BatGuild guild, @NonNull ChannelUpdateNameEvent event) { } + default void onPermissionOverride(@NonNull BatGuild guild, @NonNull GenericPermissionOverrideEvent 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 eb302ea..76086f4 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -43,6 +43,7 @@ public enum LogType { VOICE_CHANNEL_JOIN(LogCategory.CHANNEL), VOICE_CHANNEL_LEAVE(LogCategory.CHANNEL), CHANNEL_UPDATE_NAME(LogCategory.CHANNEL), + CHANNEL_PERMISSIONS_UPDATED(LogCategory.CHANNEL), /** * Guild Events diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java index 96b4ee7..a25032f 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java @@ -13,6 +13,7 @@ import cc.fascinated.bat.service.UserService; import jakarta.annotation.PostConstruct; import lombok.NonNull; import lombok.extern.log4j.Log4j2; +import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; @@ -22,6 +23,7 @@ import net.dv8tion.jda.api.entities.channel.unions.ChannelUnion; import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; import net.dv8tion.jda.api.events.channel.update.ChannelUpdateNameEvent; +import net.dv8tion.jda.api.events.guild.override.GenericPermissionOverrideEvent; import net.dv8tion.jda.api.events.guild.voice.GenericGuildVoiceEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; @@ -135,6 +137,42 @@ public class ChannelListener implements EventListener { logFeature.sendLog(guild, LogType.VOICE_CHANNEL_LEAVE, EmbedUtils.errorEmbed().setDescription(description).build()); } + @Override + public void onPermissionOverride(@NonNull BatGuild guild, @NonNull GenericPermissionOverrideEvent event) { + log.info("Channel permissions updated for channel \"{}\" in guild \"{}\"", event.getChannel().getName(), guild.getName()); + + StringBuilder allowedPermissions = new StringBuilder(); + for (Permission permission : event.getPermissionOverride().getAllowed()) { + allowedPermissions.append("`").append(permission.getName()).append("`, "); + } + + StringBuilder deniedPermissions = new StringBuilder(); + for (Permission permission : event.getPermissionOverride().getDenied()) { + deniedPermissions.append("`").append(permission.getName()).append("`, "); + } + + String target = event.getPermissionOverride().isRoleOverride() ? event.getPermissionOverride().getRole().getAsMention() + : event.getPermissionOverride().getMember().getAsMention(); + EmbedDescriptionBuilder description = new EmbedDescriptionBuilder("Channel permissions updated") + .appendLine("Channel: %s".formatted(event.getChannel().getAsMention()), true) + .appendLine("%s: %s".formatted( + event.getPermissionOverride().isRoleOverride() ? "Role" : "Member", + target + ), true); + + if (!allowedPermissions.isEmpty()) { + description.appendLine("Allowed Permissions: %s".formatted(allowedPermissions.toString().isEmpty() ? "None" + : allowedPermissions.substring(0, allowedPermissions.length() - 2)), true); + } + if (!deniedPermissions.isEmpty()) { + description.appendLine("Denied Permissions: %s".formatted(deniedPermissions.toString().isEmpty() ? "None" + : deniedPermissions.substring(0, deniedPermissions.length() - 2)), true); + } + + logFeature.sendLog(guild, LogType.CHANNEL_PERMISSIONS_UPDATED, EmbedUtils.successEmbed() + .setDescription(description.build()).build()); + } + /** * Formats the channel type * diff --git a/src/main/java/cc/fascinated/bat/service/EventService.java b/src/main/java/cc/fascinated/bat/service/EventService.java index 7314ab9..4cbc9c4 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -24,6 +24,7 @@ import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateBoostTimeEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent; +import net.dv8tion.jda.api.events.guild.override.GenericPermissionOverrideEvent; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; @@ -391,4 +392,13 @@ public class EventService extends ListenerAdapter { listener.onChannelUpdateName(guild, event); } } + + @Override + public void onGenericPermissionOverride(@NotNull GenericPermissionOverrideEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + + for (EventListener listener : LISTENERS) { + listener.onPermissionOverride(guild, event); + } + } }