From f2bcbd02ec688daec997dfaf4c2fe284e7f9e9ed Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 13 Dec 2022 09:49:40 +0100 Subject: [PATCH] Fixed issue with help thread creation actions being send too fast * delayed by 5 seconds --- .../help/HelpThreadCreatedListener.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCreatedListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCreatedListener.java index e018d80edc..e8ad4687ef 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCreatedListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCreatedListener.java @@ -9,6 +9,8 @@ import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.requests.RestAction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.EventReceiver; @@ -16,6 +18,8 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** @@ -25,6 +29,9 @@ * user. */ public final class HelpThreadCreatedListener extends ListenerAdapter implements EventReceiver { + private static final Logger logger = LoggerFactory.getLogger(HelpThreadCreatedListener.class); + private static final ScheduledExecutorService SERVICE = Executors.newScheduledThreadPool(2); + private final HelpSystemHelper helper; private final Cache threadIdToCreatedAtCache = Caffeine.newBuilder() .maximumSize(1_000) @@ -69,7 +76,20 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) { private void handleHelpThreadCreated(ThreadChannel threadChannel) { helper.writeHelpThreadToDatabase(threadChannel.getOwnerIdLong(), threadChannel); - createMessages(threadChannel).queue(); + Runnable createMessages = () -> { + try { + createMessages(threadChannel).queue(); + } catch (Exception e) { + logger.error( + "Unknown error while creating messages after help-thread ({}) creation", + threadChannel.getId(), e); + } + }; + + // The creation is delayed, because otherwise it could be too fast and be executed + // after Discord created the thread, but before Discord send OPs initial message. + // Sending messages at that moment is not allowed. + SERVICE.schedule(createMessages, 5, TimeUnit.SECONDS); } private RestAction createMessages(ThreadChannel threadChannel) {