more fixes
This commit is contained in:
parent
a32954453b
commit
4a0af5a1e5
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
build/
|
||||
.vscode/
|
||||
.vscode/
|
||||
.cache/
|
@ -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)
|
||||
|
@ -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<bool>() : false) {
|
||||
}
|
||||
bool isBot;
|
||||
/*
|
||||
nlohmann::json extract(const std::vector<std::string>& keys) {
|
||||
std::vector<std::string> 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
|
@ -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<int>()) + " " + res["t"].dump());
|
||||
switch (res["op"].get<GatewayOpcodes>()) {
|
||||
case GatewayOpcodes::Hello:
|
||||
heartbeat_interval = res["d"]["heartbeat_interval"].get<int>();
|
||||
@ -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();
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ private:
|
||||
CURL* curl;
|
||||
CURLcode res;
|
||||
WebSocket& web;
|
||||
std::chrono::duration<double, std::milli> duration;
|
||||
//std::chrono::duration<double, std::milli> 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);
|
||||
|
@ -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());
|
||||
|
@ -26,17 +26,18 @@ int main(int argc, char* argv[]) {
|
||||
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();
|
||||
std::cout << std::boolalpha << user->isBot << 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) {
|
||||
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));
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user