Skip to content

Commit

Permalink
Merge pull request #486 from danthe1st/clear-old-notifs
Browse files Browse the repository at this point in the history
Also clear unresolved help notifications after one week
  • Loading branch information
MoonTM-GIT authored Jun 24, 2024
2 parents 3c9a497 + bf41eac commit 73838c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 73838c5

Please sign in to comment.