|
| 1 | +package net.discordjug.javabot.systems.help; |
| 2 | + |
| 3 | +import java.time.OffsetDateTime; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.springframework.scheduling.annotation.Scheduled; |
| 8 | +import org.springframework.stereotype.Service; |
| 9 | + |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import net.discordjug.javabot.data.config.BotConfig; |
| 12 | +import net.discordjug.javabot.util.ExceptionLogger; |
| 13 | +import net.dv8tion.jda.api.JDA; |
| 14 | +import net.dv8tion.jda.api.entities.Guild; |
| 15 | +import net.dv8tion.jda.api.entities.Message; |
| 16 | +import net.dv8tion.jda.api.entities.MessageHistory; |
| 17 | +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; |
| 18 | + |
| 19 | +/** |
| 20 | + * Automatically delete old help notifications. |
| 21 | + * Once a day, this class deletes old messages of the bot in the help notification channel. |
| 22 | + */ |
| 23 | +@Service |
| 24 | +@RequiredArgsConstructor |
| 25 | +public class ClearOldHelpNotificationJob { |
| 26 | + private final BotConfig botConfig; |
| 27 | + private final JDA jda; |
| 28 | + |
| 29 | + /** |
| 30 | + * Runs the message deletion. |
| 31 | + */ |
| 32 | + @Scheduled(cron="0 0 0 * * *")//00:00 UTC |
| 33 | + public void execute() { |
| 34 | + for (Guild guild : jda.getGuilds()) { |
| 35 | + TextChannel helpNotificationChannel = botConfig.get(guild).getHelpConfig().getHelpNotificationChannel(); |
| 36 | + if(helpNotificationChannel != null) { |
| 37 | + MessageHistory history = helpNotificationChannel.getHistory(); |
| 38 | + deleteOldMessagesInChannel(helpNotificationChannel, history, new ArrayList<>()); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private void deleteOldMessagesInChannel(TextChannel helpNotificationChannel, MessageHistory history, List<Message> foundSoFar) { |
| 44 | + history.retrievePast(50).queue(msgs -> { |
| 45 | + boolean deleteMore = addMessagesToDelete(foundSoFar, msgs); |
| 46 | + if (deleteMore) { |
| 47 | + deleteOldMessagesInChannel(helpNotificationChannel, history, foundSoFar); |
| 48 | + }else { |
| 49 | + helpNotificationChannel.purgeMessages(foundSoFar); |
| 50 | + } |
| 51 | + }, e -> { |
| 52 | + ExceptionLogger.capture(e, getClass().getName()); |
| 53 | + helpNotificationChannel.purgeMessages(foundSoFar); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + private boolean addMessagesToDelete(List<Message> toDelete, List<Message> msgs) { |
| 58 | + for (Message message : msgs) { |
| 59 | + if (message.getAuthor().getIdLong() == message.getJDA().getSelfUser().getIdLong() && |
| 60 | + //only delete messages older than 5 days |
| 61 | + message.getTimeCreated().isBefore(OffsetDateTime.now().minusDays(5))) { |
| 62 | + |
| 63 | + //only messages sent within the past two weeks can be deleted |
| 64 | + //stop when messages near that are found |
| 65 | + if (message.getTimeCreated().isBefore(OffsetDateTime.now().minusDays(13))) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + toDelete.add(message); |
| 69 | + } |
| 70 | + } |
| 71 | + return true; |
| 72 | + } |
| 73 | +} |
0 commit comments