summaryrefslogtreecommitdiff
path: root/bot
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-04-09 21:54:40 +0500
committerilotterytea <iltsu@alright.party>2025-04-09 21:54:40 +0500
commitf2bf41bc614c7b070649a29b6fe9ca48cc1eafbb (patch)
treebadaf7067a0d0206c7892f696f7350f5021ca8a1 /bot
parentb89a39dcda1f31e1105768724c0efb36c612bebe (diff)
feat: bot_config() function for lua
Diffstat (limited to 'bot')
-rw-r--r--bot/src/commands/lua.cpp11
-rw-r--r--bot/src/config.cpp44
-rw-r--r--bot/src/config.hpp2
3 files changed, 57 insertions, 0 deletions
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<bot::Configuration> 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<sol::state> 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<sol::state> 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<Configuration> 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<sol::state> luaState) const;
};
std::optional<Configuration> parse_configuration_from_file(