impl member join and leave logs

This commit is contained in:
Lee 2024-07-02 18:31:18 +01:00
parent 8ce3a5d25c
commit e03aef0ad5
8 changed files with 75 additions and 19 deletions

@ -8,7 +8,8 @@ import lombok.Getter;
*/ */
@AllArgsConstructor @Getter @AllArgsConstructor @Getter
public enum LogCategory { public enum LogCategory {
MESSAGES("Messages"); MESSAGES("Messages"),
MEMBER("Member");
/** /**
* The name of the log category * The name of the log category

@ -1,7 +1,6 @@
package cc.fascinated.bat.features.logging; package cc.fascinated.bat.features.logging;
import cc.fascinated.bat.command.Category; import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.PasteUtils; import cc.fascinated.bat.common.PasteUtils;
import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.base.profile.FeatureProfile; 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.model.BatGuild;
import cc.fascinated.bat.service.CommandService; import cc.fascinated.bat.service.CommandService;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -28,15 +26,6 @@ public class LogFeature extends Feature {
super.registerCommand(commandService, context.getBean(LogsCommand.class)); 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 * Sends a log to the log channel
* *

@ -37,6 +37,9 @@ public class LogProfile extends Serializable {
*/ */
public TextChannel getLogChannel(LogType logType) { public TextChannel getLogChannel(LogType logType) {
TextChannel textChannel = this.logChannels.get(logType); TextChannel textChannel = this.logChannels.get(logType);
if (textChannel == null) {
return null;
}
// Ensure the channel exists // Ensure the channel exists
if (DiscordService.JDA.getTextChannelById(textChannel.getId()) == null) { if (DiscordService.JDA.getTextChannelById(textChannel.getId()) == null) {
this.logChannels.remove(logType); this.logChannels.remove(logType);

@ -15,7 +15,13 @@ public enum LogType {
* Message Events * Message Events
*/ */
MESSAGE_DELETE(LogCategory.MESSAGES, "Message Delete"), 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 * The category of the log type

@ -32,11 +32,11 @@ public class ListSubCommand extends BatSubCommand {
- A specific category by using `/logs set <category> <channel>` - A specific category by using `/logs set <category> <channel>`
- All log types by using `/logs set all <channel>` - All log types by using `/logs set all <channel>`
To remove a log channel, it's the same as setting it, To remove a log channel, it's the same as setting it,
but with `/logs remove` instead of `/logs set` but with `/logs remove` instead of `/logs set`""", false);
""", false); description.emptyLine();
for (int i = 0; i < LogCategory.values().length; i++) { for (int i = 0; i < LogCategory.values().length; i++) {
LogCategory category = LogCategory.values()[i]; LogCategory category = LogCategory.values()[i];
if (i != LogCategory.values().length - 1) { if (i != 0) {
description.emptyLine(); description.emptyLine();
} }
description.appendLine("**__%s__**".formatted(category.getName()), false); description.appendLine("**__%s__**".formatted(category.getName()), false);

@ -47,12 +47,16 @@ public class SetSubCommand extends BatSubCommand {
// Set the log channel for all log types // Set the log channel for all log types
if (type.equalsIgnoreCase("all")) { 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()) { for (LogType logType : LogType.values()) {
description.appendLine(logType.getName(), true);
profile.setLogChannel(logType, targetChannel); profile.setLogChannel(logType, targetChannel);
} }
event.replyEmbeds(EmbedUtils.successEmbed() event.replyEmbeds(EmbedUtils.successEmbed()
.setDescription("Successfully set the log channel for all log types to %s".formatted(targetChannel.getAsMention())) .setDescription(description.build())
.build()).queue(); .build()).queue();
return; return;
} }

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

@ -1,6 +1,7 @@
package cc.fascinated.bat.features.logging.listeners; package cc.fascinated.bat.features.logging.listeners;
import cc.fascinated.bat.common.EmbedDescriptionBuilder; import cc.fascinated.bat.common.EmbedDescriptionBuilder;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.features.logging.LogFeature; import cc.fascinated.bat.features.logging.LogFeature;
import cc.fascinated.bat.features.logging.LogType; 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) { public void onGuildMessageDelete(@NonNull BatGuild guild, BatUser user, DiscordMessage message, @NonNull MessageDeleteEvent event) {
if (user.getDiscordUser().isBot() || message.getAuthor().isBot()) return; 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") .setDescription(new EmbedDescriptionBuilder("Message Deleted")
.appendLine("Author: %s (%s)".formatted(message.getAuthor().getAsMention(), message.getAuthor().getId()), true) .appendLine("Author: %s (%s)".formatted(message.getAuthor().getAsMention(), message.getAuthor().getId()), true)
.appendLine("Channel: %s".formatted(message.getChannel().getAsMention()), true) .appendLine("Channel: %s".formatted(message.getChannel().getAsMention()), true)
@ -44,7 +45,7 @@ public class MessageListener implements EventListener {
@NonNull DiscordMessage newMessage, @NonNull MessageUpdateEvent event) { @NonNull DiscordMessage newMessage, @NonNull MessageUpdateEvent event) {
if (user.getDiscordUser().isBot() || newMessage.getAuthor().isBot() || oldMessage == null) return; 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") .setDescription(new EmbedDescriptionBuilder("Message Edited")
.appendLine("Author: %s (%s)".formatted(newMessage.getAuthor().getAsMention(), newMessage.getAuthor().getId()), true) .appendLine("Author: %s (%s)".formatted(newMessage.getAuthor().getAsMention(), newMessage.getAuthor().getId()), true)
.appendLine("Channel: %s".formatted(newMessage.getChannel().getAsMention()), true) .appendLine("Channel: %s".formatted(newMessage.getChannel().getAsMention()), true)