add emoji logs and fix log channels that are set from vanishing

This commit is contained in:
Lee 2024-07-04 05:10:35 +01:00
parent c1d775c9d0
commit cdd953351a
11 changed files with 112 additions and 193 deletions

View File

@ -1,6 +1,5 @@
package cc.fascinated.bat.common;
import cc.fascinated.bat.service.DiscordService;
import lombok.experimental.UtilityClass;
/**

View File

@ -9,9 +9,13 @@ import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken;
import lombok.NonNull;
import net.dv8tion.jda.api.entities.Invite;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent;
import net.dv8tion.jda.api.events.channel.update.GenericChannelUpdateEvent;
import net.dv8tion.jda.api.events.emoji.EmojiAddedEvent;
import net.dv8tion.jda.api.events.emoji.EmojiRemovedEvent;
import net.dv8tion.jda.api.events.emoji.update.EmojiUpdateNameEvent;
import net.dv8tion.jda.api.events.guild.GuildBanEvent;
import net.dv8tion.jda.api.events.guild.GuildUnbanEvent;
import net.dv8tion.jda.api.events.guild.invite.GuildInviteCreateEvent;
@ -40,250 +44,94 @@ import java.util.List;
* @author Fascinated (fascinated7)
*/
public interface EventListener {
/**
* Called when a ScoreSaber score is received
*
* @param score the score that was set
* @param leaderboard the leaderboard that the score was set on
* @param player the player that set the score
*/
default void onScoresaberScoreReceived(@NonNull ScoreSaberPlayerScoreToken score, @NonNull ScoreSaberLeaderboardToken leaderboard,
@NonNull ScoreSaberScoreToken.LeaderboardPlayerInfo player) {
}
/**
* Called when a user joins a guild
*
* @param guild the guild the user joined
* @param user the user that joined the guild
*/
default void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) {
}
/**
* Called when a user leaves a guild
*
* @param guild the guild the user left
* @param user the user that left the guild
*/
default void onGuildMemberLeave(@NonNull BatGuild guild, BatUser user, @NonNull GuildMemberRemoveEvent event) {
}
/**
* Called when a user types a message
*
* @param guild the guild that the message was sent in
* @param user the user that sent the message
*/
default void onGuildMessageReceive(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull MessageReceivedEvent event) {
}
/**
* Called when a user updates a message
*
* @param guild the guild that the message was updated in
* @param user the user that updated the message
*/
default void onGuildMessageEdit(@NonNull BatGuild guild, @NonNull BatUser user, DiscordMessage oldMessage,
@NonNull DiscordMessage newMessage, @NonNull MessageUpdateEvent event) {
}
/**
* Called when a user deletes a message
*
* @param guild the guild that the message was deleted in
* @param user the user that deleted the message
*/
default void onGuildMessageDelete(@NonNull BatGuild guild, BatUser user, DiscordMessage message, @NonNull MessageDeleteEvent event) {
}
/**
* Called when a user selects a string
*
* @param guild the guild that the string was selected in
* @param user the user that selected the string
*/
default void onStringSelectInteraction(BatGuild guild, @NonNull BatUser user, @NonNull StringSelectInteractionEvent event) {
}
/**
* Called when a user interacts with a button
*
* @param guild the guild that the button was interacted with in
* @param user the user that interacted with the button
*/
default void onButtonInteraction(BatGuild guild, @NonNull BatUser user, @NonNull ButtonInteractionEvent event) {
}
/**
* Called when a user interacts with a modal
*
* @param guild the guild that the modal was interacted with in
* @param user the user that interacted with the modal
*/
default void onModalInteraction(BatGuild guild, @NonNull BatUser user, @NonNull ModalInteractionEvent event) {
}
/**
* Called when a user updates their global name
*
* @param user the user that updated their global name
* @param oldName the old global name
* @param newName the new global name
*/
default void onUserUpdateGlobalName(@NonNull BatUser user, String oldName, String newName, @NonNull UserUpdateGlobalNameEvent event) {
}
/**
* Called when a user updates their nickname in a guild
*
* @param guild the guild that the user updated their nickname in
* @param user the user that updated their nickname
* @param oldName the old nickname
* @param newName the new nickname
*/
default void onGuildMemberUpdateNickname(@NonNull BatGuild guild, @NonNull BatUser user, String oldName, String newName, @NonNull GuildMemberUpdateNicknameEvent event) {
}
/**
* Called when a user gets roles added to them
*
* @param guild the guild that the user added the role in
* @param user the user that added the role
* @param rolesAdded the roles that were added
*/
default void onGuildMemberRoleAdd(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull List<Role> rolesAdded, @NonNull GuildMemberRoleAddEvent event) {
}
/**
* Called when a user gets roles removed from them
*
* @param guild the guild that the user removed the role in
* @param user the user that removed the role
* @param rolesAdded the roles that were removed
*/
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 a user is banned from a guild
*
* @param guild the guild that the user was banned from
* @param user the user that was banned
*/
default void onGuildMemberBan(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildBanEvent event) {
}
/**
* Called when a user is unbanned from a guild
*
* @param guild the guild that the user was unbanned from
* @param user the user that was unbanned
*/
default void onGuildMemberUnban(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildUnbanEvent event) {
}
/**
* Called when a user gets timed out in a guild (gets muted)
*
* @param guild the guild that the user timed out in
* @param user the user that timed out
*/
default void onGuildMemberTimeout(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberUpdateTimeOutEvent event) {
}
/**
* Called when a channels state is updated
*
* @param guild the guild that the channel was updated in
* @param event the event that was fired
*/
default void onGenericChannelUpdate(@NonNull BatGuild guild, @NonNull GenericChannelUpdateEvent<?> event) {
}
/**
* Called when a user updates their username
*
* @param user the user that updated their name
* @param oldName the old username
* @param newName the new username
*/
default void onUserUpdateName(@NonNull BatUser user, String oldName, String newName, @NonNull UserUpdateNameEvent event) {
}
/**
* Called when a user updates their avatar
*
* @param user the user that updated their avatar
* @param oldAvatarUrl the old avatar url
* @param newAvatarUrl the new avatar url
*/
default void onUserUpdateAvatar(@NonNull BatUser user, String oldAvatarUrl, String newAvatarUrl, @NonNull UserUpdateAvatarEvent event) {
}
/**
* Called when a user joins or leaves a voice channel
*
* @param guild the guild that the user joined or left the voice channel in
* @param user the user that joined or left the voice channel
*/
default void onGuildVoiceUpdate(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GenericGuildVoiceEvent event) {
}
/**
* Called when a guild invite is created
*
* @param guild the guild that the invite was created in
* @param invite the invite that was created
*/
default void onGuildInviteCreate(@NonNull BatGuild guild, @NonNull Invite invite, @NonNull GuildInviteCreateEvent event) {
}
/**
* Called when a user boosts a guild
*
* @param guild the guild that the user boosted
* @param user the user that boosted the guild
* @param oldBoostTime the old boost time
* @param newBoostTime the new boost time
* @param event the event that was fired
*/
default void onGuildMemberBoostAdd(@NonNull BatGuild guild, @NonNull BatUser user, OffsetDateTime oldBoostTime,
@NonNull OffsetDateTime newBoostTime, @NonNull GuildMemberUpdateBoostTimeEvent event) {
}
/**
* Called when a user boost expires in a guild
*
* @param guild the guild that the user's boost expired in
* @param user the user that the boost expired for
* @param oldBoostTime the old boost time
* @param newBoostTime the new boost time
* @param event the event that was fired
*/
default void onGuildMemberBoostRemove(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull OffsetDateTime oldBoostTime,
OffsetDateTime newBoostTime, @NonNull GuildMemberUpdateBoostTimeEvent event) {
}
/**
* Called when Bat is shutting down
*/
default void onEmojiAdd(@NonNull BatGuild guild, @NonNull Emoji emoji, @NonNull EmojiAddedEvent event) {
}
default void onEmojiRemove(@NonNull BatGuild guild, @NonNull Emoji emoji, @NonNull EmojiRemovedEvent event) {
}
default void onEmojiRename(@NonNull BatGuild guild, @NonNull Emoji emoji, @NonNull String oldName, @NonNull String newName, @NonNull EmojiUpdateNameEvent event) {
}
default void onShutdown() {
}
}

