package us.cyrien.minecordbot.commands.discordCommand;

import com.jagrosh.jdautilities.commandclient.CommandEvent;
import net.dv8tion.jda.core.EmbedBuilder;
import us.cyrien.mcutils.logger.Logger;
import us.cyrien.minecordbot.Bot;
import us.cyrien.minecordbot.Minecordbot;
import us.cyrien.minecordbot.commands.MCBCommand;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class EvalCmd extends MCBCommand {

    public EvalCmd(Minecordbot minecordbot) {
        super(minecordbot);
        this.name = "eval";
        this.arguments = "<script...>";
        this.help = "js eval function";
        this.aliases = new String[]{"ev"};
        this.ownerCommand = true;
        this.category = Bot.OWNER;
    }

    @Override
    public void doCommand(CommandEvent event) {
        String arg = event.getArgs();
        arg = arg.replaceAll("```js", "");
        arg = arg.replaceAll("```", "");
        if (event.getArgs().isEmpty()) {
            event.replyInDM(event.getClient().getError() + " please provide something to evaluate");
            return;
        }

        ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("Nashorn");
        scriptEngine.put("mcb", mcb);
        scriptEngine.put("event", event);
        scriptEngine.put("jda", event.getJDA());
        scriptEngine.put("guild", event.getGuild());
        scriptEngine.put("cfgMngr", mcb.getMcbConfigsManager());
        scriptEngine.put("channel", event.getTextChannel());
        EmbedBuilder eb = new EmbedBuilder();
        eb.setColor(event.getGuild().getMemberById(event.getJDA().getSelfUser().getId()).getColor());
        try {
            eb.addField("Result:", "" + scriptEngine.eval(arg.trim()), false);
            event.reply(eb.build());
        } catch (Exception e1) {
            eb.addField("Error:" + e1.getClass().getSimpleName(), e1.getCause().toString(), false);
            Logger.err("Eval command execution Error:" + e1.getClass().getSimpleName() + e1.getCause().toString());
            event.reply(eb.build());
        }
    }
}


@@ -113,90 +113,8 @@
 nt;%0A
-import net.dv8tion.jda.core.EmbedBuilder;%0Aimport us.cyrien.mcutils.logger.Logger;%0A
 impo
@@ -649,16 +649,48 @@
 .OWNER;%0A
+        this.type = Type.EMBED;%0A
     %7D%0A%0A 
@@ -1455,21 +1455,22 @@
     
-Embed
+String
 Builder 
 eb =
@@ -1469,132 +1469,36 @@
 der 
-eb = new EmbedBuilder();%0A        eb.setColor(event.getGuild().getMemberById(event.getJDA().getSelfUser().getId()).getColor()
+builder = new StringBuilder(
 );%0A 
@@ -1526,36 +1526,46 @@
     
-eb.addFiel
+builder.appen
 d(%22
+**
 Result
-:%22, %22%22 + 
+**: %22).append(
 scri
@@ -1589,23 +1589,16 @@
 .trim())
-, false
 );%0A     
@@ -1604,38 +1604,57 @@
         
-event.reply(eb.build()
+respond(builder.toString(), event).queue(
 );%0A     
@@ -1697,31 +1697,88 @@
     
-eb.addFiel
+builder = new StringBuilder();%0A            builder.appen
 d(%22
+**
 Error
-:%22 + 
+**: %22).append(
 e1.g
@@ -1806,194 +1806,155 @@
 me()
-, e1.getCause().toString(), false);%0A            Logger.err(%22Eval command execution Error:%22 + e1.getClass().getSimpleName() + e1.getCause().toString());%0A            event.reply(eb.build()
+).append(%22%5Cn%22);%0A            builder.append(%22**Cause**: %5Cn%22).append(e1.getCause().toString());%0A            respond(builder.toString(), event).queue(
 );%0A 

Embed nashorn responses