diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-17 02:58:17 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-17 02:58:17 +0500 |
| commit | d54fa21b30a52c453aaa03c734e278f9fc8adb02 (patch) | |
| tree | 3f8c6558c6476eb2483b78bd8db4c09cc4ac55b9 /bot/src/commands | |
| parent | 0d1935d624a0f7c10cadc752c37f354bc792af32 (diff) | |
upd: moved lua command to main lua.hpp
Diffstat (limited to 'bot/src/commands')
| -rw-r--r-- | bot/src/commands/command.cpp | 5 | ||||
| -rw-r--r-- | bot/src/commands/lua.hpp | 63 |
2 files changed, 65 insertions, 3 deletions
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<mod::LuaExecution>()); - this->add_command(std::make_unique<mod::LuaRemoteExecution>()); + this->add_command(std::make_unique<lua::mod::LuaExecution>()); + this->add_command(std::make_unique<lua::mod::LuaRemoteExecution>()); this->luaState = std::make_shared<sol::state>(); 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<sol::state> 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<ResponseError::NOT_ENOUGH_ARGUMENTS>( + 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<ResponseError::NOT_ENOUGH_ARGUMENTS>( + request, bundle.localization, command::CommandArgument::VALUE); + } + + std::string url = request.message.value(); + + std::vector<std::string> 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<ResponseError::EXTERNAL_API_ERROR>( + 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<ResponseError::INCORRECT_ARGUMENT>( + 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 |