View File

@ -2,7 +2,6 @@ package cc.fascinated.bat.features.logging;
import cc.fascinated.bat.common.ChannelUtils;
import cc.fascinated.bat.common.Serializable;
import cc.fascinated.bat.service.DiscordService;
import com.google.gson.Gson;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.bson.Document;
@ -17,7 +16,7 @@ public class LogProfile extends Serializable {
/**
* The log channels for this profile
*/
private final Map<LogType, TextChannel> logChannels = new HashMap<>();
private final Map<LogType, String> logChannels = new HashMap<>();
/**
* Checks if the log channel for the specified log type exists
@ -36,16 +35,7 @@ public class LogProfile extends Serializable {
* @return the log channel, or null if it doesn't exist
*/
public TextChannel getLogChannel(LogType logType) {
TextChannel textChannel = this.logChannels.get(logType);
if (textChannel == null) {
return null;
}
// Ensure the channel exists
if (ChannelUtils.getTextChannel(textChannel.getId()) == null) {
this.logChannels.remove(logType);
return null;
}
return textChannel;
return ChannelUtils.getTextChannel(this.logChannels.get(logType));
}
/**
@ -55,7 +45,7 @@ public class LogProfile extends Serializable {
* @param channel - the channel
*/
public void setLogChannel(LogType logType, TextChannel channel) {
this.logChannels.put(logType, channel);
this.logChannels.put(logType, channel.getId());
}
/**
@ -75,7 +65,7 @@ public class LogProfile extends Serializable {
if (channel == null) {
return;
}
this.logChannels.put(logType, channel);
this.logChannels.put(logType, channel.getId());
}
}
}
@ -83,8 +73,8 @@ public class LogProfile extends Serializable {
@Override
public Document serialize(Gson gson) {
Document document = new Document();
for (Map.Entry<LogType, TextChannel> entry : this.logChannels.entrySet()) {
document.append(entry.getKey().name(), entry.getValue().getId());
for (Map.Entry<LogType, String> entry : this.logChannels.entrySet()) {
document.append(entry.getKey().name(), entry.getValue());
}
return document;
}

View File

@ -45,7 +45,10 @@ public enum LogType {
/**
* Guild Events
*/
INVITE_CREATE(LogCategory.GUILD);
INVITE_CREATE(LogCategory.GUILD),
EMOJI_ADD(LogCategory.GUILD),
EMOJI_REMOVE(LogCategory.GUILD),
EMOJI_RENAME(LogCategory.GUILD);
/**
* The category of the log type

View File

@ -10,6 +10,7 @@ import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion;
@ -43,10 +44,12 @@ public class ChannelListener implements EventListener {
@Override
public void onChannelCreate(@NonNull BatGuild guild, @NonNull ChannelCreateEvent event) {
log.info("Channel \"{}\" was created in guild \"{}\"", event.getChannel().getName(), guild.getName());
ChannelUnion channel = event.getChannel();
String type = formatChannelType(channel);
log.info("{} \"{}\" was created in guild \"{}\"", type, event.getChannel().getName(), guild.getName());
logFeature.sendLog(guild, LogType.CHANNEL_CREATE, EmbedUtils.successEmbed()
.setDescription(new EmbedDescriptionBuilder("%s Channel Created".formatted(EnumUtils.getEnumName(event.getChannel().getType())))
.appendLine("Channel: %s".formatted(event.getChannel().getAsMention()), true)
.setDescription(new EmbedDescriptionBuilder("%s Created".formatted(type))
.appendLine("%s: %s".formatted(type, event.getChannel().getAsMention()), true)
.appendLine("Name: %s".formatted(event.getChannel().getName()), true)
.build())
.build());
@ -54,13 +57,14 @@ public class ChannelListener implements EventListener {
@Override
public void onChannelDelete(@NonNull BatGuild guild, @NonNull ChannelDeleteEvent event) {
log.info("Channel \"{}\" was deleted in guild \"{}\"", event.getChannel().getName(), guild.getName());
ChannelUnion channel = event.getChannel();
EmbedDescriptionBuilder description = new EmbedDescriptionBuilder("%s Channel Deleted".formatted(EnumUtils.getEnumName(channel.getType())))
String type = formatChannelType(channel);
log.info("{} \"{}\" was deleted in guild \"{}\"", type, event.getChannel().getName(), guild.getName());
EmbedDescriptionBuilder description = new EmbedDescriptionBuilder("%s Deleted".formatted(type))
.appendLine("Name: #%s".formatted(channel.getName()), true);
if (channel instanceof TextChannel) {
TextChannel textChannel = channel.asTextChannel();
description.appendLine("Topic: %s".formatted(textChannel.getTopic()), true);
description.appendLine("Topic: %s".formatted(textChannel.getTopic() == null ? "No Topic" : textChannel.getTopic()), true);
}
logFeature.sendLog(guild, LogType.CHANNEL_DELETE, EmbedUtils.errorEmbed().setDescription(description.build()).build());
}
@ -91,4 +95,15 @@ public class ChannelListener implements EventListener {
}
logFeature.sendLog(guild, LogType.VOICE_CHANNEL_LEAVE, EmbedUtils.errorEmbed().setDescription(description).build());
}
/**
* Formats the channel type
*
* @param channel - the channel
* @return the formatted channel type
*/
public String formatChannelType(ChannelUnion channel) {
return (channel.getType() == ChannelType.TEXT || channel.getType() == ChannelType.VOICE) ? EnumUtils.getEnumName(channel.getType()) + " Channel"
: EnumUtils.getEnumName(channel.getType());
}
}

