Skip to content

Commit 0d2f654

Browse files
authored
Pin the correct message in questions thread (#893)
1 parent 68deee0 commit 0d2f654

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
1010
import net.dv8tion.jda.api.hooks.ListenerAdapter;
1111
import net.dv8tion.jda.api.requests.RestAction;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
1412

1513
import org.togetherjava.tjbot.features.EventReceiver;
1614

1715
import java.time.Instant;
1816
import java.time.temporal.ChronoUnit;
19-
import java.util.concurrent.Executors;
20-
import java.util.concurrent.ScheduledExecutorService;
2117
import java.util.concurrent.TimeUnit;
2218

2319
/**
@@ -27,8 +23,6 @@
2723
* user.
2824
*/
2925
public 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

Comments
 (0)