|
9 | 9 | import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; |
10 | 10 | import net.dv8tion.jda.api.hooks.ListenerAdapter; |
11 | 11 | import net.dv8tion.jda.api.requests.RestAction; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
12 | 14 |
|
13 | 15 | import org.togetherjava.tjbot.commands.EventReceiver; |
14 | 16 |
|
15 | 17 | import javax.annotation.Nonnull; |
16 | 18 |
|
17 | 19 | import java.time.Instant; |
18 | 20 | import java.time.temporal.ChronoUnit; |
| 21 | +import java.util.concurrent.Executors; |
| 22 | +import java.util.concurrent.ScheduledExecutorService; |
19 | 23 | import java.util.concurrent.TimeUnit; |
20 | 24 |
|
21 | 25 | /** |
|
25 | 29 | * user. |
26 | 30 | */ |
27 | 31 | public final class HelpThreadCreatedListener extends ListenerAdapter implements EventReceiver { |
| 32 | + private static final Logger logger = LoggerFactory.getLogger(HelpThreadCreatedListener.class); |
| 33 | + private static final ScheduledExecutorService SERVICE = Executors.newScheduledThreadPool(2); |
| 34 | + |
28 | 35 | private final HelpSystemHelper helper; |
29 | 36 | private final Cache<Long, Instant> threadIdToCreatedAtCache = Caffeine.newBuilder() |
30 | 37 | .maximumSize(1_000) |
@@ -69,7 +76,20 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) { |
69 | 76 | private void handleHelpThreadCreated(ThreadChannel threadChannel) { |
70 | 77 | helper.writeHelpThreadToDatabase(threadChannel.getOwnerIdLong(), threadChannel); |
71 | 78 |
|
72 | | - createMessages(threadChannel).queue(); |
| 79 | + Runnable createMessages = () -> { |
| 80 | + try { |
| 81 | + createMessages(threadChannel).queue(); |
| 82 | + } catch (Exception e) { |
| 83 | + logger.error( |
| 84 | + "Unknown error while creating messages after help-thread ({}) creation", |
| 85 | + threadChannel.getId(), e); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + // The creation is delayed, because otherwise it could be too fast and be executed |
| 90 | + // after Discord created the thread, but before Discord send OPs initial message. |
| 91 | + // Sending messages at that moment is not allowed. |
| 92 | + SERVICE.schedule(createMessages, 5, TimeUnit.SECONDS); |
73 | 93 | } |
74 | 94 |
|
75 | 95 | private RestAction<Message> createMessages(ThreadChannel threadChannel) { |
|
0 commit comments