From 112136c184bcb226fa84e9b5a39c2d1dc557c4d3 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 8 Apr 2025 01:07:11 +0400 Subject: feat: a method for loading a single lua file --- bot/src/commands/command.cpp | 30 +++++++++++++++++------------- bot/src/commands/command.hpp | 1 + 2 files changed, 18 insertions(+), 13 deletions(-) (limited to 'bot/src/commands') diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index 87eecfd..4544810 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -61,22 +61,26 @@ namespace bot { void CommandLoader::load_lua_directory(const std::string &folder_path) { for (const auto &entry : std::filesystem::directory_iterator(folder_path)) { - std::ifstream ifs(entry.path()); - if (!ifs.is_open()) { - throw new std::runtime_error("Failed to open the Lua file at " + - entry.path().string()); - } - std::string content, line; - - while (std::getline(ifs, line)) { - content += line + '\n'; - } + load_lua_file(entry.path()); + } + } - ifs.close(); + void CommandLoader::load_lua_file(const std::string &file_path) { + std::ifstream ifs(file_path); + if (!ifs.is_open()) { + throw new std::runtime_error("Failed to open the Lua file at " + + file_path); + } + std::string content, line; - this->add_command( - std::make_unique(this->luaState, content)); + while (std::getline(ifs, line)) { + content += line + '\n'; } + + ifs.close(); + + this->add_command( + std::make_unique(this->luaState, content)); } void CommandLoader::add_command(std::unique_ptr command) { diff --git a/bot/src/commands/command.hpp b/bot/src/commands/command.hpp index fcaacdb..507504c 100644 --- a/bot/src/commands/command.hpp +++ b/bot/src/commands/command.hpp @@ -44,6 +44,7 @@ namespace bot { void add_command(std::unique_ptr cmd); void load_lua_directory(const std::string &folder_path); + void load_lua_file(const std::string &file_path); std::optional run(const InstanceBundle &bundle, const Request &msg); -- cgit v1.2.3