1616import net .dv8tion .jda .api .interactions .components .ActionComponent ;
1717import net .dv8tion .jda .api .interactions .components .ActionRow ;
1818import net .dv8tion .jda .api .interactions .components .buttons .Button ;
19+ import net .dv8tion .jda .api .utils .messages .MessageCreateBuilder ;
1920import net .javadiscord .javabot .annotations .AutoDetectableComponentHandler ;
2021import net .javadiscord .javabot .data .config .BotConfig ;
2122import net .javadiscord .javabot .data .config .guild .HelpConfig ;
@@ -57,6 +58,14 @@ public class HelpListener extends ListenerAdapter implements ButtonHandler {
5758 private final HelpAccountRepository helpAccountRepository ;
5859 private final HelpTransactionRepository helpTransactionRepository ;
5960 private final DbActions dbActions ;
61+ private final String [][] closeSuggestionDetectors = {
62+ {"close" , "post" },
63+ {"close" , "thread" },
64+ {"close" , "question" },
65+ {"problem" ,"solv" },
66+ {"issue" ,"solv" },
67+ {"thank" }
68+ };
6069
6170 @ Override
6271 public void onMessageReceived (@ NotNull MessageReceivedEvent event ) {
@@ -67,7 +76,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
6776 ThreadChannel post = event .getChannel ().asThreadChannel ();
6877 // send post buttons
6978 post .sendMessageComponents (ActionRow .of (
70- Button . primary ( ComponentIdBuilder . build ( HelpManager . HELP_CLOSE_IDENTIFIER , post . getIdLong ()), "Close Post" ),
79+ createCloseSuggestionButton ( post ),
7180 Button .secondary (ComponentIdBuilder .build (HelpManager .HELP_GUIDELINES_IDENTIFIER ), "View Help Guidelines" )
7281 )).queue (success -> post .sendMessageFormat (config .getReservedChannelMessageTemplate (), UserSnowflake .fromId (post .getOwnerId ()).getAsMention (), config .getInactivityTimeoutMinutes ()).queue ());
7382 newThreadChannels .remove (event .getChannel ().getIdLong ());
@@ -91,6 +100,42 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
91100 messages .addAll (HELP_POST_MESSAGES .get (post .getIdLong ()));
92101 }
93102 HELP_POST_MESSAGES .put (post .getIdLong (), messages );
103+ // suggest to close post on "problem solved"-messages
104+ replyCloseSuggestionIfPatternMatches (event .getMessage ());
105+ }
106+
107+ private void replyCloseSuggestionIfPatternMatches (Message msg ) {
108+ String content = msg .getContentRaw ().toLowerCase ();
109+ if (msg .getChannel ().asThreadChannel ().getOwnerIdLong () == msg .getAuthor ().getIdLong ()) {
110+ for (String [] detector : closeSuggestionDetectors ) {
111+ if (doesMatchDetector (content , detector )) {
112+ msg .reply (new MessageCreateBuilder ()
113+ .addContent ("""
114+ If you are finished with your post, please close your post.
115+ If you are not, please ignore this message.
116+ Note that you will not be able to send further messages after this post have been closed.
117+ """ )
118+ .addActionRow (createCloseSuggestionButton (msg .getChannel ().asThreadChannel ()))
119+ .build ())
120+ .queue ();
121+ }
122+ }
123+ }
124+ }
125+
126+ private boolean doesMatchDetector (String content , String [] detector ) {
127+ int currentIndex = 0 ;
128+ for (String keyword : detector ) {
129+ currentIndex = content .indexOf (keyword );
130+ if (currentIndex == -1 ) {
131+ return false ;
132+ }
133+ }
134+ return true ;
135+ }
136+
137+ private Button createCloseSuggestionButton (ThreadChannel post ) {
138+ return Button .primary (ComponentIdBuilder .build (HelpManager .HELP_CLOSE_IDENTIFIER , post .getIdLong ()), "Close Post" );
94139 }
95140
96141 @ Override
0 commit comments