diff options
Diffstat (limited to 'bot/src/commands/lua.hpp')
| -rw-r--r-- | bot/src/commands/lua.hpp | 63 |
1 files changed, 63 insertions, 0 deletions
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 |
