This commit is contained in:
fluttershy 2025-01-27 17:24:33 +05:00
parent 9542885b42
commit f5341d6ea5
8 changed files with 26 additions and 28 deletions

View File

@ -18,8 +18,6 @@ find_package(CURL REQUIRED)
find_path(IXWEBSOCKET_INCLUDE_DIR ixwebsocket) find_path(IXWEBSOCKET_INCLUDE_DIR ixwebsocket)
find_library(IXWEBSOCKET_LIBRARIES ixwebsocket) find_library(IXWEBSOCKET_LIBRARIES ixwebsocket)
message(STATUS "Current compiler: ${CMAKE_CXX_COMPILER}")
if(NOT IXWEBSOCKET_INCLUDE_DIR OR NOT IXWEBSOCKET_LIBRARIES) if(NOT IXWEBSOCKET_INCLUDE_DIR OR NOT IXWEBSOCKET_LIBRARIES)
message(FATAL_ERROR "ixwebsocket not found") message(FATAL_ERROR "ixwebsocket not found")
endif() endif()
@ -42,6 +40,7 @@ if(CMAKE_BUILD_TYPE)
endif() endif()
endif() endif()
message(STATUS "Current compiler: ${CMAKE_CXX_COMPILER}")
message(STATUS "${CMAKE_BUILD_TYPE}") message(STATUS "${CMAKE_BUILD_TYPE}")
add_executable(${PROJECT_NAME} ${SOURCE}) add_executable(${PROJECT_NAME} ${SOURCE})

View File

@ -4,8 +4,7 @@
#include <net.hpp> #include <net.hpp>
class Author { class Author {
private: private:
nlohmann::json data; const nlohmann::json& data, &d;
const nlohmann::json& d;
WebSocket& web; WebSocket& web;
NetworkManager& req; NetworkManager& req;
public: public:

View File

@ -4,16 +4,10 @@
template<typename...Args> template<typename...Args>
class Discord { class Discord {
private: private:
const nlohmann::json& data;
std::tuple<std::unique_ptr<Args>...> net; std::tuple<std::unique_ptr<Args>...> net;
nlohmann::json data;
template<unsigned long... Is>
void initializeCtx(const nlohmann::json& data, std::index_sequence<Is...>) {
net = std::make_tuple(std::make_unique<Args>(data)...);
}
public: public:
Discord(const nlohmann::json& data) : data(data) { Discord(const nlohmann::json& data) : data(data), net(std::make_tuple(std::make_unique<Args>(data)...)) {}
initializeCtx(data, std::index_sequence_for<Args...>{});
}
template<class T> template<class T>
[[deprecated("Use ctx() instead")]] auto& get() const { [[deprecated("Use ctx() instead")]] auto& get() const {
return *std::get<std::unique_ptr<T>>(net); return *std::get<std::unique_ptr<T>>(net);
@ -24,7 +18,7 @@ public:
}; };
class Bot { class Bot {
private: private:
nlohmann::json data; const nlohmann::json& data;
public: public:
Bot(const nlohmann::json& data) : data(data) {}; Bot(const nlohmann::json& data) : data(data) {};
std::string id() const { std::string id() const {

View File

@ -4,11 +4,11 @@
#include <net.hpp> #include <net.hpp>
class Channel { class Channel {
private: private:
nlohmann::json data; const nlohmann::json& data;
WebSocket& web; WebSocket& web;
NetworkManager& req; NetworkManager& req;
public: 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) { std::string send(const nlohmann::json& msg) {
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<std::string>() + "/messages", msg.dump()); return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<std::string>() + "/messages", msg.dump());
} }

View File

@ -4,11 +4,11 @@
#include <net.hpp> #include <net.hpp>
class Message { class Message {
private: private:
nlohmann::json data; const nlohmann::json& data;
WebSocket& web; WebSocket& web;
NetworkManager& req; NetworkManager& req;
public: 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) { std::string send(const std::string& id, const nlohmann::json& msg) {
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + id + "/messages", msg.dump()); return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + id + "/messages", msg.dump());
} }
@ -27,9 +27,9 @@ public:
return { { key, value } }; return { { key, value } };
} }
struct Api { struct Api {
nlohmann::json data; const nlohmann::json& data;
NetworkManager& req; 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 { unsigned latency() const {
return req.getLatency(); return req.getLatency();
} }

View File

@ -5,13 +5,12 @@
#include <vector> #include <vector>
class User { class User {
private: private:
nlohmann::json data; const nlohmann::json& data;
WebSocket& web; WebSocket& web;
NetworkManager& req; NetworkManager& req;
public: public:
User(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()), User(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()),
isBot(data["d"]["user"].contains("bot") ? data["d"]["user"]["bot"].get<bool>() : false) { isBot(data["d"]["user"].contains("bot") ? data["d"]["user"]["bot"].get<bool>() : false) {}
}
bool isBot; bool isBot;
/* /*
nlohmann::json extract(const std::vector<std::string>& keys) { nlohmann::json extract(const std::vector<std::string>& keys) {

View File

@ -16,6 +16,16 @@ protected:
} }
} }
public: 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) { void on(const std::string& event, eventHandlers handler) {
handlers[event].emplace_back(handler); handlers[event].emplace_back(handler);
} }
@ -139,8 +149,5 @@ public:
int getIntents() const { int getIntents() const {
return WebSocket::intents; return WebSocket::intents;
} }
void start() {
while (1) std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}; };
#endif #endif

View File

@ -34,13 +34,13 @@ int main(int argc, char* argv[]) {
std::cout << nlohmann::json::parse(user->me())["username"].get<std::string>() << std::endl; std::cout << nlohmann::json::parse(user->me())["username"].get<std::string>() << std::endl;
}); });
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author>& msg) { bot->on(GatewayEvents::MESSAGE_CREATE, [bot](const Discord<Message, Author>& msg) {
auto& [message, user, author] = msg.ctx(); auto& [message, author] = msg.ctx();
if (!author->isBot) { if (!author->isBot) {
message->send("939957962972229634", message->pack("content", author->content)); message->send("939957962972229634", message->pack("content", author->content));
bot->getEvents();
} }
}); });
bot->start(); bot->start();
return 0; return 0;
} }