Skip to content

Commit e5460a1

Browse files
authored
Fixed issue with help thread creation actions being send too fast (#724)
* delayed by 5 seconds
1 parent ec8f08c commit e5460a1

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
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;
1214

1315
import org.togetherjava.tjbot.commands.EventReceiver;
1416

1517
import javax.annotation.Nonnull;
1618

1719
import java.time.Instant;
1820
import java.time.temporal.ChronoUnit;
21+
import java.util.concurrent.Executors;
22+
import java.util.concurrent.ScheduledExecutorService;
1923
import java.util.concurrent.TimeUnit;
2024

2125
/**
@@ -25,6 +29,9 @@
2529
* user.
2630
*/
2731
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+
2835
private final HelpSystemHelper helper;
2936
private final Cache<Long, Instant> threadIdToCreatedAtCache = Caffeine.newBuilder()
3037
.maximumSize(1_000)
@@ -69,7 +76,20 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) {
6976
private void handleHelpThreadCreated(ThreadChannel threadChannel) {
7077
helper.writeHelpThreadToDatabase(threadChannel.getOwnerIdLong(), threadChannel);
7178

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);
7393
}
7494

7595
private RestAction<Message> createMessages(ThreadChannel threadChannel) {

0 commit comments

Comments
 (0)