From 4a0af5a1e53409d003ac5770d2ac21977647963c Mon Sep 17 00:00:00 2001 From: fluttershy Date: Sun, 26 Jan 2025 23:33:52 +0500 Subject: [PATCH] more fixes --- .gitignore | 3 ++- CMakeLists.txt | 2 +- libs/api/User.hpp | 18 +++++++----------- libs/gateway/websocket.hpp | 13 +++++++------ libs/network/network.hpp | 13 +++++++------ libs/utils/log.hpp | 11 +++++++++++ sources/main.cpp | 9 +++++---- 7 files changed, 40 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 01f9cb9..7f8d2fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ -.vscode/ \ No newline at end of file +.vscode/ +.cache/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 366d91d..7ed77ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ set(SOURCE sources/main.cpp) set(LIBS ${CMAKE_SOURCE_DIR}/libs/) set(INCLUDE ${CMAKE_SOURCE_DIR}/include/) set(TESTS ${CMAKE_SOURCE_DIR}/tests) -set(ADDITIONAL_CXX_FLAGS_DEBUG "-pipe -Wall -Wextra -O0 -fsanitize=address") +set(ADDITIONAL_CXX_FLAGS_DEBUG "-march=native -pipe -Wall -Wextra -fsanitize=address -O0 -flto") set(ADDITIONAL_CXX_FLAGS_RELEASE "-march=native -pipe -Wall -Wextra -O2 -flto") find_package(CURL REQUIRED) diff --git a/libs/api/User.hpp b/libs/api/User.hpp index ee4ab9e..2ebb687 100644 --- a/libs/api/User.hpp +++ b/libs/api/User.hpp @@ -9,8 +9,11 @@ private: WebSocket& web; NetworkManager& req; public: - User(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {} -#ifdef DEBUG + User(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()), + isBot(data["d"]["user"].contains("bot") ? data["d"]["user"]["bot"].get() : false) { + } + bool isBot; + /* nlohmann::json extract(const std::vector& keys) { std::vector d = { "d" }; d.insert(d.end(), keys.begin(), keys.end()); @@ -23,17 +26,10 @@ public: } } return current; - } -#endif + }; + */ std::string me() { return req.request(HttpMethods::GET, DiscordEndpoints::details::latest + "/users/@me"); } - bool isBot() const { - try { - return data["d"]["author"]["bot"]; - } catch (...) { - return false; - } - } }; #endif \ No newline at end of file diff --git a/libs/gateway/websocket.hpp b/libs/gateway/websocket.hpp index fbaa747..1f8e1fe 100644 --- a/libs/gateway/websocket.hpp +++ b/libs/gateway/websocket.hpp @@ -41,14 +41,14 @@ private: int intents; bool isBot; ix::WebSocket webSocket; - const nlohmann::json payload = { {"op", 1}, {"d", nullptr} }; + const nlohmann::json payload = { {"op", GatewayOpcodes::Heartbeat}, {"d", nullptr} }; WebSocket& operator=(const WebSocket&) = delete; WebSocket& operator=(WebSocket&&) = delete; WebSocket(WebSocket&&) = delete; WebSocket(const WebSocket&) = delete; - WebSocket(const std::string& token = "", const int& intents = 0, const bool& isBot = true) : token(token), intents(intents), isBot(isBot) { + WebSocket(const std::string& token, const int& intents, const bool& isBot) : token(token), intents(intents), isBot(isBot) { nlohmann::json id = { - {"op", 2}, + {"op", GatewayOpcodes::Identify}, {"d", { {"token", token}, {"intents", intents}, @@ -73,13 +73,13 @@ private: }; ix::initNetSystem(); webSocket.setUrl("wss://gateway.discord.gg/?v=10&encoding=json"); - //webSocket.disableAutomaticReconnection(); + webSocket.disableAutomaticReconnection(); Log::create(INFO, WEBSOCKET, "ixwebsocket init"); webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 0, connected = false, id](const ix::WebSocketMessagePtr& msg) mutable { switch (msg->type) { case ix::WebSocketMessageType::Message: res = nlohmann::json::parse(msg->str); - Log::create(INFO, WEBSOCKET, res["op"].dump() + " " + res["t"].dump()); + Log::create(INFO, WEBSOCKET, std::to_string(res["op"].get()) + " " + res["t"].dump()); switch (res["op"].get()) { case GatewayOpcodes::Hello: heartbeat_interval = res["d"]["heartbeat_interval"].get(); @@ -100,12 +100,13 @@ private: } break; case ix::WebSocketMessageType::Error: - std::cout << msg->errorInfo.reason << std::endl; + Log::force_create(ERROR, WEBSOCKET, msg->errorInfo.reason); exit(-1); break; default: break; } + }); webSocket.start(); } diff --git a/libs/network/network.hpp b/libs/network/network.hpp index ce5945a..841a6f4 100644 --- a/libs/network/network.hpp +++ b/libs/network/network.hpp @@ -8,7 +8,7 @@ private: CURL* curl; CURLcode res; WebSocket& web; - std::chrono::duration duration; + //std::chrono::duration duration; NetworkManager& operator=(const NetworkManager&) = delete; NetworkManager& operator=(NetworkManager&&) = delete; NetworkManager(NetworkManager&&) = delete; @@ -16,7 +16,8 @@ private: NetworkManager() : web(WebSocket::getInstance()) { Log::create(INFO, NETWORK, "Network init"); curl_global_init(CURL_GLOBAL_DEFAULT); - if (!(curl = curl_easy_init())) { + curl = curl_easy_init(); + if (!curl) { Log::create(CRITICAL, NETWORK, "Failed to initialize CURL"); abort(); } @@ -33,12 +34,12 @@ private: } public: static NetworkManager& getInstance() { - Log::create(WARNING, NETWORK, "Instance event"); + Log::create(INFO, NETWORK, "Instance event"); static NetworkManager instance; return instance; } unsigned long getLatency() const { - return duration.count(); + return 0; } std::string request(const std::string& method, const std::string& path, const std::string& data = "") { std::string response; @@ -55,10 +56,10 @@ public: curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY); - auto start = std::chrono::steady_clock::now(); + //auto start = std::chrono::steady_clock::now(); if ((res = curl_easy_perform(curl)) != 0) Log::create(ERROR, NETWORK, "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res))); curl_slist_free_all(headers); - duration = std::chrono::steady_clock::now() - start; + //duration = std::chrono::steady_clock::now() - start; } #ifdef DEBUG Log::create(INFO, NETWORK, response); diff --git a/libs/utils/log.hpp b/libs/utils/log.hpp index fade943..97482e5 100644 --- a/libs/utils/log.hpp +++ b/libs/utils/log.hpp @@ -22,6 +22,17 @@ public: std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[0m" << message << "\033[0m" << std::endl; #endif } + static void force_create(const level& lvl, const type& t, const std::string& message) { + std::string color; + switch (lvl) { + case INFO: color = "\033[34;1m"; break; + case WARNING: color = "\033[33;1m"; break; + case ERROR: color = "\033[31;1m"; break; + case CRITICAL: color = "\033[31;1;2m"; break; + default: color = "\033[0m"; break; + } + std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[0m" << message << "\033[0m" << std::endl; + } private: static const std::string getCurrentTime() { std::time_t now_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); diff --git a/sources/main.cpp b/sources/main.cpp index 0b854d1..9526eff 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -26,17 +26,18 @@ int main(int argc, char* argv[]) { return -1; } - WebSocket* bot = &WebSocket::getInstance(result["token"].as(), GatewayIntents::AllIntents); + class WebSocket* bot = &WebSocket::getInstance(result["token"].as(), GatewayIntents::AllIntents); - bot->on(GatewayEvents::READY, [](const Discord& ctx) { + bot->on(GatewayEvents::READY, [](const Discord& ctx) { auto& [user] = ctx.ctx(); + std::cout << std::boolalpha << user->isBot << std::endl; std::cout << nlohmann::json::parse(user->me())["username"].get() << std::endl; }); bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord& msg) { - const auto& [message, user, author] = msg.ctx(); + auto& [message, user, author] = msg.ctx(); if (!author->isBot) { - message->send("939957962972229634", message->pack("content", author->channel_id)); + message->send("939957962972229634", message->pack("content", author->content)); } });