diff --git a/CMakeLists.txt b/CMakeLists.txt index 508a7b0..1db9cda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,6 @@ find_package(CURL REQUIRED) find_path(IXWEBSOCKET_INCLUDE_DIR ixwebsocket) find_library(IXWEBSOCKET_LIBRARIES ixwebsocket) -message(STATUS "Current compiler: ${CMAKE_CXX_COMPILER}") - if(NOT IXWEBSOCKET_INCLUDE_DIR OR NOT IXWEBSOCKET_LIBRARIES) message(FATAL_ERROR "ixwebsocket not found") endif() @@ -42,6 +40,7 @@ if(CMAKE_BUILD_TYPE) endif() endif() +message(STATUS "Current compiler: ${CMAKE_CXX_COMPILER}") message(STATUS "${CMAKE_BUILD_TYPE}") add_executable(${PROJECT_NAME} ${SOURCE}) diff --git a/libs/api/Author.hpp b/libs/api/Author.hpp index e2157df..1e63152 100644 --- a/libs/api/Author.hpp +++ b/libs/api/Author.hpp @@ -4,8 +4,7 @@ #include class Author { private: - nlohmann::json data; - const nlohmann::json& d; + const nlohmann::json& data, &d; WebSocket& web; NetworkManager& req; public: diff --git a/libs/api/Bot.hpp b/libs/api/Bot.hpp index f558026..446a19f 100644 --- a/libs/api/Bot.hpp +++ b/libs/api/Bot.hpp @@ -4,16 +4,10 @@ template class Discord { private: + const nlohmann::json& data; std::tuple...> net; - nlohmann::json data; - template - void initializeCtx(const nlohmann::json& data, std::index_sequence) { - net = std::make_tuple(std::make_unique(data)...); - } public: - Discord(const nlohmann::json& data) : data(data) { - initializeCtx(data, std::index_sequence_for{}); - } + Discord(const nlohmann::json& data) : data(data), net(std::make_tuple(std::make_unique(data)...)) {} template [[deprecated("Use ctx() instead")]] auto& get() const { return *std::get>(net); @@ -24,7 +18,7 @@ public: }; class Bot { private: - nlohmann::json data; + const nlohmann::json& data; public: Bot(const nlohmann::json& data) : data(data) {}; std::string id() const { diff --git a/libs/api/Channel.hpp b/libs/api/Channel.hpp index f46a5fa..e92a946 100644 --- a/libs/api/Channel.hpp +++ b/libs/api/Channel.hpp @@ -4,11 +4,11 @@ #include class Channel { private: - nlohmann::json data; + const nlohmann::json& data; WebSocket& web; NetworkManager& req; public: - Channel(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {} + Channel(const nlohmann::json& data) : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {} std::string send(const nlohmann::json& msg) { return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get() + "/messages", msg.dump()); } diff --git a/libs/api/Message.hpp b/libs/api/Message.hpp index 9de19b2..d773273 100644 --- a/libs/api/Message.hpp +++ b/libs/api/Message.hpp @@ -4,11 +4,11 @@ #include class Message { private: - nlohmann::json data; + const nlohmann::json& data; WebSocket& web; NetworkManager& req; public: - Message(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {}; + Message(const nlohmann::json& data) : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {}; std::string send(const std::string& id, const nlohmann::json& msg) { return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + id + "/messages", msg.dump()); } @@ -27,9 +27,9 @@ public: return { { key, value } }; } struct Api { - nlohmann::json data; + const nlohmann::json& data; NetworkManager& req; - Api(const nlohmann::json& json = "") : data(json), req(NetworkManager::getInstance()) {}; + Api(const nlohmann::json& json) : data(json), req(NetworkManager::getInstance()) {}; unsigned latency() const { return req.getLatency(); } diff --git a/libs/api/User.hpp b/libs/api/User.hpp index 2ebb687..5064532 100644 --- a/libs/api/User.hpp +++ b/libs/api/User.hpp @@ -5,13 +5,12 @@ #include class User { private: - nlohmann::json data; + const nlohmann::json& data; WebSocket& web; NetworkManager& req; public: 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) { - } + isBot(data["d"]["user"].contains("bot") ? data["d"]["user"]["bot"].get() : false) {} bool isBot; /* nlohmann::json extract(const std::vector& keys) { diff --git a/libs/gateway/websocket.hpp b/libs/gateway/websocket.hpp index 1f8e1fe..137341d 100644 --- a/libs/gateway/websocket.hpp +++ b/libs/gateway/websocket.hpp @@ -16,6 +16,16 @@ protected: } } public: + void getEvents() { + Log::create(CRITICAL, WEBSOCKET, "Event list"); + for (const auto& handler : handlers) { + Log::create(INFO, WEBSOCKET, handler.first); + } + Log::create(CRITICAL, WEBSOCKET, "End of list"); + } + void start() { + while (1) std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } void on(const std::string& event, eventHandlers handler) { handlers[event].emplace_back(handler); } @@ -139,8 +149,5 @@ public: int getIntents() const { return WebSocket::intents; } - void start() { - while (1) std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } }; #endif \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index 9526eff..85ab742 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -34,13 +34,13 @@ int main(int argc, char* argv[]) { std::cout << nlohmann::json::parse(user->me())["username"].get() << std::endl; }); - bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord& msg) { - auto& [message, user, author] = msg.ctx(); + bot->on(GatewayEvents::MESSAGE_CREATE, [bot](const Discord& msg) { + auto& [message, author] = msg.ctx(); if (!author->isBot) { message->send("939957962972229634", message->pack("content", author->content)); + bot->getEvents(); } }); - bot->start(); return 0; } \ No newline at end of file