package ml.duncte123.skybot.commands.essentials;

import ml.duncte123.skybot.objects.command.Command;
import ml.duncte123.skybot.utils.AirUtils;
import ml.duncte123.skybot.utils.Settings;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.Arrays;
import java.util.List;

public class EvalCommand extends Command {

    private ScriptEngine engine;
    private List<String> packageImports;

    /**
     * This initialises the engine
     */
    public EvalCommand() {
        engine = new ScriptEngineManager().getEngineByName("groovy");
        packageImports =  Arrays.asList("java.io",
                "java.lang",
                "java.util",
                "net.dv8tion.jda.core",
                "net.dv8tion.jda.core.entities",
                "net.dv8tion.jda.core.entities.impl",
                "net.dv8tion.jda.core.managers",
                "net.dv8tion.jda.core.managers.impl",
                "net.dv8tion.jda.core.utils",
                "ml.duncte123.skybot.utils");
    }

    /**
     * This is the executeCommand of the command, the thing you want the command to to needs to be in here
     * @param args The command agruments
     * @param event a instance of {@link net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent GuildMessageReceivedEvent}
     */
    @Override
    public void executeCommand(String[] args, GuildMessageReceivedEvent event) {

        if(!event.getAuthor().getId().equals(Settings.ownerId)) {
            sendError(event.getMessage());
            return;
        }

        try {

            Bindings bindings = engine.createBindings();

            bindings.put("commands", AirUtils.commandSetup.getCommands());

            bindings.put("message", event.getMessage());
            bindings.put("channel", event.getChannel());
            bindings.put("guild", event.getGuild());
            bindings.put("member", event.getMember());
            bindings.put("jda", event.getJDA());
            bindings.put("event", event);

            bindings.put("args", args);


            engine.eval(
                    "public void sendMsg(String msg) {" +
                            "channel.sendMessage(msg).queue();" +
                            "}", bindings);

            StringBuilder importStringBuilder = new StringBuilder();
            for (final String s : packageImports) {
                importStringBuilder.append("import ").append(s).append(".*;");
            }

            Object out_ = null;

            Thread thread = new Thread(() -> {
                Object out = null;
                try {
                    out = engine.eval(importStringBuilder.toString() +
                                    event.getMessage().getRawContent().substring(event.getMessage().getRawContent().split(" ")[0].length()).replaceAll("getToken", "getSelfUser")
                            , bindings);

                } catch (Exception e) {
                    e.printStackTrace();
                    sendMsg(event, e.getMessage());
                }
                if (out != null && !String.valueOf(out).isEmpty() ) {
                    sendMsg(event, out.toString());
                }
            });
            thread.run();
            // Object out = engine.eval(importStringBuilder.toString() +
            //       event.getMessage().getRawContent().substring(event.getMessage().getRawContent().split(" ")[0].length()).replaceAll("getToken", "getSelfUser")
            //, bindings);
            if (out_ != null || String.valueOf(out_).isEmpty() ) {
                sendMsg(event, out_.toString());
            }
        }
        catch (ScriptException e) {
            event.getChannel().sendMessage("Error: " + e.getMessage()).queue();
        }
        catch (Exception e1) {
            event.getChannel().sendMessage("Error: " + e1.getMessage()).queue();
            e1.printStackTrace();
        }
        sendSuccess(event.getMessage());
    }

    /**
     * The usage instructions of the command
     * @return a String
     */
    @Override
    public String help() {
        return "A simple eval command";
    }

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

@@ -2624,41 +2624,8 @@
  %7D%0A%0A
-            Object out_ = null;%0A%0A
     
@@ -3352,401 +3352,8 @@
 ();%0A
-            // Object out = engine.eval(importStringBuilder.toString() +%0A            //       event.getMessage().getRawContent().substring(event.getMessage().getRawContent().split(%22 %22)%5B0%5D.length()).replaceAll(%22getToken%22, %22getSelfUser%22)%0A            //, bindings);%0A            if (out_ != null %7C%7C String.valueOf(out_).isEmpty() ) %7B%0A                sendMsg(event, out_.toString());%0A            %7D%0A
     

Remove junk