Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
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;

import javax.annotation.Nonnull;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -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<Long, Instant> threadIdToCreatedAtCache = Caffeine.newBuilder()
.maximumSize(1_000)
Expand Down Expand Up @@ -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<Message> createMessages(ThreadChannel threadChannel) {
Expand Down