From 96486bb3a106084704f9751e59ce3361812ca2e9 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 02:25:26 +0100 Subject: [PATCH] fix shutdown db saving --- .../java/cc/fascinated/bat/BatApplication.java | 15 +++++++-------- .../cc/fascinated/bat/event/EventListener.java | 4 ++-- .../logging/listeners/ChannelListener.java | 2 +- .../cc/fascinated/bat/service/GuildService.java | 2 +- .../cc/fascinated/bat/service/UserService.java | 4 ++-- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/BatApplication.java b/src/main/java/cc/fascinated/bat/BatApplication.java index 9d348d2..fb76d9f 100644 --- a/src/main/java/cc/fascinated/bat/BatApplication.java +++ b/src/main/java/cc/fascinated/bat/BatApplication.java @@ -39,16 +39,15 @@ public class BatApplication { } log.info("Found configuration at '{}'", config.getAbsolutePath()); // Log the found config + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + log.info("Shutting down..."); + for (EventListener listener : EventService.LISTENERS) { + listener.onShutdown(); + } + })); + // Start the app SpringApplication.run(BatApplication.class, args); log.info("APP IS RUNNING IN %s MODE!!!!!!!!!".formatted(Config.isProduction() ? "PRODUCTION" : "DEVELOPMENT")); } - - @PreDestroy - public void onShutdown() { - log.info("Shutting down..."); - for (EventListener listener : EventService.LISTENERS) { - listener.onSpringShutdown(); - } - } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index 8ab8d5b..567306d 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -243,8 +243,8 @@ public interface EventListener { } /** - * Called when Spring is shutting down + * Called when Bat is shutting down */ - default void onSpringShutdown() { + default void onShutdown() { } } diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java index ddfc40b..245a74e 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java @@ -80,7 +80,7 @@ public class ChannelListener implements EventListener { if (!joined) { lastVoiceChannel.remove(user); } - log.info("User \"{}\" {} voice channel \"{}\" in guild \"{}\"", user.getId(), joined ? "joined" : "left", voiceChannel.getName(), guild.getName()); + log.info("User \"{}\" {} voice channel \"{}\" in guild \"{}\"", user.getName(), joined ? "joined" : "left", voiceChannel.getName(), guild.getName()); String description = new EmbedDescriptionBuilder("User %s Voice Channel".formatted(joined ? "Joined" : "Left")) .appendLine("User: %s".formatted(user.getDiscordUser().getAsMention()), true) .appendLine("Channel: %s".formatted(voiceChannel.getAsMention()), true) diff --git a/src/main/java/cc/fascinated/bat/service/GuildService.java b/src/main/java/cc/fascinated/bat/service/GuildService.java index cacdab8..2fee5e3 100644 --- a/src/main/java/cc/fascinated/bat/service/GuildService.java +++ b/src/main/java/cc/fascinated/bat/service/GuildService.java @@ -114,7 +114,7 @@ public class GuildService extends ListenerAdapter implements EventListener { } @Override - public void onSpringShutdown() { + public void onShutdown() { log.info("Saving all guilds before shutdown..."); for (BatGuild guild : guilds.values()) { guild.save(); diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index 9f21057..a0e3597 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -83,7 +83,7 @@ public class UserService implements EventListener { } @Override - public void onSpringShutdown() { + public void onShutdown() { log.info("Saving all users before shutdown..."); for (BatUser user : users.values()) { user.save(); @@ -93,7 +93,7 @@ public class UserService implements EventListener { @Override public void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) { - user.setGlobalName(event.getUser().getName()); // Ensure the user's name is up-to-date + user.setGlobalName(event.getUser().getGlobalName()); // Ensure the user's name is up-to-date } @Override