diff --git a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java index 863f8d2..810d376 100644 --- a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java @@ -22,7 +22,7 @@ public class MoviesQuoteBot { public static final String DESCRIPTION = "I may know quotes from movies."; public static final String VERSION = "0.3.8-SNAPSHOT"; public static final String KILL_SWITCH_FILE = "system_locked"; - public static final String MAINTENANCE_MODE_FILE = "maintenance_mode_engaged"; + public static final String MAINTENANCE_MODE_FILE = "maintenance_mode_locked"; private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName()); private static final Options options = new Options(); private static boolean isInMaintenanceMode = false; @@ -52,40 +52,44 @@ public class MoviesQuoteBot { DiscordManager.logout(); })); - DiscordManager.onDisconnect(); - if (new File(MAINTENANCE_MODE_FILE).exists()) { logger.warn("Starting in maintenance mode."); switchToMaintenanceMode(); } + + DiscordManager.onDisconnect(); } public static void switchToMaintenanceMode() { - try { - if (new File(MAINTENANCE_MODE_FILE).createNewFile()) - logger.debug("Created file '{}'.", MAINTENANCE_MODE_FILE); - } catch (IOException e) { - logger.error("Couldn't create file '{}'.", MAINTENANCE_MODE_FILE); + if (!isInMaintenanceMode) { + try { + if (new File(MAINTENANCE_MODE_FILE).createNewFile()) + logger.debug("Created file '{}'.", MAINTENANCE_MODE_FILE); + } catch (IOException e) { + logger.error("Couldn't create file '{}'.", MAINTENANCE_MODE_FILE); + } + isInMaintenanceMode = true; + DiscordManager.setOnMaintenanceMode(); + OpenSubtitles.logout(); + logger.info("Maintenance mode engaged."); } - DiscordManager.setOnMaintenanceMode(); - OpenSubtitles.logout(); - logger.info("Maintenance mode engaged."); } public static void switchToNormalOperations() { - try { - Files.delete(Path.of(MAINTENANCE_MODE_FILE)); - } catch (IOException e) { - logger.error("Could not delete file '{}'.", MAINTENANCE_MODE_FILE, e); + if (isInMaintenanceMode) { + try { + Files.delete(Path.of(MAINTENANCE_MODE_FILE)); + } catch (IOException e) { + logger.error("Could not delete file '{}'.", MAINTENANCE_MODE_FILE, e); + } + isInMaintenanceMode = false; + DiscordManager.setOnline(); + OpenSubtitles.login(); + logger.info("Maintenance mode disengaged."); } - isInMaintenanceMode = false; - DiscordManager.setOnline(); - OpenSubtitles.login(); - logger.info("Maintenance mode disengaged."); } public static boolean isInMaintenanceMode() { - if (!Files.exists(Path.of(MAINTENANCE_MODE_FILE)) && isInMaintenanceMode) switchToNormalOperations(); return isInMaintenanceMode; } @@ -93,5 +97,6 @@ public class MoviesQuoteBot { CommandsClient.checkVersions(); CommandsClient.setMaintenanceMode(true); CommandsClient.setMaintenanceMode(false); + System.exit(0); } } diff --git a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java index 4073284..e686af8 100644 --- a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java +++ b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java @@ -47,9 +47,9 @@ public final class DiscordManager { private static void registerDiscordCommands() { for (Command command : commands) discordClient.getEventDispatcher().on(MessageCreateEvent.class) - .filter(event -> !isInMaintenanceMode() && event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false)) + .filter(event -> event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false)) .filter(event -> event.getMessage().getContent().split(" ")[0].equals(command.getName())) - .flatMap(event -> command.execute(event).then()) + .flatMap(event -> !isInMaintenanceMode() ? command.execute(event).then() : Mono.just("").then()) .subscribe(); } diff --git a/src/main/java/xyz/vallat/louis/sockets/CommandsClient.java b/src/main/java/xyz/vallat/louis/sockets/CommandsClient.java index 7909d5b..584fef5 100644 --- a/src/main/java/xyz/vallat/louis/sockets/CommandsClient.java +++ b/src/main/java/xyz/vallat/louis/sockets/CommandsClient.java @@ -20,6 +20,8 @@ public class CommandsClient { private static final String SOCKET_HOST = System.getenv(EnvironmentVariables.SOCKET_HOST.getValue()) == null ? "localhost" : System.getenv(EnvironmentVariables.SOCKET_HOST.getValue()); + private CommandsClient() {} + private static String sendMessage(String message) { logger.debug("Sending message '{}'.", message); try (Socket clientSocket = new Socket(SOCKET_HOST, PORT)) { diff --git a/src/main/java/xyz/vallat/louis/sockets/CommandsServer.java b/src/main/java/xyz/vallat/louis/sockets/CommandsServer.java index a4d2bdc..a90c21f 100644 --- a/src/main/java/xyz/vallat/louis/sockets/CommandsServer.java +++ b/src/main/java/xyz/vallat/louis/sockets/CommandsServer.java @@ -57,7 +57,7 @@ public class CommandsServer { logger.error("Couldn't even create kill switch file... Exiting anyway.", e); } System.exit(ExitCodes.URGENT_EXIT_REQUIRED.getValue()); - }); + }).start(); } private static boolean computeMessage(PrintWriter out, String message) { diff --git a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java index 3551ade..f870323 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java +++ b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java @@ -50,7 +50,7 @@ public final class OpenSubtitles { public static void logout() { try { - client.logout(); + if (client.isLoggedIn()) client.logout(); } catch (XmlRpcException e) { logger.error("Could not log out. Skipping."); }