From 2a49844a95593ac98e919c18651320e62f276fa7 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 6 Apr 2025 17:28:47 +0400 Subject: feat: implementing lua coding --- bot/src/commands/lua.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 bot/src/commands/lua.cpp (limited to 'bot/src/commands/lua.cpp') diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp new file mode 100644 index 0000000..cd7bc84 --- /dev/null +++ b/bot/src/commands/lua.cpp @@ -0,0 +1,68 @@ +#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 {}; + } +} \ No newline at end of file -- cgit v1.2.3