more fixes

This commit is contained in:
fluttershy 2025-01-26 23:33:52 +05:00
parent a32954453b
commit 4a0af5a1e5
7 changed files with 40 additions and 29 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
build/ build/
.vscode/ .vscode/
.cache/

View File

@ -11,7 +11,7 @@ set(SOURCE sources/main.cpp)
set(LIBS ${CMAKE_SOURCE_DIR}/libs/) set(LIBS ${CMAKE_SOURCE_DIR}/libs/)
set(INCLUDE ${CMAKE_SOURCE_DIR}/include/) set(INCLUDE ${CMAKE_SOURCE_DIR}/include/)
set(TESTS ${CMAKE_SOURCE_DIR}/tests) 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") set(ADDITIONAL_CXX_FLAGS_RELEASE "-march=native -pipe -Wall -Wextra -O2 -flto")
find_package(CURL REQUIRED) find_package(CURL REQUIRED)

View File

@ -9,8 +9,11 @@ private:
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()),
#ifdef DEBUG isBot(data["d"]["user"].contains("bot") ? data["d"]["user"]["bot"].get<bool>() : false) {
}
bool isBot;
/*
nlohmann::json extract(const std::vector<std::string>& keys) { nlohmann::json extract(const std::vector<std::string>& keys) {
std::vector<std::string> d = { "d" }; std::vector<std::string> d = { "d" };
d.insert(d.end(), keys.begin(), keys.end()); d.insert(d.end(), keys.begin(), keys.end());
@ -23,17 +26,10 @@ public:
} }
} }
return current; return current;
} };
#endif */
std::string me() { std::string me() {
return req.request(HttpMethods::GET, DiscordEndpoints::details::latest + "/users/@me"); return req.request(HttpMethods::GET, DiscordEndpoints::details::latest + "/users/@me");
} }
bool isBot() const {
try {
return data["d"]["author"]["bot"];
} catch (...) {
return false;
}
}
}; };
#endif #endif

View File

@ -41,14 +41,14 @@ private:
int intents; int intents;
bool isBot; bool isBot;
ix::WebSocket webSocket; 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=(const WebSocket&) = delete;
WebSocket& operator=(WebSocket&&) = delete; WebSocket& operator=(WebSocket&&) = delete;
WebSocket(WebSocket&&) = delete; WebSocket(WebSocket&&) = delete;
WebSocket(const 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 = { nlohmann::json id = {
{"op", 2}, {"op", GatewayOpcodes::Identify},
{"d", { {"d", {
{"token", token}, {"token", token},
{"intents", intents}, {"intents", intents},
@ -73,13 +73,13 @@ private:
}; };
ix::initNetSystem(); ix::initNetSystem();
webSocket.setUrl("wss://gateway.discord.gg/?v=10&encoding=json"); webSocket.setUrl("wss://gateway.discord.gg/?v=10&encoding=json");
//webSocket.disableAutomaticReconnection(); webSocket.disableAutomaticReconnection();
Log::create(INFO, WEBSOCKET, "ixwebsocket init"); Log::create(INFO, WEBSOCKET, "ixwebsocket init");
webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 0, connected = false, id](const ix::WebSocketMessagePtr& msg) mutable { webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 0, connected = false, id](const ix::WebSocketMessagePtr& msg) mutable {
switch (msg->type) { switch (msg->type) {
case ix::WebSocketMessageType::Message: case ix::WebSocketMessageType::Message:
res = nlohmann::json::parse(msg->str); 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<int>()) + " " + res["t"].dump());
switch (res["op"].get<GatewayOpcodes>()) { switch (res["op"].get<GatewayOpcodes>()) {
case GatewayOpcodes::Hello: case GatewayOpcodes::Hello:
heartbeat_interval = res["d"]["heartbeat_interval"].get<int>(); heartbeat_interval = res["d"]["heartbeat_interval"].get<int>();
@ -100,12 +100,13 @@ private:
} }
break; break;
case ix::WebSocketMessageType::Error: case ix::WebSocketMessageType::Error:
std::cout << msg->errorInfo.reason << std::endl; Log::force_create(ERROR, WEBSOCKET, msg->errorInfo.reason);
exit(-1); exit(-1);
break; break;
default: default:
break; break;
} }
}); });
webSocket.start(); webSocket.start();
} }

View File

@ -8,7 +8,7 @@ private:
CURL* curl; CURL* curl;
CURLcode res; CURLcode res;
WebSocket& web; WebSocket& web;
std::chrono::duration<double, std::milli> duration; //std::chrono::duration<double, std::milli> duration;
NetworkManager& operator=(const NetworkManager&) = delete; NetworkManager& operator=(const NetworkManager&) = delete;
NetworkManager& operator=(NetworkManager&&) = delete; NetworkManager& operator=(NetworkManager&&) = delete;
NetworkManager(NetworkManager&&) = delete; NetworkManager(NetworkManager&&) = delete;
@ -16,7 +16,8 @@ private:
NetworkManager() : web(WebSocket::getInstance()) { NetworkManager() : web(WebSocket::getInstance()) {
Log::create(INFO, NETWORK, "Network init"); Log::create(INFO, NETWORK, "Network init");
curl_global_init(CURL_GLOBAL_DEFAULT); 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"); Log::create(CRITICAL, NETWORK, "Failed to initialize CURL");
abort(); abort();
} }
@ -33,12 +34,12 @@ private:
} }
public: public:
static NetworkManager& getInstance() { static NetworkManager& getInstance() {
Log::create(WARNING, NETWORK, "Instance event"); Log::create(INFO, NETWORK, "Instance event");
static NetworkManager instance; static NetworkManager instance;
return instance; return instance;
} }
unsigned long getLatency() const { 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 request(const std::string& method, const std::string& path, const std::string& data = "") {
std::string response; std::string response;
@ -55,10 +56,10 @@ public:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY); 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))); 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); curl_slist_free_all(headers);
duration = std::chrono::steady_clock::now() - start; //duration = std::chrono::steady_clock::now() - start;
} }
#ifdef DEBUG #ifdef DEBUG
Log::create(INFO, NETWORK, response); Log::create(INFO, NETWORK, response);

View File

@ -22,6 +22,17 @@ public:
std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[0m" << message << "\033[0m" << std::endl; std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[0m" << message << "\033[0m" << std::endl;
#endif #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: private:
static const std::string getCurrentTime() { static const std::string getCurrentTime() {
std::time_t now_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); std::time_t now_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

View File

@ -26,17 +26,18 @@ int main(int argc, char* argv[]) {
return -1; return -1;
} }
WebSocket* bot = &WebSocket::getInstance(result["token"].as<std::string>(), GatewayIntents::AllIntents); class WebSocket* bot = &WebSocket::getInstance(result["token"].as<std::string>(), GatewayIntents::AllIntents);
bot->on(GatewayEvents::READY, [](const Discord<User>& ctx) { bot->on(GatewayEvents::READY, [](const Discord<User>& ctx) {
auto& [user] = ctx.ctx(); auto& [user] = ctx.ctx();
std::cout << std::boolalpha << user->isBot << std::endl;
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, [](const Discord<Message, User, Author>& msg) {
const auto& [message, user, author] = msg.ctx(); auto& [message, user, author] = msg.ctx();
if (!author->isBot) { if (!author->isBot) {
message->send("939957962972229634", message->pack("content", author->channel_id)); message->send("939957962972229634", message->pack("content", author->content));
} }
}); });