From b5ac5d6a9bbc72a9dd6fe7aa0bf0ae72969eff54 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 7 Jul 2024 00:10:39 +0100 Subject: [PATCH] only allow sniped messages to be sniped for 1 hour after deletion --- .../java/cc/fascinated/bat/common/EmbedUtils.java | 1 - src/main/java/cc/fascinated/bat/features/Feature.java | 2 -- .../bat/features/autorole/command/SyncSubCommand.java | 5 ++++- .../features/leveling/command/ResetSubCommand.java | 2 -- .../features/messagesnipe/MessageSnipeFeature.java | 11 +++++++---- .../command/serverwatcher/ListSubCommand.java | 1 - .../scoresaber/command/userfeed/UserSubCommand.java | 1 - .../bat/features/spotify/command/LinkSubCommand.java | 1 - src/main/java/cc/fascinated/bat/model/BatGuild.java | 1 - .../java/cc/fascinated/bat/service/EventService.java | 1 - .../java/cc/fascinated/bat/service/UserService.java | 1 - 11 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/common/EmbedUtils.java b/src/main/java/cc/fascinated/bat/common/EmbedUtils.java index ad594ce..09b5bfa 100644 --- a/src/main/java/cc/fascinated/bat/common/EmbedUtils.java +++ b/src/main/java/cc/fascinated/bat/common/EmbedUtils.java @@ -1,7 +1,6 @@ package cc.fascinated.bat.common; import cc.fascinated.bat.config.Config; -import io.sentry.Sentry; import lombok.experimental.UtilityClass; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; diff --git a/src/main/java/cc/fascinated/bat/features/Feature.java b/src/main/java/cc/fascinated/bat/features/Feature.java index 1ff4316..d5b1607 100644 --- a/src/main/java/cc/fascinated/bat/features/Feature.java +++ b/src/main/java/cc/fascinated/bat/features/Feature.java @@ -7,8 +7,6 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; -import java.lang.reflect.Member; - /** * @author Fascinated (fascinated7) */ diff --git a/src/main/java/cc/fascinated/bat/features/autorole/command/SyncSubCommand.java b/src/main/java/cc/fascinated/bat/features/autorole/command/SyncSubCommand.java index ab885bc..5c7eeb4 100644 --- a/src/main/java/cc/fascinated/bat/features/autorole/command/SyncSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/autorole/command/SyncSubCommand.java @@ -16,7 +16,10 @@ import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import org.springframework.stereotype.Component; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; /** * @author Fascinated (fascinated7) diff --git a/src/main/java/cc/fascinated/bat/features/leveling/command/ResetSubCommand.java b/src/main/java/cc/fascinated/bat/features/leveling/command/ResetSubCommand.java index 7ed5837..999b2d3 100644 --- a/src/main/java/cc/fascinated/bat/features/leveling/command/ResetSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/leveling/command/ResetSubCommand.java @@ -7,7 +7,6 @@ import cc.fascinated.bat.features.leveling.LevelingProfile; import cc.fascinated.bat.features.leveling.UserLevel; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; -import cc.fascinated.bat.service.UserService; import lombok.NonNull; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; @@ -17,7 +16,6 @@ import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.build.OptionData; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** diff --git a/src/main/java/cc/fascinated/bat/features/messagesnipe/MessageSnipeFeature.java b/src/main/java/cc/fascinated/bat/features/messagesnipe/MessageSnipeFeature.java index 3c3099e..fbf2c76 100644 --- a/src/main/java/cc/fascinated/bat/features/messagesnipe/MessageSnipeFeature.java +++ b/src/main/java/cc/fascinated/bat/features/messagesnipe/MessageSnipeFeature.java @@ -17,6 +17,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import java.util.*; +import java.util.concurrent.TimeUnit; /** * @author Fascinated (fascinated7) @@ -27,6 +28,7 @@ public class MessageSnipeFeature extends Feature implements EventListener { * The sniped messages for each guild */ private static final Map> snipedMessages = new HashMap<>(); + private static final long SNIPED_MESSAGE_EXPIRATION = TimeUnit.HOURS.toMillis(1); @Autowired public MessageSnipeFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService) { @@ -56,7 +58,9 @@ public class MessageSnipeFeature extends Feature implements EventListener { * @return the sniped messages for the given guild */ public static SnipedMessage getDeletedMessage(BatGuild guild, String channelId) { - List messages = snipedMessages.getOrDefault(guild, new ArrayList<>()).stream().filter(message -> message.getDeletedDate() != null) + List messages = snipedMessages.getOrDefault(guild, new ArrayList<>()).stream() + .filter(message -> message.getDeletedDate() != null + && message.getDeletedDate().getTime() + SNIPED_MESSAGE_EXPIRATION > System.currentTimeMillis()) .sorted(Comparator.comparing(SnipedMessage::getDeletedDate).reversed()).toList(); for (SnipedMessage message : messages) { if (message.getDeletedDate() != null // Check if the message was deleted @@ -86,13 +90,12 @@ public class MessageSnipeFeature extends Feature implements EventListener { @Override public void onGuildMessageReceive(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull MessageReceivedEvent event) { - if (event.getAuthor().isBot()) return; - if (guild.getFeatureProfile().isFeatureDisabled(this)) { + if (event.getAuthor().isBot() || guild.getFeatureProfile().isFeatureDisabled(this)) { return; } List messages = snipedMessages.getOrDefault(guild, new ArrayList<>()); - if (messages.size() >= 10) { + if (messages.size() >= 10) { // Only store the last 10 messages messages.remove(0); } messages.add(new SnipedMessage(event.getMessage(), null)); diff --git a/src/main/java/cc/fascinated/bat/features/minecraft/command/serverwatcher/ListSubCommand.java b/src/main/java/cc/fascinated/bat/features/minecraft/command/serverwatcher/ListSubCommand.java index b52a4de..d4c94a6 100644 --- a/src/main/java/cc/fascinated/bat/features/minecraft/command/serverwatcher/ListSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/minecraft/command/serverwatcher/ListSubCommand.java @@ -13,7 +13,6 @@ import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; -import org.apache.catalina.Server; import org.springframework.stereotype.Component; import xyz.mcutils.models.server.ServerPlatform; diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/command/userfeed/UserSubCommand.java b/src/main/java/cc/fascinated/bat/features/scoresaber/command/userfeed/UserSubCommand.java index 542abc1..879214a 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/command/userfeed/UserSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/command/userfeed/UserSubCommand.java @@ -7,7 +7,6 @@ import cc.fascinated.bat.features.scoresaber.profile.guild.UserScoreFeedProfile; import cc.fascinated.bat.features.scoresaber.profile.user.ScoreSaberProfile; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; -import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.UserService; import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; diff --git a/src/main/java/cc/fascinated/bat/features/spotify/command/LinkSubCommand.java b/src/main/java/cc/fascinated/bat/features/spotify/command/LinkSubCommand.java index 9ac736e..ad787b1 100644 --- a/src/main/java/cc/fascinated/bat/features/spotify/command/LinkSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/spotify/command/LinkSubCommand.java @@ -1,6 +1,5 @@ package cc.fascinated.bat.features.spotify.command; -import cc.fascinated.bat.Consts; import cc.fascinated.bat.Emojis; import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.CommandInfo; diff --git a/src/main/java/cc/fascinated/bat/model/BatGuild.java b/src/main/java/cc/fascinated/bat/model/BatGuild.java index 091edf5..8331e0b 100644 --- a/src/main/java/cc/fascinated/bat/model/BatGuild.java +++ b/src/main/java/cc/fascinated/bat/model/BatGuild.java @@ -6,7 +6,6 @@ import cc.fascinated.bat.common.Serializable; import cc.fascinated.bat.features.FeatureProfile; import cc.fascinated.bat.features.birthday.profile.BirthdayProfile; import cc.fascinated.bat.features.counter.CounterProfile; -import cc.fascinated.bat.features.leveling.LevelingFeature; import cc.fascinated.bat.features.leveling.LevelingProfile; import cc.fascinated.bat.features.logging.LogProfile; import cc.fascinated.bat.features.minecraft.MinecraftProfile; diff --git a/src/main/java/cc/fascinated/bat/service/EventService.java b/src/main/java/cc/fascinated/bat/service/EventService.java index 40c2c4d..c9e3e55 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -18,7 +18,6 @@ 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.GenericGuildInviteEvent; 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; diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index ca3513a..5c1ccef 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -18,7 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Service; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit;