diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index 567306d..4562f3a 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -7,16 +7,19 @@ import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberLeaderboardT import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; 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.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.guild.GuildBanEvent; import net.dv8tion.jda.api.events.guild.GuildUnbanEvent; +import net.dv8tion.jda.api.events.guild.invite.GuildInviteCreateEvent; 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; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; +import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateBoostTimeEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent; import net.dv8tion.jda.api.events.guild.voice.GenericGuildVoiceEvent; @@ -30,6 +33,7 @@ import net.dv8tion.jda.api.events.user.update.UserUpdateAvatarEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent; +import java.time.OffsetDateTime; import java.util.List; /** @@ -226,7 +230,7 @@ public interface EventListener { /** * Called when a user updates their avatar * - * @param user the user that updated their avatar + * @param user the user that updated their avatar * @param oldAvatarUrl the old avatar url * @param newAvatarUrl the new avatar url */ @@ -242,6 +246,41 @@ public interface EventListener { 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 */ 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 45bf56d..875823c 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogCategory.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogCategory.java @@ -10,7 +10,8 @@ import lombok.Getter; public enum LogCategory { MESSAGE("Message"), MEMBER("Member"), - CHANNEL("Channel"); + CHANNEL("Channel"), + GUILD("Guild"); /** * 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 49e996e..cea0ce9 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogType.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogType.java @@ -30,6 +30,7 @@ public enum LogType { MEMBER_BAN(LogCategory.MEMBER, "Member Ban"), MEMBER_UNBAN(LogCategory.MEMBER, "Member Unban"), MEMBER_TIMEOUT(LogCategory.MEMBER, "Member Timeout"), + MEMBER_BOOST(LogCategory.MEMBER, "Member Boost"), /** * Channel Events @@ -37,7 +38,12 @@ public enum LogType { CHANNEL_CREATE(LogCategory.CHANNEL, "Channel Create"), CHANNEL_DELETE(LogCategory.CHANNEL, "Channel Delete"), VOICE_CHANNEL_JOIN(LogCategory.CHANNEL, "Voice Channel Join"), - VOICE_CHANNEL_LEAVE(LogCategory.CHANNEL, "Voice Channel Leave"); + VOICE_CHANNEL_LEAVE(LogCategory.CHANNEL, "Voice Channel Leave"), + + /** + * Guild Events + */ + INVITE_CREATE(LogCategory.GUILD, "Invite Create"); /** * The category of the log type diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/GuildListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/GuildListener.java new file mode 100644 index 0000000..01a68ff --- /dev/null +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/GuildListener.java @@ -0,0 +1,49 @@ +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 lombok.NonNull; +import lombok.extern.log4j.Log4j2; +import net.dv8tion.jda.api.entities.Invite; +import net.dv8tion.jda.api.events.guild.invite.GuildInviteCreateEvent; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * @author Fascinated (fascinated7) + */ +@Component +@Log4j2 +public class GuildListener implements EventListener { + private final LogFeature logFeature; + + @Autowired + public GuildListener(@NonNull ApplicationContext context) { + this.logFeature = context.getBean(LogFeature.class); + } + + @Override + public void onGuildInviteCreate(@NonNull BatGuild guild, @NonNull Invite invite, @NonNull GuildInviteCreateEvent event) { + log.info("Invite created in guild \"{}\" with code \"{}\"", guild.getGuild().getName(), invite.getCode()); + + guild.getDiscordGuild().retrieveInvites().queue(invites -> { + Invite newInvite = invites.stream().filter(i -> i.getCode().equals(invite.getCode())).findFirst().orElse(null); + if (newInvite == null) { + return; + } + newInvite.expand().queue(expanded -> logFeature.sendLog(guild, LogType.INVITE_CREATE, EmbedUtils.successEmbed() + .setDescription(new EmbedDescriptionBuilder("Invite Created") + .appendLine("Invite: %s".formatted(expanded.getUrl()), true) + .appendLine("Channel: %s".formatted(expanded.getChannel()), true) + .appendLine("Max Uses: %s".formatted(expanded.getMaxUses() == 0 ? "Infinite" : expanded.getMaxUses()), true) + .appendLine("Creator: %s".formatted(expanded.getInviter() == null ? "Unknown" : expanded.getInviter().getAsMention()), true) + .build()) + .build())); + }); + } +} 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 index 0708a87..0d6517e 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/MemberListener.java @@ -10,6 +10,7 @@ import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.GuildService; import lombok.NonNull; +import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.guild.GuildBanEvent; @@ -18,6 +19,7 @@ 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; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; +import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateBoostTimeEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateAvatarEvent; @@ -30,14 +32,14 @@ import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import java.time.OffsetDateTime; +import java.time.temporal.ChronoUnit; import java.util.List; /** * @author Fascinated (fascinated7) */ -@Component +@Component @Log4j2 public class MemberListener implements EventListener { - private static final Logger log = LoggerFactory.getLogger(MemberListener.class); private final LogFeature logFeature; private final GuildService guildService; @@ -226,4 +228,38 @@ public class MemberListener implements EventListener { .build()) .build()); } + + @Override + public void onGuildMemberBoostAdd(@NonNull BatGuild guild, @NonNull BatUser user, OffsetDateTime oldBoostTime, + @NonNull OffsetDateTime newBoostTime, @NonNull GuildMemberUpdateBoostTimeEvent event) { + if (user.getDiscordUser().isBot()) return; + log.info("User \"{}\" boosted the guild \"{}\" until \"{}\"", user.getName(), guild.getDiscordGuild().getName(), newBoostTime); + + oldBoostTime = oldBoostTime == null ? OffsetDateTime.now() : oldBoostTime; + long boostAmount = ChronoUnit.MONTHS.between(oldBoostTime, newBoostTime); + long seconds = newBoostTime.toInstant().getEpochSecond(); + logFeature.sendLog(guild, LogType.MEMBER_BOOST, EmbedUtils.successEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Boosted") + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Boost Months: %s".formatted(boostAmount), true) + .appendLine("Boost End: ".formatted(seconds), true) + .build()) + .build()); + } + + @Override + public void onGuildMemberBoostRemove(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull OffsetDateTime oldBoostTime, + OffsetDateTime newBoostTime, @NonNull GuildMemberUpdateBoostTimeEvent event) { + if (user.getDiscordUser().isBot()) return; + log.info("User \"{}\" boost expired in the guild \"{}\"", user.getName(), guild.getDiscordGuild().getName()); + + newBoostTime = newBoostTime == null ? OffsetDateTime.now() : newBoostTime; + long boostAmount = ChronoUnit.MONTHS.between(newBoostTime, oldBoostTime); + logFeature.sendLog(guild, LogType.MEMBER_BOOST, EmbedUtils.errorEmbed() + .setDescription(new EmbedDescriptionBuilder("Member Boost Expired") + .appendLine("Member: %s".formatted(user.getDiscordUser().getAsMention()), true) + .appendLine("Boosts Removed: %s".formatted(boostAmount), true) + .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 752cbec..872352d 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -7,14 +7,17 @@ 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.entities.Invite; import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; 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; 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; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; +import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateBoostTimeEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; @@ -315,4 +318,39 @@ public class EventService extends ListenerAdapter { listener.onGuildVoiceUpdate(guild, user, event); } } + + @Override + public void onGuildInviteCreate(@NotNull GuildInviteCreateEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + Invite invite = event.getInvite(); + + for (EventListener listener : LISTENERS) { + listener.onGuildInviteCreate(guild, invite, event); + } + } + + @Override + public void onGuildMemberUpdateBoostTime(@NotNull GuildMemberUpdateBoostTimeEvent event) { + if (event.getUser().isBot()) { + return; + } + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + BatUser user = userService.getUser(event.getUser().getId(), event.getUser()); + + // Boost tier(s) added + if (event.getNewTimeBoosted() != null) { + for (EventListener listener : LISTENERS) { + listener.onGuildMemberBoostAdd(guild, user, event.getOldTimeBoosted(), event.getNewTimeBoosted(), event); + return; + } + } + + // Boost tier(s) removed + if (event.getOldTimeBoosted() != null) { + for (EventListener listener : LISTENERS) { + listener.onGuildMemberBoostRemove(guild, user, event.getOldTimeBoosted(), event.getNewTimeBoosted(), event); + return; + } + } + } }