From bcaaa7b3d44be3257f8b52860d59f952476bf22b Mon Sep 17 00:00:00 2001 From: billpapat <50835814+billpapat@users.noreply.github.com> Date: Tue, 1 Jul 2025 03:07:42 +0300 Subject: [PATCH] Notify proper post deletion Upon the deletion of a message within a help post, a system notification will be displayed, providing guidance on the proper procedure for deleting the post. --- .../help/HelpThreadCreatedListener.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java index 12ee788fa1..8653a08007 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java @@ -11,9 +11,12 @@ import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.forums.ForumTag; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.events.message.MessageDeleteEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.requests.RestAction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.features.EventReceiver; import org.togetherjava.tjbot.features.UserInteractionType; @@ -38,6 +41,9 @@ */ public final class HelpThreadCreatedListener extends ListenerAdapter implements EventReceiver, UserInteractor { + + private static final Logger LOGGER = LoggerFactory.getLogger(HelpThreadCreatedListener.class); + private final HelpSystemHelper helper; private final Cache threadIdToCreatedAtCache = Caffeine.newBuilder() @@ -71,6 +77,34 @@ public void onMessageReceived(MessageReceivedEvent event) { } } + @Override + public void onMessageDelete(MessageDeleteEvent event) { + // Check if the deleted message was from a thread + if (event.isFromThread()) { + ThreadChannel threadChannel = event.getChannel().asThreadChannel(); + Channel parentChannel = threadChannel.getParentChannel(); + + // Check if it's a help forum thread + if (helper.isHelpForumName(parentChannel.getName())) { + // Retrieve the thread owner using the owner's ID + long ownerId = threadChannel.getOwnerIdLong(); + threadChannel.getGuild() + .retrieveMemberById(ownerId) + .queue(member -> threadChannel + .sendMessage( + """ + It looks like a message was deleted in this help post.\s + To properly close this post, please __right-click__ on the **Created Post** __followed by__ **Delete Post**\s + instead of deleting messages. This ensures proper record-keeping and\s + helps other users find solutions.""") + .queue(), + failure -> LOGGER.error( + "Could not retrieve thread owner for thread ID {}: {}", + threadChannel.getId(), failure.getMessage(), failure)); + } + } + } + private boolean wasThreadAlreadyHandled(long threadChannelId) { // NOTE Discord/JDA fires this event twice per thread (bug?), we work around by remembering // the threads we already handled