From d54fa21b30a52c453aaa03c734e278f9fc8adb02 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Thu, 17 Apr 2025 02:58:17 +0500 Subject: upd: moved lua command to main lua.hpp --- bot/src/commands/command.cpp | 5 ++- bot/src/commands/lua.hpp | 63 ++++++++++++++++++++++++++++++++++++++ bot/src/modules/lua.hpp | 73 -------------------------------------------- 3 files changed, 65 insertions(+), 76 deletions(-) delete mode 100644 bot/src/modules/lua.hpp (limited to 'bot') diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index 417746c..2ecb80f 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -15,15 +15,14 @@ #include "../bundle.hpp" #include "../utils/chrono.hpp" #include "commands/lua.hpp" -#include "modules/lua.hpp" #include "request.hpp" #include "response.hpp" namespace bot { namespace command { CommandLoader::CommandLoader() { - this->add_command(std::make_unique()); - this->add_command(std::make_unique()); + this->add_command(std::make_unique()); + this->add_command(std::make_unique()); this->luaState = std::make_shared(); this->luaState->open_libraries(sol::lib::base, sol::lib::string, diff --git a/bot/src/commands/lua.hpp b/bot/src/commands/lua.hpp index bf48ab6..3d514f3 100644 --- a/bot/src/commands/lua.hpp +++ b/bot/src/commands/lua.hpp @@ -11,7 +11,9 @@ #include "bundle.hpp" #include "commands/command.hpp" #include "commands/response.hpp" +#include "commands/response_error.hpp" #include "config.hpp" +#include "cpr/api.h" #include "schemas/user.hpp" void print_lua_object_type(const sol::object &obj); @@ -72,4 +74,65 @@ namespace bot::command::lua { std::shared_ptr luaState; }; + + namespace mod { + class LuaExecution : public command::Command { + std::string get_name() const override { return "lua"; } + + int get_delay_seconds() const override { return 1; } + + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { + if (!request.message.has_value()) { + throw ResponseException( + request, bundle.localization, command::CommandArgument::VALUE); + } + + std::string script = request.message.value(); + + return command::lua::run_safe_lua_script(request, bundle, script); + } + }; + + class LuaRemoteExecution : public command::Command { + std::string get_name() const override { return "luaimport"; } + + int get_delay_seconds() const override { return 2; } + + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { + if (!request.message.has_value()) { + throw ResponseException( + request, bundle.localization, command::CommandArgument::VALUE); + } + + std::string url = request.message.value(); + + std::vector mimeTypes = {"text/plain", "text/x-lua"}; + + cpr::Response response = cpr::Get( + cpr::Url{url}, + cpr::Header{ + {"Accept", utils::string::join_vector(mimeTypes, ',')}, + {"User-Agent", "https://github.com/ilotterytea/bot"}}); + + if (response.status_code != 200) { + throw ResponseException( + request, bundle.localization, response.status_code); + } + + std::string contentType = response.header["Content-Type"]; + if (!std::any_of( + mimeTypes.begin(), mimeTypes.end(), + [&contentType](const auto &x) { return x == contentType; })) { + throw ResponseException( + request, bundle.localization, url); + } + + std::string script = response.text; + + return command::lua::run_safe_lua_script(request, bundle, script); + } + }; + } } \ No newline at end of file diff --git a/bot/src/modules/lua.hpp b/bot/src/modules/lua.hpp deleted file mode 100644 index 1f4b665..0000000 --- a/bot/src/modules/lua.hpp +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "bundle.hpp" -#include "commands/command.hpp" -#include "commands/lua.hpp" -#include "commands/response_error.hpp" -#include "cpr/api.h" -#include "utils/string.hpp" - -namespace bot::mod { - class LuaExecution : public command::Command { - std::string get_name() const override { return "lua"; } - - int get_delay_seconds() const override { return 1; } - - command::Response run(const InstanceBundle &bundle, - const command::Request &request) const override { - if (!request.message.has_value()) { - throw ResponseException( - request, bundle.localization, command::CommandArgument::VALUE); - } - - std::string script = request.message.value(); - - return command::lua::run_safe_lua_script(request, bundle, script); - } - }; - - class LuaRemoteExecution : public command::Command { - std::string get_name() const override { return "luaimport"; } - - int get_delay_seconds() const override { return 2; } - - command::Response run(const InstanceBundle &bundle, - const command::Request &request) const override { - if (!request.message.has_value()) { - throw ResponseException( - request, bundle.localization, command::CommandArgument::VALUE); - } - - std::string url = request.message.value(); - - std::vector mimeTypes = {"text/plain", "text/x-lua"}; - - cpr::Response response = cpr::Get( - cpr::Url{url}, - cpr::Header{{"Accept", utils::string::join_vector(mimeTypes, ',')}, - {"User-Agent", "https://github.com/ilotterytea/bot"}}); - - if (response.status_code != 200) { - throw ResponseException( - request, bundle.localization, response.status_code); - } - - std::string contentType = response.header["Content-Type"]; - if (!std::any_of( - mimeTypes.begin(), mimeTypes.end(), - [&contentType](const auto &x) { return x == contentType; })) { - throw ResponseException( - request, bundle.localization, url); - } - - std::string script = response.text; - - return command::lua::run_safe_lua_script(request, bundle, script); - } - }; -} \ No newline at end of file -- cgit v1.2.3