diff --git a/src/main/java/net/romvoid/crashbot/Bot.java b/src/main/java/net/romvoid/crashbot/Bot.java index 92ddf8e..00d2158 100644 --- a/src/main/java/net/romvoid/crashbot/Bot.java +++ b/src/main/java/net/romvoid/crashbot/Bot.java @@ -32,6 +32,7 @@ import javax.security.auth.login.LoginException; +import net.romvoid.crashbot.commands.StatusCommand; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -96,7 +97,7 @@ client.setEmojis("\uD83D\uDE03", "\uD83D\uDE2E", "\uD83D\uDE26"); client.setPrefix(prefix); - client.addCommands(new InviteCommand(), new GuildlistCommand(waiter), new GitCommand()); + client.addCommands(new InviteCommand(), new GuildlistCommand(waiter), new GitCommand(), new StatusCommand()); initJDA(); diff --git a/src/main/java/net/romvoid/crashbot/commands/StatusCommand.java b/src/main/java/net/romvoid/crashbot/commands/StatusCommand.java new file mode 100644 index 0000000..0259f2e --- /dev/null +++ b/src/main/java/net/romvoid/crashbot/commands/StatusCommand.java @@ -0,0 +1,73 @@ +package net.romvoid.crashbot.commands; + +import java.awt.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.TimeUnit; + +import com.jagrosh.jdautilities.command.Command; +import com.jagrosh.jdautilities.command.CommandEvent; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageChannel; +import net.romvoid.crashbot.Bot; +import net.romvoid.crashbot.utilities.EmbedUtil; + +public class StatusCommand extends Command { + public StatusCommand() { + this.name = "status"; + this.help = "Returns the status of services MFND.ru"; + this.botPermissions = new Permission[]{Permission.MESSAGE_EMBED_LINKS}; + this.guildOnly = false; + } + @Override + protected void execute(CommandEvent event) { + String google = "https://www.google.com/"; + String mfnd = "https://www.mfnd.ru/"; + Bot.LOG.info(event.getAuthor().getName() + " invoked command " + this.name); + + MessageChannel channel = event.getChannel(); + EmbedBuilder embed = EmbedUtil.embed("Status MFND.ru services", ""); + if (checkInternetConnection(google)) { + embed.addField("", "Route to the server - OK", false); + embed.setColor(Color.green); + } else + { + embed.addField("", "Route to the server - FAIL", false); + embed.setColor(Color.red); + } + if (checkInternetConnection(mfnd)) { + embed.addField("", "Site activity - OK", false); + embed.setColor(Color.green); + } else + { + embed.addField("", "Site activity - FAIL", false); + embed.setColor(Color.red); + } + Message msg = EmbedUtil.message(embed); + EmbedUtil.sendAndDeleteOnGuilds(channel, msg, -1, TimeUnit.MINUTES); + } + + private static boolean checkInternetConnection(String site) { + Boolean result = false; + HttpURLConnection con = null; + try { + con = (HttpURLConnection) new URL(site).openConnection(); + con.setRequestMethod("HEAD"); + result = (con.getResponseCode() == HttpURLConnection.HTTP_OK); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (con != null) { + try { + con.disconnect(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return result; + } +}