View File

@ -9,6 +9,10 @@ import cc.fascinated.bat.model.BatGuild;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.entities.Invite;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.emoji.EmojiAddedEvent;
import net.dv8tion.jda.api.events.emoji.EmojiRemovedEvent;
import net.dv8tion.jda.api.events.emoji.update.EmojiUpdateNameEvent;
import net.dv8tion.jda.api.events.guild.invite.GuildInviteCreateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
@ -46,4 +50,38 @@ public class GuildListener implements EventListener {
.build()));
});
}
@Override
public void onEmojiAdd(@NonNull BatGuild guild, @NonNull Emoji emoji, @NonNull EmojiAddedEvent event) {
log.info("Emoji \"{}\" was added in guild \"{}\"", emoji.getName(), guild.getName());
logFeature.sendLog(guild, LogType.EMOJI_ADD, EmbedUtils.successEmbed()
.setDescription(new EmbedDescriptionBuilder("Emoji Added")
.appendLine("Emoji: %s".formatted(emoji.getFormatted()), true)
.appendLine("Name: %s".formatted(emoji.getName()), true)
.build())
.build());
}
@Override
public void onEmojiRemove(@NonNull BatGuild guild, @NonNull Emoji emoji, @NonNull EmojiRemovedEvent event) {
log.info("Emoji \"{}\" was removed in guild \"{}\"", emoji.getName(), guild.getName());
logFeature.sendLog(guild, LogType.EMOJI_REMOVE, EmbedUtils.successEmbed()
.setDescription(new EmbedDescriptionBuilder("Emoji Removed")
.appendLine("Emoji: %s".formatted(emoji.getFormatted()), true)
.appendLine("Name: %s".formatted(emoji.getName()), true)
.build())
.build());
}
@Override
public void onEmojiRename(@NonNull BatGuild guild, @NonNull Emoji emoji, @NonNull String oldName, @NonNull String newName, @NonNull EmojiUpdateNameEvent event) {
log.info("Emoji \"{}\" was renamed to \"{}\" in guild \"{}\"", oldName, newName, guild.getName());
logFeature.sendLog(guild, LogType.EMOJI_RENAME, EmbedUtils.successEmbed()
.setDescription(new EmbedDescriptionBuilder("Emoji Renamed")
.appendLine("Emoji: %s".formatted(emoji.getFormatted()), true)
.appendLine("Old Name: %s".formatted(oldName), true)
.appendLine("New Name: %s".formatted(newName), true)
.build())
.build());
}
}

