From 41d1bd526827c3cabdb306347d0d18e8fea5d443 Mon Sep 17 00:00:00 2001 From: fluttershy Date: Tue, 31 Dec 2024 05:58:15 +0500 Subject: [PATCH] some fixes --- .gitignore | 1 + .vscode/settings.json | 76 -------------------------------------- libs/api/Channel.hpp | 2 +- libs/api/Message.hpp | 2 +- libs/api/User.hpp | 2 +- libs/gateway/websocket.hpp | 40 ++++++++++---------- libs/tls/network.hpp | 10 +++-- 7 files changed, 29 insertions(+), 104 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 567609b..01f9cb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +.vscode/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 6061cab..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,76 +0,0 @@ -{ - "files.associations": { - "any": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "chrono": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "codecvt": "cpp", - "compare": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "forward_list": "cpp", - "list": "cpp", - "map": "cpp", - "set": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "unordered_set": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "format": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "ranges": "cpp", - "semaphore": "cpp", - "span": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "text_encoding": "cpp", - "thread": "cpp", - "cinttypes": "cpp", - "typeinfo": "cpp", - "valarray": "cpp", - "variant": "cpp" - } -} \ No newline at end of file diff --git a/libs/api/Channel.hpp b/libs/api/Channel.hpp index 2eb830c..9ece6e2 100644 --- a/libs/api/Channel.hpp +++ b/libs/api/Channel.hpp @@ -19,7 +19,7 @@ private: public: Channel(const json& data) : data(data), web(*WebSocket::getInstance()), req(*NetworkManager::getInstance()) {} string send(const json& msg) const { - return req.request("POST", dapi + "/channels/" + data["d"]["channel_id"].get() + "/messages", web.getToken(), msg.dump()); + return req.request("POST", dapi + "/channels/" + data["d"]["channel_id"].get() + "/messages", msg.dump()); } }; #endif \ No newline at end of file diff --git a/libs/api/Message.hpp b/libs/api/Message.hpp index d019966..193be72 100644 --- a/libs/api/Message.hpp +++ b/libs/api/Message.hpp @@ -19,7 +19,7 @@ private: public: Message(const json& data) : data(data), web(*WebSocket::getInstance()), req(*NetworkManager::getInstance()) {}; string send(const string& id, const json& msg) { - return req.request("POST", dapi + "/channels/" + id + "/messages", web.getToken(), msg.dump()); + return req.request("POST", dapi + "/channels/" + id + "/messages", msg.dump()); } }; #endif \ No newline at end of file diff --git a/libs/api/User.hpp b/libs/api/User.hpp index cab6b9d..faf2a2e 100644 --- a/libs/api/User.hpp +++ b/libs/api/User.hpp @@ -22,7 +22,7 @@ public: return data["d"]["author"]["id"]; } string me() const { - return req.request("GET", dapi + "/users/@me", web.getToken(), ""); + return req.request("GET", dapi + "/users/@me", ""); } bool isBot() { try { diff --git a/libs/gateway/websocket.hpp b/libs/gateway/websocket.hpp index 0bd13a1..72ae57d 100644 --- a/libs/gateway/websocket.hpp +++ b/libs/gateway/websocket.hpp @@ -2,7 +2,6 @@ #define GATEWAY_WEBSOCKET_HPP_ #include #include -#include #include #include #include @@ -17,7 +16,7 @@ using namespace std::chrono; using namespace std::chrono_literals; class WebSocket { private: - std::unordered_map events = { + std::array, 73> events = {{ {APPLICATION_COMMAND_PERMISSIONS_UPDATE, "APPLICATION_COMMAND_PERMISSIONS_UPDATE"}, {AUTO_MODERATION_ACTION_EXECUTION, "AUTO_MODERATION_ACTION_EXECUTION"}, {AUTO_MODERATION_RULE_CREATE, "AUTO_MODERATION_RULE_CREATE"}, @@ -91,29 +90,16 @@ private: {VOICE_SERVER_UPDATE, "VOICE_SERVER_UPDATE"}, {VOICE_STATE_UPDATE, "VOICE_STATE_UPDATE"}, {WEBHOOKS_UPDATE, "WEBHOOKS_UPDATE"} - }; + }}; bool connected = false; static bool initialized, isBot; static WebSocket* instance; static int i; + static std::string t; ix::WebSocket webSocket; int heartbeat_interval, lastS; json payload = { {"op", 1},{"d", nullptr} }, id, res; - static std::string t; - std::unordered_map> eventHandlers; - void test() { - std::thread([this]() { - while (1) { - json prsUpdate = { {"op", 3},{"d", {{"since", 9187921},{"activities", json::array({{"name", "dsa"}, {"type", 1}})},{"status", "online"},{"afk", false}}} }; - //json voice = { {"op", 4},{"d", {{"guild_id", "1322653635875967140"},{"channel_id", nullptr},{"self_mute", false},{"self_deaf", false}}} }; - webSocket.send(prsUpdate.dump()); - std::this_thread::sleep_for(10000ms); - json prsUpdate2 = { {"op", 3},{"d", {{"since", 9187921},{"activities", json::array({{"name", "asd"}, {"type", 1}})},{"status", "idle"},{"afk", false}}} }; - webSocket.send(prsUpdate2.dump()); - std::this_thread::sleep_for(10000ms); - } - }).detach(); - } + std::unordered_map> eventHandlers; WebSocket(const std::string& token, const int intents, bool isBot) : lastS(0), heartbeat_interval(-1) { WebSocket::t = token; WebSocket::i = intents; @@ -168,6 +154,18 @@ private: webSocket.start(); } public: + void sendPresenceUpdate(int statusType, const std::string& activityName) { + json prsUpdate = { + {"op", 3}, + {"d", { + {"since", 9187921}, + {"activities", json::array({{"name", activityName}, {"type", statusType}})}, + {"status", statusType == 1 ? "online" : "idle"}, + {"afk", false} + }} + }; + webSocket.send(prsUpdate.dump()); + } static WebSocket* getInstance(const std::string& token = "", const int intents = 0, bool bot = true) { if (!instance) { if (token.empty() || intents == 0) { @@ -189,13 +187,13 @@ public: return WebSocket::i; } void on(const int event, std::function handler) { - eventHandlers[events[event]] = [handler](const json& message) { + eventHandlers[events[event].second] = [handler](const json& message) { json eventData = message.get(); handler(eventData); }; } void once(const int event, std::function handler) { - eventHandlers[events[event]] = [event, this, handler, isCalled = false](const json& message) mutable { + eventHandlers[events[event].second] = [event, this, handler, isCalled = false](const json& message) mutable { if (isCalled == false) { isCalled = true; const json& eventData = message.get(); @@ -212,7 +210,7 @@ public: }).detach(); } void stop() { - while (1) std::this_thread::sleep_for(1s); + connected = false; } void start() { while (1) std::this_thread::sleep_for(1ms); diff --git a/libs/tls/network.hpp b/libs/tls/network.hpp index 32997a4..1984a4e 100644 --- a/libs/tls/network.hpp +++ b/libs/tls/network.hpp @@ -9,11 +9,13 @@ #include #include #include +#include using std::cout; using std::cerr; using std::endl; class NetworkManager { private: + WebSocket& web; static NetworkManager* instance; SSL_CTX* ctx; int sock = -1; @@ -55,7 +57,7 @@ private: } freeaddrinfo(res); } - NetworkManager() : ctx(nullptr), result("") { + NetworkManager() : ctx(nullptr), result(""), web(*WebSocket::getInstance()) { if (!ctx) { ctx = SSL_CTX_new(TLS_client_method()); if (!ctx) { @@ -80,10 +82,10 @@ public: if (ctx) SSL_CTX_free(ctx); close(sock); } - const std::string& request(const std::string& method, const std::string& path, const std::string& authorization, const std::string& data) { - auto net = [this, method, path, authorization, data, bytesRead = 0, request = std::string("")]() mutable -> void { + const std::string& request(const std::string& method, const std::string& path, const std::string& data) { + auto net = [this, method, path, data, bytesRead = 0, request = std::string("")]() mutable -> void { request = method + " " + path + " HTTP/1.1\r\nHost: discord.com\r\nAccept: application/json\r\nContent-Type: application/json\r\n"; - request += "Authorization: " + authorization + "\r\n"; + request += "Authorization: " + web.getToken() + "\r\n"; request += "Content-Length: " + std::to_string(data.length()) + "\r\n"; #ifdef DEBUG request += "Connection: close\r\n\r\n";