99import net .dv8tion .jda .api .events .channel .ChannelCreateEvent ;
1010import net .dv8tion .jda .api .hooks .ListenerAdapter ;
1111import net .dv8tion .jda .api .requests .RestAction ;
12- import org .slf4j .Logger ;
13- import org .slf4j .LoggerFactory ;
1412
1513import org .togetherjava .tjbot .features .EventReceiver ;
1614
1715import java .time .Instant ;
1816import java .time .temporal .ChronoUnit ;
19- import java .util .concurrent .Executors ;
20- import java .util .concurrent .ScheduledExecutorService ;
2117import java .util .concurrent .TimeUnit ;
2218
2319/**
2723 * user.
2824 */
2925public final class HelpThreadCreatedListener extends ListenerAdapter implements EventReceiver {
30- private static final Logger logger = LoggerFactory .getLogger (HelpThreadCreatedListener .class );
31- private static final ScheduledExecutorService SERVICE = Executors .newScheduledThreadPool (2 );
3226
3327 private final HelpSystemHelper helper ;
3428 private final Cache <Long , Instant > threadIdToCreatedAtCache = Caffeine .newBuilder ()
@@ -74,20 +68,11 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) {
7468 private void handleHelpThreadCreated (ThreadChannel threadChannel ) {
7569 helper .writeHelpThreadToDatabase (threadChannel .getOwnerIdLong (), threadChannel );
7670
77- Runnable createMessages = () -> {
78- try {
79- createMessages (threadChannel ).queue ();
80- } catch (Exception e ) {
81- logger .error (
82- "Unknown error while creating messages after help-thread ({}) creation" ,
83- threadChannel .getId (), e );
84- }
85- };
86-
8771 // The creation is delayed, because otherwise it could be too fast and be executed
8872 // after Discord created the thread, but before Discord send OPs initial message.
8973 // Sending messages at that moment is not allowed.
90- SERVICE .schedule (createMessages , 5 , TimeUnit .SECONDS );
74+ createMessages (threadChannel ).and (pinOriginalQuestion (threadChannel ))
75+ .queueAfter (5 , TimeUnit .SECONDS );
9176 }
9277
9378 private RestAction <Message > createAIResponse (ThreadChannel threadChannel ) {
@@ -97,8 +82,12 @@ private RestAction<Message> createAIResponse(ThreadChannel threadChannel) {
9782 message -> helper .constructChatGptAttempt (threadChannel , message .getContentRaw ()));
9883 }
9984
85+ private RestAction <Void > pinOriginalQuestion (ThreadChannel threadChannel ) {
86+ return threadChannel .retrieveMessageById (threadChannel .getIdLong ()).flatMap (Message ::pin );
87+ }
88+
10089 private RestAction <Message > createMessages (ThreadChannel threadChannel ) {
101- return sendHelperHeadsUp (threadChannel ). flatMap ( Message :: pin )
90+ return sendHelperHeadsUp (threadChannel )
10291 .flatMap (any -> helper .sendExplanationMessage (threadChannel ))
10392 .flatMap (any -> createAIResponse (threadChannel ));
10493 }
0 commit comments