From bf41eac05168434e0b8d49d67609f64957d3bceb Mon Sep 17 00:00:00 2001 From: danthe1st Date: Tue, 23 Apr 2024 12:24:16 +0200 Subject: [PATCH] automatically clear old unresolved help notifications --- .../notify/ClearOldHelpNotificationJob.java | 28 ++++++++++++++++--- .../commands/notify/HelpPingSubcommand.java | 6 ++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/discordjug/javabot/systems/help/commands/notify/ClearOldHelpNotificationJob.java b/src/main/java/net/discordjug/javabot/systems/help/commands/notify/ClearOldHelpNotificationJob.java index 3eeb9b0df..768868600 100644 --- a/src/main/java/net/discordjug/javabot/systems/help/commands/notify/ClearOldHelpNotificationJob.java +++ b/src/main/java/net/discordjug/javabot/systems/help/commands/notify/ClearOldHelpNotificationJob.java @@ -49,10 +49,9 @@ private void deleteOldMessagesInChannel(TextChannel helpNotificationChannel, Mes .stream() .filter(msg -> msg.getAuthor().getIdLong() == msg.getJDA().getSelfUser().getIdLong()) .filter(msg -> msg.getTimeCreated().isBefore(OffsetDateTime.now().minusDays(3))) - .filter(msg -> msg - .getButtons() - .stream() - .anyMatch(button -> "Mark as unacknowledged".equals(button.getLabel()))) + .filter(msg -> + isOldUnresolvedNotification(msg) || + isResolvedNotification(msg)) .toList(); helpNotificationChannel.purgeMessages(toDelete); foundSoFar.addAll(toDelete); @@ -76,6 +75,27 @@ private void deleteOldMessagesInChannel(TextChannel helpNotificationChannel, Mes }); } + private boolean isOldUnresolvedNotification(Message msg) { + return getLastInteractionTimestamp(msg) + .isBefore(OffsetDateTime.now().minusDays(7)) && + hasButtonWithText(msg, HelpPingSubcommand.MARK_ACKNOWLEDGED_BUTTON_TEXT); + } + + private OffsetDateTime getLastInteractionTimestamp(Message msg) { + OffsetDateTime timeEdited = msg.getTimeEdited(); + return timeEdited == null ? msg.getTimeCreated() : timeEdited; + } + + private boolean isResolvedNotification(Message msg) { + return hasButtonWithText(msg, HelpPingSubcommand.MARK_UNACKNOWLEDGED_BUTTON_TEXT); + } + + private boolean hasButtonWithText(Message msg, String expectedText) { + return msg.getButtons() + .stream() + .anyMatch(button -> expectedText.equals(button.getLabel())); + } + private String convertMessageToString(Message msg) { return msg.getContentRaw()+"\n"+ msg.getEmbeds().stream().map(e->e.toData().toString()).collect(Collectors.joining("\n")); diff --git a/src/main/java/net/discordjug/javabot/systems/help/commands/notify/HelpPingSubcommand.java b/src/main/java/net/discordjug/javabot/systems/help/commands/notify/HelpPingSubcommand.java index e9b8bf133..2f5b3cac5 100644 --- a/src/main/java/net/discordjug/javabot/systems/help/commands/notify/HelpPingSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/help/commands/notify/HelpPingSubcommand.java @@ -48,6 +48,8 @@ */ @AutoDetectableComponentHandler("help-ping") public class HelpPingSubcommand extends SlashCommand.Subcommand implements ButtonHandler { + static final String MARK_UNACKNOWLEDGED_BUTTON_TEXT = "Mark as unacknowledged"; + static final String MARK_ACKNOWLEDGED_BUTTON_TEXT = "Mark as acknowledged"; private static final String WRONG_CHANNEL_MSG = "This command can only be used in **help forum posts**"; private static final long CACHE_CLEANUP_DELAY = 60L; @@ -161,11 +163,11 @@ private void appendComment(EmbedBuilder eb, String comment) { } private Button createAcknowledgementButton(String postId) { - return Button.of(ButtonStyle.SECONDARY, ComponentIdBuilder.build("help-ping", "acknowledge", postId), "Mark as acknowledged"); + return Button.of(ButtonStyle.SECONDARY, ComponentIdBuilder.build("help-ping", "acknowledge", postId), MARK_ACKNOWLEDGED_BUTTON_TEXT); } private Button createUndoAcknowledgementButton(String postId) { - return Button.of(ButtonStyle.SECONDARY, ComponentIdBuilder.build("help-ping", "unacknowledge", postId), "Mark as unacknowledged"); + return Button.of(ButtonStyle.SECONDARY, ComponentIdBuilder.build("help-ping", "unacknowledge", postId), MARK_UNACKNOWLEDGED_BUTTON_TEXT); } /**