impl channel create and delete logging

This commit is contained in:
Lee 2024-07-02 19:24:48 +01:00
parent 86c147f359
commit a3ffaf1ab9
7 changed files with 128 additions and 5 deletions

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

@ -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<Role> 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
*/

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

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

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

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

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