From dc5a62540b679b85d32e266d9056ba9af6d52377 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 30 Jul 2023 21:29:35 +0700 Subject: [PATCH] backup thingy --- .../chayapak/chomens_bot/Configuration.java | 7 +++++ .../chipmunk/chayapak/chomens_bot/Main.java | 30 +++++++++++++++++++ src/main/resources/default-config.yml | 9 ++++++ 3 files changed, 46 insertions(+) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java index 333b0ef2..04b7d88c 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java @@ -15,6 +15,8 @@ public class Configuration { public InternetCheck internetCheck = new InternetCheck(); + public Backup backup = new Backup(); + public String weatherApiKey; public Core core = new Core(); @@ -35,6 +37,11 @@ public class Configuration { public String address = "https://sus.red"; } + public static class Backup { + public boolean enabled = false; + public String address = "http://fard.sex/check"; + } + public static class Keys { public String normalKey; public String ownerKey; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java index 706be9a0..df4d213a 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java @@ -12,6 +12,7 @@ import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; import java.io.*; +import java.net.InetAddress; import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; @@ -38,6 +39,8 @@ public class Main { private static Configuration config; + private static boolean alreadyStarted = false; + public static void main(String[] args) throws IOException { final Path configPath = Path.of("config.yml"); @@ -79,6 +82,33 @@ public class Main { } }, 0, 1, TimeUnit.MINUTES); + if (!config.backup.enabled) { + initializeBots(); + return; + } + + executor.scheduleAtFixedRate(() -> { + boolean reachable; + + try { + reachable = InetAddress.getByName(config.backup.address).isReachable(5000); + } catch (IOException e) { + reachable = false; + } + + if (!reachable) { + LoggerUtilities.info("Main instance is down! Starting backup instance"); + + initializeBots(); + } else if (alreadyStarted) { + System.exit(1); + } + }, 0, 1, TimeUnit.MINUTES); + } + + public static void initializeBots() { + alreadyStarted = true; + Configuration.BotOption[] botsOptions = config.bots; // idk if these should be here lol, but it is just the discord stuff diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index d53d2d69..d34cde24 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -13,6 +13,15 @@ internetCheck: enabled: true address: 'https://sus.red' +# how backup works is that it checks for the address every 1 minute, +# if the address is reachable it will not start the bot +# if the address is not reachable then it will start the bot +# +# if the bot has already been started and address is back up it will stop the bot (using System.exit(1)) +backup: + enabled: false + address: '192.168.1.169' + discord: enabled: false prefix: 'default!'