diff options
Diffstat (limited to 'bot/src')
| -rw-r--r-- | bot/src/commands/lua.cpp | 36 | ||||
| -rw-r--r-- | bot/src/commands/lua.hpp | 1 |
2 files changed, 37 insertions, 0 deletions
diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp index f11bc9e..34776b1 100644 --- a/bot/src/commands/lua.cpp +++ b/bot/src/commands/lua.cpp @@ -14,6 +14,9 @@ #include "commands/request.hpp" #include "commands/response.hpp" #include "commands/response_error.hpp" +#include "cpr/api.h" +#include "cpr/cprtypes.h" +#include "cpr/response.h" #include "schemas/user.hpp" #include "utils/chrono.hpp" #include "utils/string.hpp" @@ -164,10 +167,43 @@ namespace bot::command::lua { }); } + void add_net_library(std::shared_ptr<sol::state> state) { + state->set_function("net_get", [state](const std::string &url) { + sol::table t = state->create_table(); + + cpr::Response response = cpr::Get(cpr::Url{url}); + + t["code"] = response.status_code; + t["text"] = response.text; + + return t; + }); + + state->set_function( + "net_get_with_headers", + [state](const std::string &url, const sol::table &headers) { + sol::table t = state->create_table(); + + cpr::Header h{}; + + for (auto &kv : headers) { + h[kv.first.as<std::string>()] = kv.second.as<std::string>(); + } + + cpr::Response response = cpr::Get(cpr::Url{url}, h); + + t["code"] = response.status_code; + t["text"] = response.text; + + return t; + }); + } + void add_base_libraries(std::shared_ptr<sol::state> state) { add_bot_library(state); add_time_library(state); add_json_library(state); + add_net_library(state); } void add_twitch_library(std::shared_ptr<sol::state> state, diff --git a/bot/src/commands/lua.hpp b/bot/src/commands/lua.hpp index 28071b6..9853b27 100644 --- a/bot/src/commands/lua.hpp +++ b/bot/src/commands/lua.hpp @@ -22,6 +22,7 @@ namespace bot::command::lua { void add_twitch_library(std::shared_ptr<sol::state> state, const Request &request, const InstanceBundle &bundle); + void add_net_library(std::shared_ptr<sol::state> state); void add_base_libraries(std::shared_ptr<sol::state> state); } |
