fixes
This commit is contained in:
parent
a96b289074
commit
4249733fe9
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1 @@
|
|||||||
build/*
|
build/
|
||||||
|
@ -7,4 +7,5 @@ git clone https://\[226:6bac:39d0:90bd:f480:fb4d:fadf:2045\]:8443/fluttershy/spa
|
|||||||
mkdir sparkle/build
|
mkdir sparkle/build
|
||||||
cd sparkle/build
|
cd sparkle/build
|
||||||
cmake .. && make
|
cmake .. && make
|
||||||
|
./sparkle -t <BOT_TOKEN> -i <BOT_INTENTS>
|
||||||
```
|
```
|
||||||
|
@ -17,7 +17,7 @@ private:
|
|||||||
WebSocket& web;
|
WebSocket& web;
|
||||||
NetworkManager& req;
|
NetworkManager& req;
|
||||||
public:
|
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 {
|
string send(const json& msg) const {
|
||||||
return req.request("POST", dapi + "/channels/" + data["d"]["channel_id"].get<string>() + "/messages", web.getToken(), msg.dump());
|
return req.request("POST", dapi + "/channels/" + data["d"]["channel_id"].get<string>() + "/messages", web.getToken(), msg.dump());
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ private:
|
|||||||
WebSocket& web;
|
WebSocket& web;
|
||||||
NetworkManager& req;
|
NetworkManager& req;
|
||||||
public:
|
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) {
|
string send(const string& id, const json& msg) {
|
||||||
return req.request("POST", dapi + "/channels/" + id + "/messages", web.getToken(), msg.dump());
|
return req.request("POST", dapi + "/channels/" + id + "/messages", web.getToken(), msg.dump());
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ private:
|
|||||||
WebSocket& web;
|
WebSocket& web;
|
||||||
NetworkManager& req;
|
NetworkManager& req;
|
||||||
public:
|
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() {
|
string id() {
|
||||||
return data["d"]["author"]["id"];
|
return data["d"]["author"]["id"];
|
||||||
}
|
}
|
||||||
|
@ -93,11 +93,13 @@ private:
|
|||||||
{WEBHOOKS_UPDATE, "WEBHOOKS_UPDATE"}
|
{WEBHOOKS_UPDATE, "WEBHOOKS_UPDATE"}
|
||||||
};
|
};
|
||||||
bool connected = false;
|
bool connected = false;
|
||||||
|
static bool initialized, isBot;
|
||||||
static WebSocket* instance;
|
static WebSocket* instance;
|
||||||
|
static int i;
|
||||||
ix::WebSocket webSocket;
|
ix::WebSocket webSocket;
|
||||||
int heartbeat_interval, lastS, i;
|
int heartbeat_interval, lastS;
|
||||||
json payload = { {"op", 1},{"d", nullptr} }, id, res;
|
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;
|
std::unordered_map<std::string, std::function<void(const json&)>> eventHandlers;
|
||||||
void test() {
|
void test() {
|
||||||
std::thread([this]() {
|
std::thread([this]() {
|
||||||
@ -112,7 +114,10 @@ private:
|
|||||||
}
|
}
|
||||||
}).detach();
|
}).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 = {
|
id = {
|
||||||
{"op", 2},
|
{"op", 2},
|
||||||
{"d", {
|
{"d", {
|
||||||
@ -144,7 +149,7 @@ private:
|
|||||||
if (msg->type == WebSocketMessageType::Message) {
|
if (msg->type == WebSocketMessageType::Message) {
|
||||||
res = json::parse(msg->str);
|
res = json::parse(msg->str);
|
||||||
res["s"].is_number() ? lastS = res["s"] : false;
|
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>()) {
|
switch (res["op"].get<int>()) {
|
||||||
case 10:
|
case 10:
|
||||||
heartbeat_interval = res["d"]["heartbeat_interval"].get<int>();
|
heartbeat_interval = res["d"]["heartbeat_interval"].get<int>();
|
||||||
@ -163,9 +168,13 @@ private:
|
|||||||
webSocket.start();
|
webSocket.start();
|
||||||
}
|
}
|
||||||
public:
|
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) {
|
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;
|
return instance;
|
||||||
}
|
}
|
||||||
@ -174,10 +183,10 @@ public:
|
|||||||
ix::uninitNetSystem();
|
ix::uninitNetSystem();
|
||||||
}
|
}
|
||||||
std::string getToken() const {
|
std::string getToken() const {
|
||||||
return t;
|
return isBot ? std::string("Bot " + WebSocket::t) : WebSocket::t;
|
||||||
}
|
}
|
||||||
int getIntents() const {
|
int getIntents() const {
|
||||||
return i;
|
return WebSocket::i;
|
||||||
}
|
}
|
||||||
void on(const int event, std::function<void(const json&)> handler) {
|
void on(const int event, std::function<void(const json&)> handler) {
|
||||||
eventHandlers[events[event]] = [handler](const json& message) {
|
eventHandlers[events[event]] = [handler](const json& message) {
|
||||||
@ -192,9 +201,6 @@ public:
|
|||||||
const json& eventData = message.get<json>();
|
const json& eventData = message.get<json>();
|
||||||
handler(eventData);
|
handler(eventData);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
eventHandlers.erase(events[event]);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
void start_heartbeat() {
|
void start_heartbeat() {
|
||||||
@ -213,4 +219,8 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
WebSocket* WebSocket::instance = nullptr;
|
WebSocket* WebSocket::instance = nullptr;
|
||||||
|
std::string WebSocket::t = "";
|
||||||
|
int WebSocket::i = 0;
|
||||||
|
bool WebSocket::initialized = false;
|
||||||
|
bool WebSocket::isBot = true;
|
||||||
#endif
|
#endif
|
@ -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 = 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 += "Authorization: " + authorization + "\r\n";
|
||||||
request += "Content-Length: " + std::to_string(data.length()) + "\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";
|
request += "Connection: keep-alive\r\n\r\n";
|
||||||
|
#endif
|
||||||
request += data;
|
request += data;
|
||||||
if (SSL_write(ssl.get(), request.c_str(), request.length()) <= 0) {
|
if (SSL_write(ssl.get(), request.c_str(), request.length()) <= 0) {
|
||||||
cerr << "Failed to send request" << endl;
|
cerr << "Failed to send request" << endl;
|
||||||
@ -93,6 +97,7 @@ public:
|
|||||||
}
|
}
|
||||||
chngcol("REQUEST: " + path, 192, 255, 0);
|
chngcol("REQUEST: " + path, 192, 255, 0);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
cout << request << endl;
|
||||||
char buffer[4096];
|
char buffer[4096];
|
||||||
while ((bytesRead = SSL_read(ssl.get(), buffer, sizeof(buffer))) > 0) {
|
while ((bytesRead = SSL_read(ssl.get(), buffer, sizeof(buffer))) > 0) {
|
||||||
result.append(buffer);
|
result.append(buffer);
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
#define je(...) {__VA_ARGS__}
|
#define je(...) {__VA_ARGS__}
|
||||||
#define g(x, y) (std::get<x>(y))
|
#define g(x, y) (std::get<x>(y))
|
||||||
#define ALL_INTENTS 131071
|
#define ALL_INTENTS 131071
|
||||||
#define TOKEN_BOT "Bot "
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
void chngcol(const std::string& text, int rBg, int gBg, int bBg) {
|
void chngcol(const std::string& text, int rBg, int gBg, int bBg) {
|
||||||
std::cout << "\033[48;2;" << rBg << ";" << gBg << ";" << bBg << "m";
|
std::cout << "\033[48;2;" << rBg << ";" << gBg << ";" << bBg << "m";
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#define RELEASE
|
#define RELEASE
|
||||||
#include <utils/json.hpp>
|
#include <utils/json.hpp>
|
||||||
#include <random>
|
|
||||||
#include <utils/enums.hpp>
|
#include <utils/enums.hpp>
|
||||||
#include <tls/network.hpp>
|
#include <tls/network.hpp>
|
||||||
#include <gateway/websocket.hpp>
|
#include <gateway/websocket.hpp>
|
||||||
@ -12,20 +11,21 @@
|
|||||||
#include <api/Author.hpp>
|
#include <api/Author.hpp>
|
||||||
#include <utils/types.hpp>
|
#include <utils/types.hpp>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
int main() {
|
int main(int argc, char* argv[]) {
|
||||||
WebSocket* bot = WebSocket::getInstance(TOKEN_BOT, ALL_INTENTS);
|
if (argc != 5) return -1;
|
||||||
|
WebSocket* bot = WebSocket::getInstance(argv[2], stoi(argv[4]), true);
|
||||||
bot->once(GatewayEvents::READY, [](const Bot<json>& b) {
|
bot->once(GatewayEvents::READY, [](const Bot<json>& b) {
|
||||||
cout << "started" << endl;
|
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) {
|
if (g(2, msg.net)->isBot() == false) {
|
||||||
cout << g(1, msg.net)->send("939957962972229634", j("content", "test")) << endl;
|
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"));
|
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"));
|
g(0, msg.net)->send("939957962972229634", j("content", "hello"));
|
||||||
});
|
});
|
||||||
bot->start();
|
bot->start();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user