From e03aef0ad5b81ad9f735eeb78bab347e79db9442 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 2 Jul 2024 18:31:18 +0100 Subject: [PATCH] impl member join and leave logs --- .../bat/features/logging/LogCategory.java | 3 +- .../bat/features/logging/LogFeature.java | 11 ---- .../bat/features/logging/LogProfile.java | 3 ++ .../bat/features/logging/LogType.java | 8 ++- .../logging/command/ListSubCommand.java | 6 +-- .../logging/command/SetSubCommand.java | 6 ++- .../logging/listeners/MemberListener.java | 52 +++++++++++++++++++ .../logging/listeners/MessageListener.java | 5 +- 8 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java 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 7f40b6c..e05f683 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogCategory.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogCategory.java @@ -8,7 +8,8 @@ import lombok.Getter; */ @AllArgsConstructor @Getter public enum LogCategory { - MESSAGES("Messages"); + MESSAGES("Messages"), + MEMBER("Member"); /** * The name of the log category diff --git a/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java b/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java index a6752db..76133c0 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java @@ -1,7 +1,6 @@ package cc.fascinated.bat.features.logging; import cc.fascinated.bat.command.Category; -import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.PasteUtils; import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.base.profile.FeatureProfile; @@ -9,7 +8,6 @@ import cc.fascinated.bat.features.logging.command.LogsCommand; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.service.CommandService; import lombok.NonNull; -import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import org.springframework.beans.factory.annotation.Autowired; @@ -28,15 +26,6 @@ public class LogFeature extends Feature { super.registerCommand(commandService, context.getBean(LogsCommand.class)); } - /** - * Creates an embed to use for logging - * - * @return the embed - */ - public EmbedBuilder baseLogEmbed() { - return EmbedUtils.genericEmbed(); - } - /** * Sends a log to the log channel * diff --git a/src/main/java/cc/fascinated/bat/features/logging/LogProfile.java b/src/main/java/cc/fascinated/bat/features/logging/LogProfile.java index 28cdce8..e40e657 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogProfile.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogProfile.java @@ -37,6 +37,9 @@ public class LogProfile extends Serializable { */ public TextChannel getLogChannel(LogType logType) { TextChannel textChannel = this.logChannels.get(logType); + if (textChannel == null) { + return null; + } // Ensure the channel exists if (DiscordService.JDA.getTextChannelById(textChannel.getId()) == null) { this.logChannels.remove(logType); 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 06010bb..7b4e7d0 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -15,7 +15,13 @@ public enum LogType { * Message Events */ MESSAGE_DELETE(LogCategory.MESSAGES, "Message Delete"), - MESSAGE_EDIT(LogCategory.MESSAGES,"Message Edit"); + MESSAGE_EDIT(LogCategory.MESSAGES,"Message Edit"), + + /** + * Member Events + */ + MEMBER_JOIN(LogCategory.MEMBER, "Member Join"), + MEMBER_LEAVE(LogCategory.MEMBER, "Member Leave"); /** * The category of the log type diff --git a/src/main/java/cc/fascinated/bat/features/logging/command/ListSubCommand.java b/src/main/java/cc/fascinated/bat/features/logging/command/ListSubCommand.java index de68db0..30be465 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/command/ListSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/logging/command/ListSubCommand.java @@ -32,11 +32,11 @@ public class ListSubCommand extends BatSubCommand { - A specific category by using `/logs set ` - All log types by using `/logs set all ` To remove a log channel, it's the same as setting it, - but with `/logs remove` instead of `/logs set` - """, false); + but with `/logs remove` instead of `/logs set`""", false); + description.emptyLine(); for (int i = 0; i < LogCategory.values().length; i++) { LogCategory category = LogCategory.values()[i]; - if (i != LogCategory.values().length - 1) { + if (i != 0) { description.emptyLine(); } description.appendLine("**__%s__**".formatted(category.getName()), false); diff --git a/src/main/java/cc/fascinated/bat/features/logging/command/SetSubCommand.java b/src/main/java/cc/fascinated/bat/features/logging/command/SetSubCommand.java index cec97ce..478e3d7 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/command/SetSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/logging/command/SetSubCommand.java @@ -47,12 +47,16 @@ public class SetSubCommand extends BatSubCommand { // Set the log channel for all log types if (type.equalsIgnoreCase("all")) { + EmbedDescriptionBuilder description = new EmbedDescriptionBuilder("Log Channel"); + description.appendLine("Successfully set the log channel for all log types to %s".formatted(targetChannel.getAsMention()), false); + description.emptyLine(); for (LogType logType : LogType.values()) { + description.appendLine(logType.getName(), true); profile.setLogChannel(logType, targetChannel); } event.replyEmbeds(EmbedUtils.successEmbed() - .setDescription("Successfully set the log channel for all log types to %s".formatted(targetChannel.getAsMention())) + .setDescription(description.build()) .build()).queue(); return; } diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java new file mode 100644 index 0000000..84df8dc --- /dev/null +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java @@ -0,0 +1,52 @@ +package cc.fascinated.bat.features.logging.listeners; + +import cc.fascinated.bat.common.EmbedDescriptionBuilder; +import cc.fascinated.bat.common.EmbedUtils; +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 cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * @author Fascinated (fascinated7) + */ +@Component +public class MemberListener implements EventListener { + private final LogFeature logFeature; + + @Autowired + public MemberListener(@NonNull ApplicationContext context) { + this.logFeature = context.getBean(LogFeature.class); + } + + @Override + public void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) { + if (user.getDiscordUser().isBot()) return; + + logFeature.sendLog(guild, LogType.MEMBER_JOIN, EmbedUtils.successEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Joined") + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Snowflake: %s".formatted(user.getId()), true) + .build()) + .build()); + } + + @Override + public void onGuildMemberLeave(@NonNull BatGuild guild, BatUser user, @NonNull GuildMemberRemoveEvent event) { + if (user.getDiscordUser().isBot()) return; + + logFeature.sendLog(guild, LogType.MEMBER_LEAVE, EmbedUtils.errorEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Left") + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Snowflake: %s".formatted(user.getId()), true) + .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 a5c702d..ee25413 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 @@ -1,6 +1,7 @@ package cc.fascinated.bat.features.logging.listeners; import cc.fascinated.bat.common.EmbedDescriptionBuilder; +import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.features.logging.LogFeature; import cc.fascinated.bat.features.logging.LogType; @@ -30,7 +31,7 @@ public class MessageListener implements EventListener { public void onGuildMessageDelete(@NonNull BatGuild guild, BatUser user, DiscordMessage message, @NonNull MessageDeleteEvent event) { if (user.getDiscordUser().isBot() || message.getAuthor().isBot()) return; - logFeature.sendLog(guild, LogType.MESSAGE_DELETE, logFeature.baseLogEmbed() + logFeature.sendLog(guild, LogType.MESSAGE_DELETE, EmbedUtils.errorEmbed() .setDescription(new EmbedDescriptionBuilder("Message Deleted") .appendLine("Author: %s (%s)".formatted(message.getAuthor().getAsMention(), message.getAuthor().getId()), true) .appendLine("Channel: %s".formatted(message.getChannel().getAsMention()), true) @@ -44,7 +45,7 @@ public class MessageListener implements EventListener { @NonNull DiscordMessage newMessage, @NonNull MessageUpdateEvent event) { if (user.getDiscordUser().isBot() || newMessage.getAuthor().isBot() || oldMessage == null) return; - logFeature.sendLog(guild, LogType.MESSAGE_EDIT, logFeature.baseLogEmbed() + logFeature.sendLog(guild, LogType.MESSAGE_EDIT, EmbedUtils.genericEmbed() .setDescription(new EmbedDescriptionBuilder("Message Edited") .appendLine("Author: %s (%s)".formatted(newMessage.getAuthor().getAsMention(), newMessage.getAuthor().getId()), true) .appendLine("Channel: %s".formatted(newMessage.getChannel().getAsMention()), true)