Compare commits

..

3 Commits

Author SHA1 Message Date
fluttershy
99cf38cf12 ifelse -> switch 2025-01-26 16:12:25 +05:00
fluttershy
69e16e9c96 patches 2025-01-26 15:48:38 +05:00
fluttershy
a72a3e3869 Merge pull request 'add cxxopts and change args handling' (#2) from lame_lexem/sparkle:main into main
Reviewed-on: http://applejack.ygg/fluttershy/sparkle/pulls/2
2025-01-25 15:22:54 +01:00
4 changed files with 85 additions and 65 deletions

View File

@ -7,5 +7,4 @@
#include <utils/functions.hpp> #include <utils/functions.hpp>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <memory>
#endif #endif

View File

@ -1,26 +1,53 @@
#ifndef GATEWAY_WEBSOCKET_HPP_ #ifndef GATEWAY_WEBSOCKET_HPP_
#define GATEWAY_WEBSOCKET_HPP_ #define GATEWAY_WEBSOCKET_HPP_
#include <includes.hpp> #include <includes.hpp>
#include <thread>
#include <ixwebsocket/IXNetSystem.h> #include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h> #include <ixwebsocket/IXWebSocket.h>
class WebSocket { class EventEmitter {
private:
using eventHandlers = std::function<void(const nlohmann::json&)>;
std::unordered_map<std::string, std::list<eventHandlers>> handlers;
protected:
void emit(const std::string& event, const nlohmann::json& data) {
if (handlers.find(event) != handlers.end()) {
for (const auto& handler : handlers[event]) {
handler(data);
}
}
}
public:
void on(const std::string& event, eventHandlers handler) {
handlers[event].emplace_back(handler);
}
void once(const std::string& event, eventHandlers handler) {
auto wrappedHandler = [this, event, handler](const nlohmann::json& data) {
handler(data);
off(event, handler);
};
handlers[event].emplace_back(wrappedHandler);
}
void off(const std::string& event, eventHandlers handler) {
if (handlers.find(event) != handlers.end()) {
auto& vec = handlers[event];
vec.erase(std::remove_if(vec.begin(), vec.end(), [&handler](const eventHandlers& h) {
return h.target<eventHandlers>() == handler.target<eventHandlers>();
}), vec.end());
}
}
};
class WebSocket : public EventEmitter {
private: private:
bool isBot;
int intents;
std::string token; std::string token;
int intents;
bool isBot;
ix::WebSocket webSocket; ix::WebSocket webSocket;
nlohmann::json payload = { {"op", 1},{"d", nullptr} }, id; const nlohmann::json payload = { {"op", 1}, {"d", nullptr} };
std::unordered_map<std::string, std::function<void(const nlohmann::json&)>> eventHandlers;
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;
explicit WebSocket(const std::string& token, const int& intents, const bool& isBot) { WebSocket(const std::string& token = "", const int& intents = 0, const bool& isBot = true) : token(token), intents(intents), isBot(isBot) {
WebSocket::token = token; nlohmann::json id = {
WebSocket::intents = intents;
WebSocket::isBot = isBot;
id = {
{"op", 2}, {"op", 2},
{"d", { {"d", {
{"token", token}, {"token", token},
@ -30,12 +57,12 @@ private:
{"browser", "firefox"}, {"browser", "firefox"},
{"device", "firefox"} {"device", "firefox"}
}}, }},
//{"compress", 1}, {"compress", 1},
{"presence", { {"presence", {
{"activities", nlohmann::json::array({ {"activities", nlohmann::json::array({
{ {
//{"name", "asdsadsadsadsa"}, {"name", "meow"},
//{"type", 2} {"type", 2}
} }
})}, })},
{"status", "idle"}, {"status", "idle"},
@ -46,53 +73,53 @@ 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 = 1000, connected = false](const ix::WebSocketMessagePtr& msg) mutable { webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 0, connected = false, id](const ix::WebSocketMessagePtr& msg) mutable {
if (msg->type == ix::WebSocketMessageType::Message) { switch (msg->type) {
res = nlohmann::json::parse(msg->str); case ix::WebSocketMessageType::Message:
Log::create(INFO, WEBSOCKET, res["op"].dump() + " " + res["t"].dump()); res = nlohmann::json::parse(msg->str);
switch (res["op"].get<int>()) { Log::create(INFO, WEBSOCKET, res["op"].dump() + " " + res["t"].dump());
case 10: switch (res["op"].get<int>()) {
heartbeat_interval = res["d"]["heartbeat_interval"].get<int>(); case 10:
!connected ? connected = true, webSocket.send(id.dump()) : 0; heartbeat_interval = res["d"]["heartbeat_interval"].get<int>();
std::thread([this, &heartbeat_interval, &connected]() { !connected ? connected = true, webSocket.send(id.dump()) : 0;
while (connected && heartbeat_interval != -1) { std::thread([this, &heartbeat_interval, &connected]() {
Log::create(INFO, WEBSOCKET, "Heartbeat " + std::to_string(heartbeat_interval)); while (connected && heartbeat_interval != -1) {
std::this_thread::sleep_for(std::chrono::milliseconds(heartbeat_interval)); Log::create(INFO, WEBSOCKET, "Heartbeat " + std::to_string(heartbeat_interval));
webSocket.send(payload.dump()); std::this_thread::sleep_for(std::chrono::milliseconds(heartbeat_interval));
} webSocket.send(payload.dump());
}).detach(); }
break; }).detach();
case 0: break;
if (eventHandlers.find(res["t"].get<std::string>()) != eventHandlers.end()) { case 0: emit(res["t"].get<std::string>(), res); break;
eventHandlers[res["t"].get<std::string>()](res); }
} break;
break; case ix::WebSocketMessageType::Error:
} std::cout << msg->errorInfo.reason << std::endl;
} else if (msg->type == ix::WebSocketMessageType::Error) { exit(-1);
std::cout << msg->errorInfo.reason << std::endl; break;
exit(-1); default:
break;
} }
}); });
webSocket.start(); webSocket.start();
} }
public: public:
/* [[maybe_unused]]
void sendPresenceUpdate(int statusType, const std::string& activityName) { void sendPresenceUpdate(const int& statusType, const std::string& activityName) {
json prsUpdate = { nlohmann::json prsUpdate = {
{"op", 3}, {"op", 3},
{"d", { {"d", {
{"since", 0}, {"since", 0},
{"activities", json::array({{"name", activityName}, {"type", 2}})}, {"activities", nlohmann::json::array({{"name", activityName}, {"type", 2}})},
{"status", statusType == 1 ? "online" : "idle"}, {"status", statusType == 1 ? "online" : "idle"},
{"afk", false} {"afk", false}
}} }}
}; };
webSocket.send(prsUpdate.dump()); webSocket.send(prsUpdate.dump());
} }
*/ static WebSocket& getInstance(const std::string& token = "", const int& intents = 0, const bool& bot = true) {
static WebSocket& getInstance(const std::string& token = "", const int intents = 0, bool bot = true) {
Log::create(INFO, WEBSOCKET, "Instance event"); Log::create(INFO, WEBSOCKET, "Instance event");
static WebSocket instance(token, intents, bot); static WebSocket instance(token, intents, bot);
return instance; return instance;
@ -107,18 +134,8 @@ public:
int getIntents() const { int getIntents() const {
return WebSocket::intents; return WebSocket::intents;
} }
void on(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
eventHandlers[event] = [handler](const nlohmann::json& message) {
handler(message.get<nlohmann::json>());
};
}
void once(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
eventHandlers[event] = [event, handler, isCalled = false](const nlohmann::json& message) mutable {
isCalled ? handler(message.get<nlohmann::json>()), true : isCalled = true;
};
}
void start() { void start() {
while (1) std::this_thread::sleep_for(std::chrono::milliseconds(100)); while (1) std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
}; };
#endif #endif

View File

@ -10,12 +10,13 @@ private:
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(NetworkManager&&) = delete;
NetworkManager(const NetworkManager&) = delete; NetworkManager(const NetworkManager&) = delete;
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);
curl = curl_easy_init(); if (!(curl = curl_easy_init())) {
if (!curl) {
Log::create(CRITICAL, NETWORK, "Failed to initialize CURL"); Log::create(CRITICAL, NETWORK, "Failed to initialize CURL");
abort(); abort();
} }

View File

@ -3,7 +3,6 @@
#include <includes.hpp> #include <includes.hpp>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
cxxopts::Options options("sparkle", "WIP discord bot library in C++"); cxxopts::Options options("sparkle", "WIP discord bot library in C++");
options.add_options() options.add_options()
("h,help", "Print help") ("h,help", "Print help")
("V,version", "Show version information") ("V,version", "Show version information")
@ -12,10 +11,11 @@ int main(int argc, char* argv[]) {
auto result = options.parse(argc, argv); auto result = options.parse(argc, argv);
if (result.count("version")) { if (result.count("version")) {
std::cout << "sparkle version 0.1" << std::endl; std::cout << "sparkle version 0.3" << std::endl;
return 0; return 0;
} }
if (result.count("help")){
if (result.count("help")) {
std::cout << options.help() << std::endl; std::cout << options.help() << std::endl;
return 0; return 0;
} }
@ -36,10 +36,13 @@ int main(int argc, char* argv[]) {
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(); const auto& [message, user, author] = msg.ctx();
if (!author->isBot) { if (!author->isBot) {
message->send("939957962972229634", message->pack("content", author->avatar)); message->send("939957962972229634", message->pack("content", author->channel_id));
} }
}); });
bot->on(GatewayEvents::MESSAGE_REACTION_ADD, [](const Discord<Message>& msg) {
const auto& [message] = msg.ctx();
message->send("939957962972229634", message->pack("content", "test"));
});
bot->start(); bot->start();
return 0; return 0;
} }