View File

@ -1,7 +1,6 @@
package cc.fascinated.bat.features.reminder;
import cc.fascinated.bat.common.ChannelUtils;
import cc.fascinated.bat.service.DiscordService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;

View File

@ -2,7 +2,6 @@ package cc.fascinated.bat.features.scoresaber.profile.guild;
import cc.fascinated.bat.common.ChannelUtils;
import cc.fascinated.bat.common.Serializable;
import cc.fascinated.bat.service.DiscordService;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.NoArgsConstructor;

View File

@ -4,7 +4,6 @@ import cc.fascinated.bat.common.ChannelUtils;
import cc.fascinated.bat.common.Serializable;
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.service.DiscordService;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;

View File

@ -2,7 +2,6 @@ package cc.fascinated.bat.model;
import cc.fascinated.bat.common.ChannelUtils;
import cc.fascinated.bat.common.UserUtils;
import cc.fascinated.bat.service.DiscordService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

View File

@ -10,6 +10,9 @@ import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.entities.Invite;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent;
import net.dv8tion.jda.api.events.emoji.EmojiAddedEvent;
import net.dv8tion.jda.api.events.emoji.EmojiRemovedEvent;
import net.dv8tion.jda.api.events.emoji.update.EmojiUpdateNameEvent;
import net.dv8tion.jda.api.events.guild.GuildBanEvent;
import net.dv8tion.jda.api.events.guild.GuildUnbanEvent;
import net.dv8tion.jda.api.events.guild.invite.GuildInviteCreateEvent;
@ -351,4 +354,31 @@ public class EventService extends ListenerAdapter {
}
}
}
@Override
public void onEmojiAdded(@NotNull EmojiAddedEvent event) {
BatGuild guild = guildService.getGuild(event.getGuild().getId());
for (EventListener listener : LISTENERS) {
listener.onEmojiAdd(guild, event.getEmoji(), event);
}
}
@Override
public void onEmojiRemoved(@NotNull EmojiRemovedEvent event) {
BatGuild guild = guildService.getGuild(event.getGuild().getId());
for (EventListener listener : LISTENERS) {
listener.onEmojiRemove(guild, event.getEmoji(), event);
}
}
@Override
public void onEmojiUpdateName(@NotNull EmojiUpdateNameEvent event) {
BatGuild guild = guildService.getGuild(event.getGuild().getId());
for (EventListener listener : LISTENERS) {
listener.onEmojiRename(guild, event.getEmoji(), event.getOldName(), event.getNewName(), event);
}
}
}