Newer
Older
CrashLogBot / src / main / java / net / romvoid / crashbot / commands / StatusCommand.java
@yavarenkov yavarenkov on 26 Feb 2022 4 KB add minecraft server ping
package net.romvoid.crashbot.commands;

import java.awt.*;
import java.io.IOException;
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 br.com.azalim.mcserverping.MCPing;
import br.com.azalim.mcserverping.MCPingOptions;
import br.com.azalim.mcserverping.MCPingResponse;
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", "Google traceroute", false);
            embed.setColor(Color.green);
        } else
        {
            embed.addField("Route to the server - FAIL", "Google traceroute", false);
            embed.setColor(Color.red);
        }
        if (checkInternetConnection(mfnd)) {
            embed.addField("Site activity - OK", "Ping www.mfnd.ru", false);
            embed.setColor(Color.green);
        } else
        {
            embed.addField("Site activity - FAIL", "Ping www.mfnd.ru", false);
            embed.setColor(Color.red);
        }
        MCPingOptions iisoptions = MCPingOptions.builder()
                .hostname("www.mfnd.ru")
                .port(22630)
                .build();
        MCPingResponse iisreply;
        try {
            iisreply = MCPing.getPing(iisoptions);
        } catch (IOException ex) {
            embed.addField("", "IIS server - FAIL", false);
            embed.setColor(Color.red);
            return;
        }
        MCPingResponse.Players iisplayers = iisreply.getPlayers();
        embed.addField("IIS server - OK", "Online count: "+ iisplayers.getOnline() + "/" + iisplayers.getMax(), false);
        embed.setColor(Color.green);
        MCPingOptions impactoptions = MCPingOptions.builder()
                .hostname("www.mfnd.ru")
                .port(22631)
                .build();
        MCPingResponse impactreply;
        try {
            impactreply = MCPing.getPing(impactoptions);
        } catch (IOException ex) {
            embed.addField("", "IMPACT server - FAIL", false);
            embed.setColor(Color.red);
            return;
        }
        MCPingResponse.Players impactplayers = impactreply.getPlayers();
        embed.addField("IMPACT server - OK", "Online count: "+ impactplayers.getOnline() + "/" + impactplayers.getMax(), false);
        embed.setColor(Color.green);
//        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;
    }
}