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 ac478e9..aaaf854 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java @@ -13,6 +13,8 @@ public class Configuration { public Keys keys = new Keys(); + public InternetCheck internetCheck = new InternetCheck(); + public String weatherApiKey; public Core core = new Core(); @@ -28,6 +30,11 @@ public class Configuration { public BotOption[] bots = new BotOption[]{}; + public static class InternetCheck { + public boolean enabled = true; + public String address = "https://sus.red"; + } + public static class ConsolePrefixes { public String normalCommandsPrefix; public String consoleServerPrefix; 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 939117a..4b18767 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java @@ -10,11 +10,15 @@ import org.yaml.snakeyaml.constructor.Constructor; import javax.security.auth.login.LoginException; import java.io.*; +import java.net.URL; +import java.net.URLConnection; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class Main { public static final List bots = new ArrayList<>(); @@ -22,11 +26,12 @@ public class Main { public static final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); public static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()); + private static Configuration config; + public static void main(String[] args) throws IOException { final File file = new File("config.yml"); final Constructor constructor = new Constructor(Configuration.class, new LoaderOptions()); final Yaml yaml = new Yaml(constructor); - Configuration _config; if (!file.exists()) { // creates config file from default-config.yml @@ -53,9 +58,15 @@ public class Main { InputStream opt = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); - _config = yaml.load(reader); + config = yaml.load(reader); - final Configuration config = _config; + executor.scheduleAtFixedRate(() -> { + try { + checkInternet(); + } catch (IOException e) { + e.printStackTrace(); + } + }, 0, 1, TimeUnit.MINUTES); Configuration.BotOption[] botsOptions = config.bots; @@ -84,4 +95,26 @@ public class Main { // fard new ConsolePlugin(bots, config, jda); } + + private static void checkInternet () throws IOException { + if (!config.internetCheck.enabled) return; + + boolean reachable = false; + + try { + final URL url = new URL(config.internetCheck.address); + + final URLConnection connection = url.openConnection(); + + connection.connect(); + + reachable = true; + } catch (UnknownHostException ignored) {} + + if (!reachable) { + System.err.println("No internet, exiting"); + + System.exit(1); + } + } } \ No newline at end of file diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index 660c418..6f0d379 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -11,6 +11,10 @@ consolePrefixes: normalCommandsPrefix: '.' consoleServerPrefix: '/' +internetCheck: + enabled: true + url: 'https://sus.red' + discord: enabled: false prefix: 'default!'