Newer
Older
CrashLogBot / src / main / java / net / romvoid / crashbot / commands / StatusCommand.java
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);
        }
//        if () {
//            embed.addField("", "Jabber connect - OK", false);
//            embed.setColor(Color.green);
//        } else
//        {
//            embed.addField("", "Jabber connect - 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;
    }
}