From 66375efc2b5067dd2902be38906de2e2a6cddeae Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 9 Apr 2025 02:02:43 +0400 Subject: feat: net functions --- bot/src/commands/lua.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'bot/src/commands/lua.cpp') 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 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()] = kv.second.as(); + } + + 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 state) { add_bot_library(state); add_time_library(state); add_json_library(state); + add_net_library(state); } void add_twitch_library(std::shared_ptr state, -- cgit v1.2.3