2323import net .javadiscord .javabot .systems .help .dao .HelpAccountRepository ;
2424import net .javadiscord .javabot .systems .help .dao .HelpTransactionRepository ;
2525import net .javadiscord .javabot .util .ExceptionLogger ;
26+ import net .javadiscord .javabot .util .InteractionUtils ;
2627import net .javadiscord .javabot .util .Responses ;
2728import org .jetbrains .annotations .NotNull ;
2829import org .springframework .dao .DataAccessException ;
3233import java .util .ArrayList ;
3334import java .util .HashMap ;
3435import java .util .HashSet ;
36+ import java .util .LinkedHashMap ;
3537import java .util .List ;
3638import java .util .Map ;
3739import java .util .Set ;
@@ -57,6 +59,21 @@ public class HelpListener extends ListenerAdapter implements ButtonHandler {
5759 private final HelpAccountRepository helpAccountRepository ;
5860 private final HelpTransactionRepository helpTransactionRepository ;
5961 private final DbActions dbActions ;
62+ private final String [][] closeSuggestionDetectors = {
63+ {"close" , "post" },
64+ {"close" , "thread" },
65+ {"close" , "question" },
66+ {"problem" ,"solv" },
67+ {"issue" ,"solv" },
68+ {"thank" }
69+ };
70+ private final long SUGGEST_CLOSE_TIMEOUT = 5 * 60_000L ;//5 minutes
71+ private final Map <Long , Long > recentlyCloseSuggestedPosts = new LinkedHashMap <>(8 , 0.75f , true ) {
72+ @ Override
73+ protected boolean removeEldestEntry (Map .Entry <Long , Long > eldest ) {
74+ return System .currentTimeMillis () > eldest .getValue () || size () >= 32 ;
75+ }
76+ };
6077
6178 @ Override
6279 public void onMessageReceived (@ NotNull MessageReceivedEvent event ) {
@@ -67,7 +84,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
6784 ThreadChannel post = event .getChannel ().asThreadChannel ();
6885 // send post buttons
6986 post .sendMessageComponents (ActionRow .of (
70- Button . primary ( ComponentIdBuilder . build ( HelpManager . HELP_CLOSE_IDENTIFIER , post . getIdLong ()), "Close Post" ),
87+ createCloseSuggestionButton ( post ),
7188 Button .secondary (ComponentIdBuilder .build (HelpManager .HELP_GUIDELINES_IDENTIFIER ), "View Help Guidelines" )
7289 )).queue (success -> post .sendMessageFormat (config .getReservedChannelMessageTemplate (), UserSnowflake .fromId (post .getOwnerId ()).getAsMention (), config .getInactivityTimeoutMinutes ()).queue ());
7390 newThreadChannels .remove (event .getChannel ().getIdLong ());
@@ -91,6 +108,49 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
91108 messages .addAll (HELP_POST_MESSAGES .get (post .getIdLong ()));
92109 }
93110 HELP_POST_MESSAGES .put (post .getIdLong (), messages );
111+ // suggest to close post on "problem solved"-messages
112+ replyCloseSuggestionIfPatternMatches (event .getMessage ());
113+ }
114+
115+ private void replyCloseSuggestionIfPatternMatches (Message msg ) {
116+ String content = msg .getContentRaw ().toLowerCase ();
117+ if (content .contains ("```" )) {
118+ return ;
119+ }
120+ long postId = msg .getChannel ().getIdLong ();
121+ if (recentlyCloseSuggestedPosts .containsKey (postId ) && recentlyCloseSuggestedPosts .get (postId ) > System .currentTimeMillis ()) {
122+ return ;
123+ }
124+ if (msg .getChannel ().asThreadChannel ().getOwnerIdLong () == msg .getAuthor ().getIdLong ()) {
125+ for (String [] detector : closeSuggestionDetectors ) {
126+ if (doesMatchDetector (content , detector )) {
127+ msg .reply ("""
128+ If you are finished with your post, please close it.
129+ If you are not, please ignore this message.
130+ Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
131+ """ )
132+ .addActionRow (createCloseSuggestionButton (msg .getChannel ().asThreadChannel ()),
133+ Button .secondary (InteractionUtils .DELETE_ORIGINAL_TEMPLATE , "\uD83D \uDDD1 ️" ))
134+ .queue ();
135+ recentlyCloseSuggestedPosts .put (postId , System .currentTimeMillis () + SUGGEST_CLOSE_TIMEOUT );
136+ }
137+ }
138+ }
139+ }
140+
141+ private boolean doesMatchDetector (String content , String [] detector ) {
142+ int currentIndex = 0 ;
143+ for (String keyword : detector ) {
144+ currentIndex = content .indexOf (keyword );
145+ if (currentIndex == -1 ) {
146+ return false ;
147+ }
148+ }
149+ return true ;
150+ }
151+
152+ private Button createCloseSuggestionButton (ThreadChannel post ) {
153+ return Button .primary (ComponentIdBuilder .build (HelpManager .HELP_CLOSE_IDENTIFIER , post .getIdLong ()), "Close Post" );
94154 }
95155
96156 @ Override
0 commit comments