diff --git a/pom.xml b/pom.xml index 32c0e09..b503d68 100644 --- a/pom.xml +++ b/pom.xml @@ -90,6 +90,12 @@ snakeyaml 1.29 + + + org.luaj + luaj-jse + 3.0.1 + diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java new file mode 100644 index 0000000..7da273d --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java @@ -0,0 +1,57 @@ +package me.chayapak1.chomens_bot.commands; + +import me.chayapak1.chomens_bot.command.Command; +import me.chayapak1.chomens_bot.command.CommandContext; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.luaj.vm2.Globals; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.lib.jse.CoerceJavaToLua; +import org.luaj.vm2.lib.jse.JsePlatform; + +import java.util.ArrayList; +import java.util.List; + +public class ServerEvalCommand implements Command { + public String name() { return "servereval"; } + + public String description() { + return "Evaluate codes using LuaJ"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add("<{code}>"); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add(""); + + return aliases; + } + + public int trustLevel() { + return 2; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + try { + Globals globals = JsePlatform.standardGlobals(); + + globals.set("bot", CoerceJavaToLua.coerce(context.bot())); + + LuaValue chunk = globals.load(String.join(" ", args)); + + LuaValue output = chunk.call(); + + context.sendOutput(Component.text(output.toString()).color(NamedTextColor.GREEN)); + } catch (Exception e) { + return Component.text(e.toString()).color(NamedTextColor.RED); + } + + return Component.text("success"); + } +} diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java index dfae2f6..8bfab08 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java @@ -37,6 +37,7 @@ public class CommandHandlerPlugin { registerCommand(new UrbanCommand()); registerCommand(new ClearChatCommand()); registerCommand(new ListCommand()); + registerCommand(new ServerEvalCommand()); } public void registerCommand (Command command) {