77import net .dv8tion .jda .api .entities .MessageEmbed ;
88import net .dv8tion .jda .api .entities .Role ;
99import net .dv8tion .jda .api .entities .User ;
10+ import net .dv8tion .jda .api .entities .channel .Channel ;
1011import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
1112import net .dv8tion .jda .api .entities .channel .forums .ForumTag ;
12- import net .dv8tion .jda .api .events .channel .ChannelCreateEvent ;
1313import net .dv8tion .jda .api .events .interaction .component .ButtonInteractionEvent ;
14+ import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
1415import net .dv8tion .jda .api .hooks .ListenerAdapter ;
1516import net .dv8tion .jda .api .requests .RestAction ;
17+ import org .jetbrains .annotations .NotNull ;
1618
1719import org .togetherjava .tjbot .features .EventReceiver ;
1820import org .togetherjava .tjbot .features .UserInteractionType ;
@@ -56,20 +58,18 @@ public HelpThreadCreatedListener(HelpSystemHelper helper) {
5658 }
5759
5860 @ Override
59- public void onChannelCreate (ChannelCreateEvent createEvent ) {
60- if (!createEvent .getChannelType ().isThread ()) {
61- return ;
62- }
63- ThreadChannel threadChannel = createEvent .getChannel ().asThreadChannel ();
64-
65- if (wasThreadAlreadyHandled (threadChannel .getIdLong ())) {
66- return ;
67- }
68-
69- if (!helper .isHelpForumName (threadChannel .getParentChannel ().getName ())) {
70- return ;
61+ public void onMessageReceived (@ NotNull MessageReceivedEvent event ) {
62+ if (event .isFromThread ()) {
63+ Channel parentChannel = event .getChannel ().asThreadChannel ().getParentChannel ();
64+ if (helper .isHelpForumName (parentChannel .getName ())) {
65+ ThreadChannel threadChannel = event .getChannel ().asThreadChannel ();
66+ int messageCount = threadChannel .getMessageCount ();
67+ if (messageCount > 1 || wasThreadAlreadyHandled (threadChannel .getIdLong ())) {
68+ return ;
69+ }
70+ handleHelpThreadCreated (threadChannel );
71+ }
7172 }
72- handleHelpThreadCreated (threadChannel );
7373 }
7474
7575 private boolean wasThreadAlreadyHandled (long threadChannelId ) {
@@ -82,23 +82,10 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) {
8282 }
8383
8484 private void handleHelpThreadCreated (ThreadChannel threadChannel ) {
85- threadChannel .retrieveMessageById (threadChannel .getIdLong ()).queue (message -> {
86-
87- long authorId = threadChannel .getOwnerIdLong ();
88-
89- if (isPostedBySelfUser (message )) {
90- // When transfer-command is used
91- authorId = getMentionedAuthorByMessage (message ).getIdLong ();
92- }
93-
94- helper .writeHelpThreadToDatabase (authorId , threadChannel );
95- });
96-
97- // The creation is delayed, because otherwise it could be too fast and be executed
98- // after Discord created the thread, but before Discord send OPs initial message.
99- // Sending messages at that moment is not allowed.
100- createMessages (threadChannel ).and (pinOriginalQuestion (threadChannel ))
101- .queueAfter (5 , TimeUnit .SECONDS );
85+ threadChannel .retrieveStartMessage ().flatMap (message -> {
86+ registerThreadDataInDB (message , threadChannel );
87+ return generateAutomatedResponse (threadChannel );
88+ }).flatMap (message -> pinOriginalQuestion (threadChannel )).queue ();
10289 }
10390
10491 private static User getMentionedAuthorByMessage (Message message ) {
@@ -126,7 +113,7 @@ private RestAction<Void> pinOriginalQuestion(ThreadChannel threadChannel) {
126113 return threadChannel .retrieveMessageById (threadChannel .getIdLong ()).flatMap (Message ::pin );
127114 }
128115
129- private RestAction <Message > createMessages (ThreadChannel threadChannel ) {
116+ private RestAction <Message > generateAutomatedResponse (ThreadChannel threadChannel ) {
130117 return sendHelperHeadsUp (threadChannel ).flatMap (any -> createAIResponse (threadChannel ));
131118 }
132119
@@ -228,4 +215,15 @@ private void handleDismiss(Member interactionUser, ThreadChannel channel,
228215 }
229216 deleteMessages .queue ();
230217 }
218+
219+ private void registerThreadDataInDB (Message message , ThreadChannel threadChannel ) {
220+ long authorId = threadChannel .getOwnerIdLong ();
221+
222+ if (isPostedBySelfUser (message )) {
223+ // When transfer-command is used
224+ authorId = getMentionedAuthorByMessage (message ).getIdLong ();
225+ }
226+
227+ helper .writeHelpThreadToDatabase (authorId , threadChannel );
228+ }
231229}
0 commit comments