#include "commands/lua.hpp" #include #include #include "bundle.hpp" #include "commands/request.hpp" #include "commands/response.hpp" #include "schemas/user.hpp" namespace bot::command::lua { LuaCommand::LuaCommand(std::shared_ptr luaState, const std::string &script) { this->luaState = luaState; sol::table data = luaState->script(script); this->name = data["name"]; this->delay = data["delay_sec"]; sol::table subcommands = data["subcommands"]; for (auto &k : subcommands) { sol::object value = k.second; if (value.is()) { this->subcommands.push_back(value.as()); } } std::string rights_text = data["minimal_rights"]; if (rights_text == "suspended") { this->level = schemas::PermissionLevel::SUSPENDED; } else if (rights_text == "user") { this->level = schemas::PermissionLevel::USER; } else if (rights_text == "vip") { this->level = schemas::PermissionLevel::VIP; } else if (rights_text == "moderator") { this->level = schemas::PermissionLevel::MODERATOR; } else if (rights_text == "broadcaster") { this->level = schemas::PermissionLevel::BROADCASTER; } else { this->level = schemas::PermissionLevel::USER; } this->handle = data["run"]; } Response LuaCommand::run(const InstanceBundle &bundle, const Request &request) const { sol::object response = this->handle(request.as_lua_table(this->luaState)); if (response.is()) { return {response.as()}; } else if (response.is()) { sol::table tbl = response.as(); std::vector items; for (auto &kv : tbl) { sol::object value = kv.second; if (value.is()) { items.push_back(value.as()); } } return items; } return {}; } }