From f2bf41bc614c7b070649a29b6fe9ca48cc1eafbb Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 9 Apr 2025 21:54:40 +0500 Subject: feat: bot_config() function for lua --- bot/src/commands/lua.cpp | 11 +++++++++++ bot/src/config.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ bot/src/config.hpp | 2 ++ 3 files changed, 57 insertions(+) (limited to 'bot') diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp index 6c20b15..7f70e68 100644 --- a/bot/src/commands/lua.cpp +++ b/bot/src/commands/lua.cpp @@ -54,6 +54,17 @@ namespace bot::command::lua { []() { return BOT_COMPILED_TIMESTAMP; }); state->set_function("bot_get_version", []() { return BOT_VERSION; }); + + state->set_function("bot_config", [state]() { + std::optional o_cfg = + bot::parse_configuration_from_file(".env"); + + if (!o_cfg.has_value()) { + return sol::make_object(*state, sol::nil); + } + + return sol::make_object(*state, o_cfg->as_lua_table(state)); + }); } void add_time_library(std::shared_ptr state) { diff --git a/bot/src/config.cpp b/bot/src/config.cpp index eef5142..010a0ec 100644 --- a/bot/src/config.cpp +++ b/bot/src/config.cpp @@ -9,6 +9,50 @@ #include "logger.hpp" namespace bot { + sol::table Configuration::as_lua_table( + std::shared_ptr luaState) const { + sol::table o = luaState->create_table(); + + // we parse only safe-to-leak parts of configuration + + // --- COMMAND + sol::table cmds = luaState->create_table(); + cmds["join_allowed"] = this->commands.join_allowed; + cmds["join_allow_from_other_chats"] = + this->commands.join_allow_from_other_chats; + o["commands"] = cmds; + + // --- OWNER + sol::table owner = luaState->create_table(); + if (this->owner.name.has_value()) { + owner["name"] = this->owner.name.value(); + } else { + owner["name"] = sol::nil; + } + if (this->owner.id.has_value()) { + owner["id"] = this->owner.id.value(); + } else { + owner["id"] = sol::nil; + } + o["owner"] = owner; + + // --- URL + sol::table url = luaState->create_table(); + if (this->url.help.has_value()) { + url["help"] = this->url.help.value(); + } else { + url["help"] = sol::nil; + } + if (this->url.paste_service.has_value()) { + url["paste_service"] = this->url.paste_service.value(); + } else { + url["paste_service"] = sol::nil; + } + o["url"] = url; + + return o; + } + std::optional parse_configuration_from_file( const std::string &file_path) { std::ifstream ifs(file_path); diff --git a/bot/src/config.hpp b/bot/src/config.hpp index db73f9a..11493d7 100644 --- a/bot/src/config.hpp +++ b/bot/src/config.hpp @@ -53,6 +53,8 @@ namespace bot { OwnerConfiguration owner; UrlConfiguration url; TokenConfiguration tokens; + + sol::table as_lua_table(std::shared_ptr luaState) const; }; std::optional parse_configuration_from_file( -- cgit v1.2.3