forked from rcxpony/sparkle
Compare commits
No commits in common. "99cf38cf129973e24688344e34c6d7c57ae0a8b7" and "728c65b8b392a305d33a24c7241757ee189dab6f" have entirely different histories.
99cf38cf12
...
728c65b8b3
@ -7,4 +7,5 @@
|
|||||||
#include <utils/functions.hpp>
|
#include <utils/functions.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
#endif
|
#endif
|
@ -1,53 +1,26 @@
|
|||||||
#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 EventEmitter {
|
class WebSocket {
|
||||||
private:
|
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:
|
|
||||||
std::string token;
|
|
||||||
int intents;
|
|
||||||
bool isBot;
|
bool isBot;
|
||||||
|
int intents;
|
||||||
|
std::string token;
|
||||||
ix::WebSocket webSocket;
|
ix::WebSocket webSocket;
|
||||||
const nlohmann::json payload = { {"op", 1}, {"d", nullptr} };
|
nlohmann::json payload = { {"op", 1},{"d", nullptr} }, id;
|
||||||
|
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;
|
||||||
WebSocket(const std::string& token = "", const int& intents = 0, const bool& isBot = true) : token(token), intents(intents), isBot(isBot) {
|
explicit WebSocket(const std::string& token, const int& intents, const bool& isBot) {
|
||||||
nlohmann::json id = {
|
WebSocket::token = token;
|
||||||
|
WebSocket::intents = intents;
|
||||||
|
WebSocket::isBot = isBot;
|
||||||
|
id = {
|
||||||
{"op", 2},
|
{"op", 2},
|
||||||
{"d", {
|
{"d", {
|
||||||
{"token", token},
|
{"token", token},
|
||||||
@ -57,12 +30,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", "meow"},
|
//{"name", "asdsadsadsadsa"},
|
||||||
{"type", 2}
|
//{"type", 2}
|
||||||
}
|
}
|
||||||
})},
|
})},
|
||||||
{"status", "idle"},
|
{"status", "idle"},
|
||||||
@ -73,11 +46,10 @@ 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 = 1000, connected = false](const ix::WebSocketMessagePtr& msg) mutable {
|
||||||
switch (msg->type) {
|
if (msg->type == 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, res["op"].dump() + " " + res["t"].dump());
|
||||||
switch (res["op"].get<int>()) {
|
switch (res["op"].get<int>()) {
|
||||||
@ -92,34 +64,35 @@ private:
|
|||||||
}
|
}
|
||||||
}).detach();
|
}).detach();
|
||||||
break;
|
break;
|
||||||
case 0: emit(res["t"].get<std::string>(), res); break;
|
case 0:
|
||||||
|
if (eventHandlers.find(res["t"].get<std::string>()) != eventHandlers.end()) {
|
||||||
|
eventHandlers[res["t"].get<std::string>()](res);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ix::WebSocketMessageType::Error:
|
}
|
||||||
|
} else if (msg->type == ix::WebSocketMessageType::Error) {
|
||||||
std::cout << msg->errorInfo.reason << std::endl;
|
std::cout << msg->errorInfo.reason << std::endl;
|
||||||
exit(-1);
|
exit(-1);
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
webSocket.start();
|
webSocket.start();
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
[[maybe_unused]]
|
/*
|
||||||
void sendPresenceUpdate(const int& statusType, const std::string& activityName) {
|
void sendPresenceUpdate(int statusType, const std::string& activityName) {
|
||||||
nlohmann::json prsUpdate = {
|
json prsUpdate = {
|
||||||
{"op", 3},
|
{"op", 3},
|
||||||
{"d", {
|
{"d", {
|
||||||
{"since", 0},
|
{"since", 0},
|
||||||
{"activities", nlohmann::json::array({{"name", activityName}, {"type", 2}})},
|
{"activities", 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;
|
||||||
@ -134,8 +107,18 @@ 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(1));
|
while (1) std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
@ -10,13 +10,12 @@ 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);
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#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")
|
||||||
@ -11,11 +12,10 @@ 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.3" << std::endl;
|
std::cout << "sparkle version 0.1" << 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,13 +36,10 @@ 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->channel_id));
|
message->send("939957962972229634", message->pack("content", author->avatar));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
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;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user