From 04d93dd7a50602d94d05b7fff158ae26d3ebb377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 9 Mar 2025 11:30:59 +0100 Subject: [PATCH] fix autocloseable executors --- src/main/java/wtf/beatrice/hidekobot/HidekoBot.java | 6 ++++-- .../wtf/beatrice/hidekobot/commands/slash/DieCommand.java | 5 ++++- src/main/java/wtf/beatrice/hidekobot/util/Logger.java | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 41aa572..ee150e6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -187,8 +187,10 @@ public class HidekoBot // update slash commands (delayed) final boolean finalForceUpdateCommands = forceUpdateCommands; - Executors.newSingleThreadScheduledExecutor().schedule(() -> // todo: try-with-resources - CommandUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS); + try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor()) { + executor.schedule(() -> CommandUtil.updateSlashCommands(finalForceUpdateCommands), + 1, TimeUnit.SECONDS); + } // set the bot's status jda.getPresence().setStatus(OnlineStatus.ONLINE); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java index e6a4895..6bc9fdd 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java @@ -10,6 +10,7 @@ import wtf.beatrice.hidekobot.HidekoBot; import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class DieCommand extends SlashCommandImpl @@ -28,7 +29,9 @@ public class DieCommand extends SlashCommandImpl event.reply("Sorry, only the bot owner can run this command!").setEphemeral(true).queue(); } else { event.reply("Going to sleep! Cya ✨").queue(); - Executors.newSingleThreadScheduledExecutor().schedule(HidekoBot::shutdown, 3, TimeUnit.SECONDS); + try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor()) { + executor.schedule(HidekoBot::shutdown, 3, TimeUnit.SECONDS); + } } } } diff --git a/src/main/java/wtf/beatrice/hidekobot/util/Logger.java b/src/main/java/wtf/beatrice/hidekobot/util/Logger.java index 8709bcc..eeecd46 100644 --- a/src/main/java/wtf/beatrice/hidekobot/util/Logger.java +++ b/src/main/java/wtf/beatrice/hidekobot/util/Logger.java @@ -3,6 +3,7 @@ package wtf.beatrice.hidekobot.util; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @Deprecated(since = "0.5.16", forRemoval = true) @@ -49,7 +50,9 @@ public class Logger { // create a new scheduled executor with an anonymous runnable... //... after waiting seconds. - Executors.newSingleThreadScheduledExecutor().schedule(() -> log(message), delay, TimeUnit.SECONDS); + try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor()) { + executor.schedule(() -> log(message), delay, TimeUnit.SECONDS); + } }