diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-09 02:02:43 +0400 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-09 02:02:58 +0400 |
| commit | 66375efc2b5067dd2902be38906de2e2a6cddeae (patch) | |
| tree | ca73606c63633698907b54066d4c65afa8522720 /bot/src/commands | |
| parent | e1e754cff8d04269ff9276f7c42c94c8cfe8b85a (diff) | |
feat: net functions
Diffstat (limited to 'bot/src/commands')
| -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); } |
