From a3ffaf1ab913845b165489ffb75768060f2b2784 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 2 Jul 2024 19:24:48 +0100 Subject: [PATCH] impl channel create and delete logging --- .../cc/fascinated/bat/common/EnumUtils.java | 21 +++++++ .../fascinated/bat/event/EventListener.java | 18 ++++++ .../bat/features/logging/LogCategory.java | 5 +- .../bat/features/logging/LogType.java | 12 +++- .../logging/listeners/ChannelListener.java | 55 +++++++++++++++++++ .../logging/listeners/MessageListener.java | 1 + .../fascinated/bat/service/EventService.java | 21 +++++++ 7 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/EnumUtils.java create mode 100644 src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java diff --git a/src/main/java/cc/fascinated/bat/common/EnumUtils.java b/src/main/java/cc/fascinated/bat/common/EnumUtils.java new file mode 100644 index 0000000..be6b346 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/EnumUtils.java @@ -0,0 +1,21 @@ +package cc.fascinated.bat.common; + +/** + * @author Fascinated (fascinated7) + */ +public class EnumUtils { + /** + * Gets the name of the enum + * + * @param e the enum + * @return the name + */ + public static String getEnumName(Enum e) { + String[] split = e.name().split("_"); + StringBuilder builder = new StringBuilder(); + for (String s : split) { + builder.append(s.substring(0, 1).toUpperCase()).append(s.substring(1).toLowerCase()).append(" "); + } + return builder.toString().trim(); + } +} diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index fda7455..45f9e5b 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -8,6 +8,8 @@ import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreT import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import lombok.NonNull; import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; +import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent; @@ -152,6 +154,22 @@ public interface EventListener { default void onGuildMemberRoleRemove(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull List rolesAdded, @NonNull GuildMemberRoleRemoveEvent event) { } + /** + * Called when a channel is created + * + * @param guild the guild that the channel was created in + */ + default void onChannelCreate(@NonNull BatGuild guild, @NonNull ChannelCreateEvent event) { + } + + /** + * Called when a channel is deleted + * + * @param guild the guild that the channel was deleted in + */ + default void onChannelDelete(@NonNull BatGuild guild, @NonNull ChannelDeleteEvent event) { + } + /** * Called when Spring is shutting down */ diff --git a/src/main/java/cc/fascinated/bat/features/logging/LogCategory.java b/src/main/java/cc/fascinated/bat/features/logging/LogCategory.java index e05f683..45bf56d 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogCategory.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogCategory.java @@ -8,8 +8,9 @@ import lombok.Getter; */ @AllArgsConstructor @Getter public enum LogCategory { - MESSAGES("Messages"), - MEMBER("Member"); + MESSAGE("Message"), + MEMBER("Member"), + CHANNEL("Channel"); /** * The name of the log category 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 1d546b4..6592f12 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -14,8 +14,8 @@ public enum LogType { /** * Message Events */ - MESSAGE_DELETE(LogCategory.MESSAGES, "Message Delete"), - MESSAGE_EDIT(LogCategory.MESSAGES,"Message Edit"), + MESSAGE_DELETE(LogCategory.MESSAGE, "Message Delete"), + MESSAGE_EDIT(LogCategory.MESSAGE,"Message Edit"), /** * Member Events @@ -23,7 +23,13 @@ public enum LogType { MEMBER_JOIN(LogCategory.MEMBER, "Member Join"), MEMBER_LEAVE(LogCategory.MEMBER, "Member Leave"), MEMBER_NICKNAME_UPDATE(LogCategory.MEMBER, "Member Nickname Update"), - MEMBER_ROLE_UPDATE(LogCategory.MEMBER, "Member Role Update"); + MEMBER_ROLE_UPDATE(LogCategory.MEMBER, "Member Role Update"), + + /** + * Channel Events + */ + CHANNEL_CREATE(LogCategory.CHANNEL, "Channel Create"), + CHANNEL_DELETE(LogCategory.CHANNEL, "Channel Delete"); /** * The category of the log type 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 new file mode 100644 index 0000000..814ab05 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java @@ -0,0 +1,55 @@ +package cc.fascinated.bat.features.logging.listeners; + +import cc.fascinated.bat.common.EmbedDescriptionBuilder; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.common.EnumUtils; +import cc.fascinated.bat.event.EventListener; +import cc.fascinated.bat.features.logging.LogFeature; +import cc.fascinated.bat.features.logging.LogType; +import cc.fascinated.bat.model.BatGuild; +import lombok.NonNull; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +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 org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * @author Fascinated (fascinated7) + */ +@Component +public class ChannelListener implements EventListener { + private final LogFeature logFeature; + + @Autowired + public ChannelListener(@NonNull ApplicationContext context) { + this.logFeature = context.getBean(LogFeature.class); + } + + @Override + public void onChannelCreate(@NonNull BatGuild guild, @NonNull ChannelCreateEvent event) { + logFeature.sendLog(guild, LogType.CHANNEL_CREATE, EmbedUtils.successEmbed() + .setDescription(new EmbedDescriptionBuilder("Channel Created") + .appendLine("Channel: %s".formatted(event.getChannel().getAsMention()), true) + .appendLine("Name: %s".formatted(event.getChannel().getName()), true) + .appendLine("Type: %s".formatted(EnumUtils.getEnumName(event.getChannel().getType())), true) + .build()) + .build()); + } + + @Override + public void onChannelDelete(@NonNull BatGuild guild, @NonNull ChannelDeleteEvent event) { + ChannelUnion channel = event.getChannel(); + EmbedDescriptionBuilder description = new EmbedDescriptionBuilder("Channel Deleted") + .appendLine("Channel: %s".formatted(channel.getAsMention()), true) + .appendLine("Name: %s".formatted(channel.getName()), true); + if (channel.getType().isMessage()) { + TextChannel textChannel = channel.asTextChannel(); + description.appendLine("Topic: %s".formatted(textChannel.getTopic()), true); + } + description.appendLine("Type: %s".formatted(EnumUtils.getEnumName(channel.getType())), true); + logFeature.sendLog(guild, LogType.CHANNEL_DELETE, EmbedUtils.errorEmbed().setDescription(description.build()).build()); + } +} diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/MessageListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/MessageListener.java index abd83c5..b8cdbe4 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/MessageListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/MessageListener.java @@ -51,6 +51,7 @@ public class MessageListener implements EventListener { .appendLine("Channel: %s".formatted(newMessage.getChannel().getAsMention()), true) .appendLine("Old Content: %s".formatted(logFeature.formatContent(oldMessage.getContent())), true) .appendLine("New Content: %s".formatted(logFeature.formatContent(newMessage.getContent())), true) + .appendLine("*[Jump to Message](%s)*".formatted(newMessage.getMessageUrl()), false) .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 5e5567d..a7a39e8 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -6,6 +6,8 @@ import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.DiscordMessage; import lombok.NonNull; import lombok.extern.log4j.Log4j2; +import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; +import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent; @@ -190,6 +192,7 @@ public class EventService extends ListenerAdapter { } } + @Override public void onGuildMemberRoleRemove(@NotNull GuildMemberRoleRemoveEvent event) { if (event.getUser().isBot()) { return; @@ -201,4 +204,22 @@ public class EventService extends ListenerAdapter { listener.onGuildMemberRoleRemove(guild, user, event.getRoles(), event); } } + + @Override + public void onChannelCreate(@NotNull ChannelCreateEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + + for (EventListener listener : LISTENERS) { + listener.onChannelCreate(guild, event); + } + } + + @Override + public void onChannelDelete(@NotNull ChannelDeleteEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + + for (EventListener listener : LISTENERS) { + listener.onChannelDelete(guild, event); + } + } }