|
| 1 | +package net.discordjug.javabot.systems.help.commands.notify; |
| 2 | + |
| 3 | +import java.nio.charset.StandardCharsets; |
| 4 | +import java.time.OffsetDateTime; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +import org.springframework.scheduling.annotation.Scheduled; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | + |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import net.discordjug.javabot.data.config.BotConfig; |
| 14 | +import net.discordjug.javabot.util.ExceptionLogger; |
| 15 | +import net.dv8tion.jda.api.JDA; |
| 16 | +import net.dv8tion.jda.api.entities.Guild; |
| 17 | +import net.dv8tion.jda.api.entities.Message; |
| 18 | +import net.dv8tion.jda.api.entities.MessageHistory; |
| 19 | +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; |
| 20 | +import net.dv8tion.jda.api.utils.FileUpload; |
| 21 | + |
| 22 | +/** |
| 23 | + * Automatically delete old help notifications. |
| 24 | + * Once a day, this class deletes old messages of the bot in the help notification channel. |
| 25 | + */ |
| 26 | +@Service |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class ClearOldHelpNotificationJob { |
| 29 | + private final BotConfig botConfig; |
| 30 | + private final JDA jda; |
| 31 | + |
| 32 | + /** |
| 33 | + * Runs the message deletion. |
| 34 | + */ |
| 35 | + @Scheduled(cron="0 0 0 * * *")//00:00 UTC |
| 36 | + public void execute() { |
| 37 | + for (Guild guild : jda.getGuilds()) { |
| 38 | + TextChannel helpNotificationChannel = botConfig.get(guild).getHelpConfig().getHelpNotificationChannel(); |
| 39 | + if(helpNotificationChannel != null) { |
| 40 | + MessageHistory history = helpNotificationChannel.getHistory(); |
| 41 | + deleteOldMessagesInChannel(helpNotificationChannel, history, new ArrayList<>()); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void deleteOldMessagesInChannel(TextChannel helpNotificationChannel, MessageHistory history, List<Message> foundSoFar) { |
| 47 | + history.retrievePast(50).queue(msgs -> { |
| 48 | + List<Message> toDelete = msgs |
| 49 | + .stream() |
| 50 | + .filter(msg -> msg.getAuthor().getIdLong() == msg.getJDA().getSelfUser().getIdLong()) |
| 51 | + .filter(msg -> msg.getTimeCreated().isBefore(OffsetDateTime.now().minusDays(3))) |
| 52 | + .filter(msg -> msg |
| 53 | + .getButtons() |
| 54 | + .stream() |
| 55 | + .anyMatch(button -> "Mark as unacknowledged".equals(button.getLabel()))) |
| 56 | + .toList(); |
| 57 | + helpNotificationChannel.purgeMessages(toDelete); |
| 58 | + foundSoFar.addAll(toDelete); |
| 59 | + if (!msgs.isEmpty()) { |
| 60 | + deleteOldMessagesInChannel(helpNotificationChannel, history, foundSoFar); |
| 61 | + }else { |
| 62 | + if (foundSoFar.size() > 50) { |
| 63 | + String messageInfo = foundSoFar |
| 64 | + .stream() |
| 65 | + .map(msg -> convertMessageToString(msg)) |
| 66 | + .collect(Collectors.joining("\n\n===============\n\n")); |
| 67 | + botConfig.get(helpNotificationChannel.getGuild()).getModerationConfig().getLogChannel() |
| 68 | + .sendMessageFormat("Warning: deleting %d messages in help notification channel, this might be due to help notification spam", foundSoFar.size()) |
| 69 | + .addFiles(FileUpload.fromData(messageInfo.getBytes(StandardCharsets.UTF_8), "messages.txt")) |
| 70 | + .queue(); |
| 71 | + } |
| 72 | + } |
| 73 | + }, e -> { |
| 74 | + ExceptionLogger.capture(e, getClass().getName()); |
| 75 | + helpNotificationChannel.purgeMessages(foundSoFar); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private String convertMessageToString(Message msg) { |
| 80 | + return msg.getContentRaw()+"\n"+ |
| 81 | + msg.getEmbeds().stream().map(e->e.toData().toString()).collect(Collectors.joining("\n")); |
| 82 | + } |
| 83 | +} |
0 commit comments