Skip to content
Merged
Show file tree
Hide file tree
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 @@ -4,7 +4,7 @@
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -56,11 +56,10 @@ public static void main(final String[] args) {
return;
}

try {
runBot(config);
} catch (Exception t) {
logger.error("Unknown error", t);
}
Thread.setDefaultUncaughtExceptionHandler(Application::onUncaughtException);
Runtime.getRuntime().addShutdownHook(new Thread(Application::onShutdown));

runBot(config);
}

/**
Expand All @@ -86,8 +85,6 @@ public static void runBot(Config config) {
jda.addEventListener(new BotCore(jda, database, config));
jda.awaitReady();
logger.info("Bot is ready");

Runtime.getRuntime().addShutdownHook(new Thread(Application::onShutdown));
} catch (LoginException e) {
logger.error("Failed to login", e);
} catch (InterruptedException e) {
Expand All @@ -109,4 +106,9 @@ private static void onShutdown() {
logger.info("Bot has been stopped");
}

private static void onUncaughtException(@NotNull Thread failingThread,
@NotNull Throwable failure) {
logger.error("Unknown error in thread {}.", failingThread.getName(), failure);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,16 @@ public ComponentIdStore(@NotNull Database database, long evictEveryInitialDelay,
.maximumSize(CACHE_SIZE)
.expireAfterAccess(EVICT_CACHE_OLDER_THAN, TimeUnit.of(EVICT_CACHE_OLDER_THAN_UNIT))
.build();
evictionTask = evictionService.scheduleWithFixedDelay(this::evictDatabase,
evictEveryInitialDelay, evictEveryDelay, TimeUnit.of(evictEveryUnit));

Runnable evictCommand = () -> {
try {
evictDatabase();
} catch (Exception e) {
logger.error("Unknown error while evicting the component ID store database.", e);
}
};
evictionTask = evictionService.scheduleWithFixedDelay(evictCommand, evictEveryInitialDelay,
evictEveryDelay, TimeUnit.of(evictEveryUnit));

logDebugSizeStatistics();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,18 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
message.delete().queue();

// Thread creation can sometimes take a bit longer than the actual message, so that
// "getThreadChannels()"
// would not pick it up, hence we execute the update with some slight delay.
UPDATE_SERVICE.schedule(() -> updateOverviewForGuild(event.getGuild()), 2,
TimeUnit.SECONDS);
// "getThreadChannels()" would not pick it up, hence we execute the update with some slight
// delay.
Runnable updateOverviewCommand = () -> {
try {
updateOverviewForGuild(event.getGuild());
} catch (Exception e) {
logger.error(
"Unknown error while attempting to update the help overview for guild {}.",
event.getGuild().getId(), e);
}
};
UPDATE_SERVICE.schedule(updateOverviewCommand, 2, TimeUnit.SECONDS);
}

private void updateOverviewForGuild(@NotNull Guild guild) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ public BotCore(@NotNull JDA jda, @NotNull Database database, @NotNull Config con
.forEach(routine -> {
Runnable command = () -> {
String routineName = routine.getClass().getSimpleName();
logger.debug("Running routine %s...".formatted(routineName));
routine.runRoutine(jda);
logger.debug("Finished routine %s.".formatted(routineName));
try {
logger.debug("Running routine %s...".formatted(routineName));
routine.runRoutine(jda);
logger.debug("Finished routine %s.".formatted(routineName));
} catch (Exception e) {
logger.error("Unknown error in routine {}.", routineName, e);
}
};

Routine.Schedule schedule = routine.createSchedule();
Expand Down