no internet check

This commit is contained in:
Chayapak 2023-07-06 19:17:51 +07:00
parent 5a5b9c18a8
commit fd3c7d62f7
3 changed files with 47 additions and 3 deletions

View file

@ -13,6 +13,8 @@ public class Configuration {
public Keys keys = new Keys(); public Keys keys = new Keys();
public InternetCheck internetCheck = new InternetCheck();
public String weatherApiKey; public String weatherApiKey;
public Core core = new Core(); public Core core = new Core();
@ -28,6 +30,11 @@ public class Configuration {
public BotOption[] bots = new BotOption[]{}; public BotOption[] bots = new BotOption[]{};
public static class InternetCheck {
public boolean enabled = true;
public String address = "https://sus.red";
}
public static class ConsolePrefixes { public static class ConsolePrefixes {
public String normalCommandsPrefix; public String normalCommandsPrefix;
public String consoleServerPrefix; public String consoleServerPrefix;

View file

@ -10,11 +10,15 @@ import org.yaml.snakeyaml.constructor.Constructor;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.io.*; import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main { public class Main {
public static final List<Bot> bots = new ArrayList<>(); public static final List<Bot> bots = new ArrayList<>();
@ -22,11 +26,12 @@ public class Main {
public static final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); public static final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(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 { public static void main(String[] args) throws IOException {
final File file = new File("config.yml"); final File file = new File("config.yml");
final Constructor constructor = new Constructor(Configuration.class, new LoaderOptions()); final Constructor constructor = new Constructor(Configuration.class, new LoaderOptions());
final Yaml yaml = new Yaml(constructor); final Yaml yaml = new Yaml(constructor);
Configuration _config;
if (!file.exists()) { if (!file.exists()) {
// creates config file from default-config.yml // creates config file from default-config.yml
@ -53,9 +58,15 @@ public class Main {
InputStream opt = new FileInputStream(file); InputStream opt = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); 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; Configuration.BotOption[] botsOptions = config.bots;
@ -84,4 +95,26 @@ public class Main {
// fard // fard
new ConsolePlugin(bots, config, jda); 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);
}
}
} }

View file

@ -11,6 +11,10 @@ consolePrefixes:
normalCommandsPrefix: '.' normalCommandsPrefix: '.'
consoleServerPrefix: '/' consoleServerPrefix: '/'
internetCheck:
enabled: true
url: 'https://sus.red'
discord: discord:
enabled: false enabled: false
prefix: 'default!' prefix: 'default!'