This commit is contained in:
fluttershy 2024-12-31 04:46:39 +05:00
parent a96b289074
commit 4249733fe9
9 changed files with 37 additions and 22 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
build/*
build/

View File

@ -7,4 +7,5 @@ git clone https://\[226:6bac:39d0:90bd:f480:fb4d:fadf:2045\]:8443/fluttershy/spa
mkdir sparkle/build
cd sparkle/build
cmake .. && make
./sparkle -t <BOT_TOKEN> -i <BOT_INTENTS>
```

View File

@ -17,7 +17,7 @@ private:
WebSocket& web;
NetworkManager& req;
public:
Channel(const json& data) : data(data), web(*WebSocket::getInstance(TOKEN_BOT, ALL_INTENTS)), req(*NetworkManager::getInstance()) {}
Channel(const json& data) : data(data), web(*WebSocket::getInstance()), req(*NetworkManager::getInstance()) {}
string send(const json& msg) const {
return req.request("POST", dapi + "/channels/" + data["d"]["channel_id"].get<string>() + "/messages", web.getToken(), msg.dump());
}

View File

@ -17,7 +17,7 @@ private:
WebSocket& web;
NetworkManager& req;
public:
Message(const json& data) : data(data), web(*WebSocket::getInstance(TOKEN_BOT, ALL_INTENTS)), req(*NetworkManager::getInstance()) {};
Message(const json& data) : data(data), web(*WebSocket::getInstance()), req(*NetworkManager::getInstance()) {};
string send(const string& id, const json& msg) {
return req.request("POST", dapi + "/channels/" + id + "/messages", web.getToken(), msg.dump());
}

View File

@ -17,7 +17,7 @@ private:
WebSocket& web;
NetworkManager& req;
public:
User(const json& data) : data(data), web(*WebSocket::getInstance(TOKEN_BOT, ALL_INTENTS)), req(*NetworkManager::getInstance()) {}
User(const json& data) : data(data), web(*WebSocket::getInstance()), req(*NetworkManager::getInstance()) {}
string id() {
return data["d"]["author"]["id"];
}

View File

@ -93,11 +93,13 @@ private:
{WEBHOOKS_UPDATE, "WEBHOOKS_UPDATE"}
};
bool connected = false;
static bool initialized, isBot;
static WebSocket* instance;
static int i;
ix::WebSocket webSocket;
int heartbeat_interval, lastS, i;
int heartbeat_interval, lastS;
json payload = { {"op", 1},{"d", nullptr} }, id, res;
std::string t;
static std::string t;
std::unordered_map<std::string, std::function<void(const json&)>> eventHandlers;
void test() {
std::thread([this]() {
@ -112,7 +114,10 @@ private:
}
}).detach();
}
WebSocket(const std::string& token, const int intents) : t(token), i(intents), lastS(0), heartbeat_interval(-1) {
WebSocket(const std::string& token, const int intents, bool isBot) : lastS(0), heartbeat_interval(-1) {
WebSocket::t = token;
WebSocket::i = intents;
WebSocket::isBot = isBot;
id = {
{"op", 2},
{"d", {
@ -144,7 +149,7 @@ private:
if (msg->type == WebSocketMessageType::Message) {
res = json::parse(msg->str);
res["s"].is_number() ? lastS = res["s"] : false;
chngcol(std::string("WEBSOCKET: " + std::to_string(lastS) + " " + res["op"].dump() + " " + res["t"].dump() + " " + res["d"]["content"].dump()), 0, 0, 120);
chngcol(std::string("WEBSOCKET: " + std::to_string(lastS) + " " + res["op"].dump() + " " + res["t"].dump()), 0, 0, 120);
switch (res["op"].get<int>()) {
case 10:
heartbeat_interval = res["d"]["heartbeat_interval"].get<int>();
@ -163,9 +168,13 @@ private:
webSocket.start();
}
public:
static WebSocket* getInstance(const std::string& token = "", const int intents = 0) {
static WebSocket* getInstance(const std::string& token = "", const int intents = 0, bool bot = true) {
if (!instance) {
instance = new WebSocket(token, intents);
if (token.empty() || intents == 0) {
throw std::invalid_argument("Token and intents is empty.");
}
instance = new WebSocket(token, intents, bot);
initialized = true;
}
return instance;
}
@ -174,10 +183,10 @@ public:
ix::uninitNetSystem();
}
std::string getToken() const {
return t;
return isBot ? std::string("Bot " + WebSocket::t) : WebSocket::t;
}
int getIntents() const {
return i;
return WebSocket::i;
}
void on(const int event, std::function<void(const json&)> handler) {
eventHandlers[events[event]] = [handler](const json& message) {
@ -192,9 +201,6 @@ public:
const json& eventData = message.get<json>();
handler(eventData);
}
else {
eventHandlers.erase(events[event]);
}
};
}
void start_heartbeat() {
@ -213,4 +219,8 @@ public:
}
};
WebSocket* WebSocket::instance = nullptr;
std::string WebSocket::t = "";
int WebSocket::i = 0;
bool WebSocket::initialized = false;
bool WebSocket::isBot = true;
#endif

View File

@ -85,7 +85,11 @@ public:
request = method + " " + path + " HTTP/1.1\r\nHost: discord.com\r\nAccept: application/json\r\nContent-Type: application/json\r\n";
request += "Authorization: " + authorization + "\r\n";
request += "Content-Length: " + std::to_string(data.length()) + "\r\n";
#ifdef DEBUG
request += "Connection: close\r\n\r\n";
#elif defined(RELEASE)
request += "Connection: keep-alive\r\n\r\n";
#endif
request += data;
if (SSL_write(ssl.get(), request.c_str(), request.length()) <= 0) {
cerr << "Failed to send request" << endl;
@ -93,6 +97,7 @@ public:
}
chngcol("REQUEST: " + path, 192, 255, 0);
#ifdef DEBUG
cout << request << endl;
char buffer[4096];
while ((bytesRead = SSL_read(ssl.get(), buffer, sizeof(buffer))) > 0) {
result.append(buffer);

View File

@ -5,7 +5,6 @@
#define je(...) {__VA_ARGS__}
#define g(x, y) (std::get<x>(y))
#define ALL_INTENTS 131071
#define TOKEN_BOT "Bot "
#include <iostream>
void chngcol(const std::string& text, int rBg, int gBg, int bBg) {
std::cout << "\033[48;2;" << rBg << ";" << gBg << ";" << bBg << "m";

View File

@ -1,6 +1,5 @@
#define RELEASE
#include <utils/json.hpp>
#include <random>
#include <utils/enums.hpp>
#include <tls/network.hpp>
#include <gateway/websocket.hpp>
@ -12,20 +11,21 @@
#include <api/Author.hpp>
#include <utils/types.hpp>
using namespace std;
int main() {
WebSocket* bot = WebSocket::getInstance(TOKEN_BOT, ALL_INTENTS);
int main(int argc, char* argv[]) {
if (argc != 5) return -1;
WebSocket* bot = WebSocket::getInstance(argv[2], stoi(argv[4]), true);
bot->once(GatewayEvents::READY, [](const Bot<json>& b) {
cout << "started" << endl;
});
bot->on(GatewayEvents::MESSAGE_CREATE, [bot](const Bot<json, Message, Author, EmbedBuilder>& msg) {
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Bot<json, Message, Author, EmbedBuilder>& msg) {
if (g(2, msg.net)->isBot() == false) {
cout << g(1, msg.net)->send("939957962972229634", j("content", "test")) << endl;
}
});
bot->on(GatewayEvents::GUILD_MEMBER_REMOVE, [](const Bot<Message>& msg) {
bot->once(GatewayEvents::MESSAGE_REACTION_REMOVE, [bot](const Bot<Message>& msg) {
g(0, msg.net)->send("939957962972229634", j("content", "bye"));
});
bot->on(GatewayEvents::GUILD_MEMBER_ADD, [](const Bot<Message>& msg) {
bot->on(GatewayEvents::MESSAGE_REACTION_ADD, [](const Bot<Message>& msg) {
g(0, msg.net)->send("939957962972229634", j("content", "hello"));
});
bot->start();