/*
 * Skybot, a multipurpose discord bot
 *      Copyright (C) 2017 - 2019  Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ml.duncte123.skybot.commands.uncategorized;

import ml.duncte123.skybot.objects.command.Command;
import ml.duncte123.skybot.objects.command.CommandContext;
import javax.annotation.Nonnull;

import java.text.DecimalFormat;
import java.time.temporal.ChronoUnit;

import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg;

public class PingCommand extends Command {
    @Override
    public void executeCommand(@Nonnull CommandContext ctx) {

        final long start = System.nanoTime();

        sendMsg(ctx.getEvent(), "PONG!", (it) -> {

            final String restPing = new DecimalFormat("#.##").format((System.nanoTime() - start) * 1E-6);
            final long messagePing = ctx.getMessage().getCreationTime().until(it.getCreationTime(), ChronoUnit.MILLIS);

            it.editMessageFormat("PONG!\n" +
                    "Rest ping: %sms\n" +
                    "Message ping: %sms\n" +
                    "Websocket ping: %sms\n" +
                    "Average shard ping: %sms",
                restPing,
                messagePing,
                ctx.getJDA().getPing(),
                ctx.getShardManager().getAveragePing()
            ).queue();
        });
    }

    @Override
    public String getName() {
        return "ping";
    }

    @Override
    public String help() {
        return "Shows the delay from the bot to the discord servers.";
    }
}

@@ -959,16 +959,17 @@
 ontext;%0A
+%0A
 import j
@@ -996,41 +996,8 @@
 ull;
-%0A%0Aimport java.text.DecimalFormat;
 %0Aimp
@@ -1035,76 +1035,52 @@
 it;%0A
-%0A
 import 
-static me.duncte123.botcommons.messaging.MessageUtils.sendMs
+java.util.concurrent.atomic.AtomicLon
 g;%0A%0A
@@ -1197,16 +1197,89 @@
 t ctx) %7B
+%0A        if (!ctx.getChannel().canTalk()) %7B%0A            return;%0A        %7D
 %0A%0A      
@@ -1282,25 +1282,31 @@
       final 
-l
+AtomicL
 ong start = 
@@ -1309,69 +1309,223 @@
 t = 
-System.nanoTime();%0A%0A        sendMsg(ctx.getEvent(), %22PONG!%22, 
+new AtomicLong();%0A%0A        ctx.getChannel().sendMessage(%22Pong!!%22)%0A            .setCheck(() -%3E %7B%0A                start.set(System.currentTimeMillis());%0A%0A                return true;%0A            %7D)%0A            .queue(
 (it)
@@ -1530,17 +1530,20 @@
 t) -%3E %7B%0A
-%0A
+    
         
@@ -1552,22 +1552,22 @@
   final 
-String
+double
  restPin
@@ -1574,78 +1574,54 @@
 g = 
-new DecimalFormat(%22#.##%22).format((System.nanoTime() - start) * 1E-6);%0A
+System.currentTimeMillis() - start.get();%0A    
     
@@ -1749,16 +1749,20 @@
         
+    
 it.editM
@@ -1806,16 +1806,20 @@
         
+    
 %22Rest pi
@@ -1824,32 +1824,36 @@
 ping: %25sms%5Cn%22 +%0A
+    
                 
@@ -1901,16 +1901,20 @@
         
+    
 %22Websock
@@ -1924,32 +1924,36 @@
 ping: %25sms%5Cn%22 +%0A
+    
                 
@@ -2000,16 +2000,20 @@
         
+    
 restPing
@@ -2010,24 +2010,28 @@
   restPing,%0A
+    
             
@@ -2059,24 +2059,28 @@
             
+    
 ctx.getJDA()
@@ -2091,16 +2091,20 @@
 Ping(),%0A
+    
         
@@ -2162,16 +2162,20 @@
         
+    
 ).queue(
@@ -2173,24 +2173,28 @@
  ).queue();%0A
+    
         %7D);%0A

Make REST ping more presice