some fixes
This commit is contained in:
parent
4249733fe9
commit
41d1bd5268
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
build/
|
build/
|
||||||
|
.vscode/
|
76
.vscode/settings.json
vendored
76
.vscode/settings.json
vendored
@ -1,76 +0,0 @@
|
|||||||
{
|
|
||||||
"files.associations": {
|
|
||||||
"any": "cpp",
|
|
||||||
"array": "cpp",
|
|
||||||
"atomic": "cpp",
|
|
||||||
"bit": "cpp",
|
|
||||||
"*.tcc": "cpp",
|
|
||||||
"bitset": "cpp",
|
|
||||||
"cctype": "cpp",
|
|
||||||
"charconv": "cpp",
|
|
||||||
"chrono": "cpp",
|
|
||||||
"clocale": "cpp",
|
|
||||||
"cmath": "cpp",
|
|
||||||
"codecvt": "cpp",
|
|
||||||
"compare": "cpp",
|
|
||||||
"concepts": "cpp",
|
|
||||||
"condition_variable": "cpp",
|
|
||||||
"cstdarg": "cpp",
|
|
||||||
"cstddef": "cpp",
|
|
||||||
"cstdint": "cpp",
|
|
||||||
"cstdio": "cpp",
|
|
||||||
"cstdlib": "cpp",
|
|
||||||
"cstring": "cpp",
|
|
||||||
"ctime": "cpp",
|
|
||||||
"cwchar": "cpp",
|
|
||||||
"cwctype": "cpp",
|
|
||||||
"deque": "cpp",
|
|
||||||
"forward_list": "cpp",
|
|
||||||
"list": "cpp",
|
|
||||||
"map": "cpp",
|
|
||||||
"set": "cpp",
|
|
||||||
"string": "cpp",
|
|
||||||
"unordered_map": "cpp",
|
|
||||||
"unordered_set": "cpp",
|
|
||||||
"vector": "cpp",
|
|
||||||
"exception": "cpp",
|
|
||||||
"algorithm": "cpp",
|
|
||||||
"functional": "cpp",
|
|
||||||
"iterator": "cpp",
|
|
||||||
"memory": "cpp",
|
|
||||||
"memory_resource": "cpp",
|
|
||||||
"numeric": "cpp",
|
|
||||||
"optional": "cpp",
|
|
||||||
"random": "cpp",
|
|
||||||
"ratio": "cpp",
|
|
||||||
"string_view": "cpp",
|
|
||||||
"system_error": "cpp",
|
|
||||||
"tuple": "cpp",
|
|
||||||
"type_traits": "cpp",
|
|
||||||
"utility": "cpp",
|
|
||||||
"format": "cpp",
|
|
||||||
"initializer_list": "cpp",
|
|
||||||
"iomanip": "cpp",
|
|
||||||
"iosfwd": "cpp",
|
|
||||||
"iostream": "cpp",
|
|
||||||
"istream": "cpp",
|
|
||||||
"limits": "cpp",
|
|
||||||
"mutex": "cpp",
|
|
||||||
"new": "cpp",
|
|
||||||
"numbers": "cpp",
|
|
||||||
"ostream": "cpp",
|
|
||||||
"ranges": "cpp",
|
|
||||||
"semaphore": "cpp",
|
|
||||||
"span": "cpp",
|
|
||||||
"sstream": "cpp",
|
|
||||||
"stdexcept": "cpp",
|
|
||||||
"stop_token": "cpp",
|
|
||||||
"streambuf": "cpp",
|
|
||||||
"text_encoding": "cpp",
|
|
||||||
"thread": "cpp",
|
|
||||||
"cinttypes": "cpp",
|
|
||||||
"typeinfo": "cpp",
|
|
||||||
"valarray": "cpp",
|
|
||||||
"variant": "cpp"
|
|
||||||
}
|
|
||||||
}
|
|
@ -19,7 +19,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
Channel(const json& data) : data(data), web(*WebSocket::getInstance()), 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", msg.dump());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
@ -19,7 +19,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
Message(const json& data) : data(data), web(*WebSocket::getInstance()), 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", msg.dump());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
@ -22,7 +22,7 @@ public:
|
|||||||
return data["d"]["author"]["id"];
|
return data["d"]["author"]["id"];
|
||||||
}
|
}
|
||||||
string me() const {
|
string me() const {
|
||||||
return req.request("GET", dapi + "/users/@me", web.getToken(), "");
|
return req.request("GET", dapi + "/users/@me", "");
|
||||||
}
|
}
|
||||||
bool isBot() {
|
bool isBot() {
|
||||||
try {
|
try {
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#define GATEWAY_WEBSOCKET_HPP_
|
#define GATEWAY_WEBSOCKET_HPP_
|
||||||
#include <utils/enums.hpp>
|
#include <utils/enums.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
|
||||||
#include <utils/json.hpp>
|
#include <utils/json.hpp>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -17,7 +16,7 @@ using namespace std::chrono;
|
|||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
class WebSocket {
|
class WebSocket {
|
||||||
private:
|
private:
|
||||||
std::unordered_map<int, std::string> events = {
|
std::array<std::pair<int, std::string_view>, 73> events = {{
|
||||||
{APPLICATION_COMMAND_PERMISSIONS_UPDATE, "APPLICATION_COMMAND_PERMISSIONS_UPDATE"},
|
{APPLICATION_COMMAND_PERMISSIONS_UPDATE, "APPLICATION_COMMAND_PERMISSIONS_UPDATE"},
|
||||||
{AUTO_MODERATION_ACTION_EXECUTION, "AUTO_MODERATION_ACTION_EXECUTION"},
|
{AUTO_MODERATION_ACTION_EXECUTION, "AUTO_MODERATION_ACTION_EXECUTION"},
|
||||||
{AUTO_MODERATION_RULE_CREATE, "AUTO_MODERATION_RULE_CREATE"},
|
{AUTO_MODERATION_RULE_CREATE, "AUTO_MODERATION_RULE_CREATE"},
|
||||||
@ -91,29 +90,16 @@ private:
|
|||||||
{VOICE_SERVER_UPDATE, "VOICE_SERVER_UPDATE"},
|
{VOICE_SERVER_UPDATE, "VOICE_SERVER_UPDATE"},
|
||||||
{VOICE_STATE_UPDATE, "VOICE_STATE_UPDATE"},
|
{VOICE_STATE_UPDATE, "VOICE_STATE_UPDATE"},
|
||||||
{WEBHOOKS_UPDATE, "WEBHOOKS_UPDATE"}
|
{WEBHOOKS_UPDATE, "WEBHOOKS_UPDATE"}
|
||||||
};
|
}};
|
||||||
bool connected = false;
|
bool connected = false;
|
||||||
static bool initialized, isBot;
|
static bool initialized, isBot;
|
||||||
static WebSocket* instance;
|
static WebSocket* instance;
|
||||||
static int i;
|
static int i;
|
||||||
|
static std::string t;
|
||||||
ix::WebSocket webSocket;
|
ix::WebSocket webSocket;
|
||||||
int heartbeat_interval, lastS;
|
int heartbeat_interval, lastS;
|
||||||
json payload = { {"op", 1},{"d", nullptr} }, id, res;
|
json payload = { {"op", 1},{"d", nullptr} }, id, res;
|
||||||
static std::string t;
|
std::unordered_map<std::string_view, std::function<void(const json&)>> eventHandlers;
|
||||||
std::unordered_map<std::string, std::function<void(const json&)>> eventHandlers;
|
|
||||||
void test() {
|
|
||||||
std::thread([this]() {
|
|
||||||
while (1) {
|
|
||||||
json prsUpdate = { {"op", 3},{"d", {{"since", 9187921},{"activities", json::array({{"name", "dsa"}, {"type", 1}})},{"status", "online"},{"afk", false}}} };
|
|
||||||
//json voice = { {"op", 4},{"d", {{"guild_id", "1322653635875967140"},{"channel_id", nullptr},{"self_mute", false},{"self_deaf", false}}} };
|
|
||||||
webSocket.send(prsUpdate.dump());
|
|
||||||
std::this_thread::sleep_for(10000ms);
|
|
||||||
json prsUpdate2 = { {"op", 3},{"d", {{"since", 9187921},{"activities", json::array({{"name", "asd"}, {"type", 1}})},{"status", "idle"},{"afk", false}}} };
|
|
||||||
webSocket.send(prsUpdate2.dump());
|
|
||||||
std::this_thread::sleep_for(10000ms);
|
|
||||||
}
|
|
||||||
}).detach();
|
|
||||||
}
|
|
||||||
WebSocket(const std::string& token, const int intents, bool isBot) : lastS(0), heartbeat_interval(-1) {
|
WebSocket(const std::string& token, const int intents, bool isBot) : lastS(0), heartbeat_interval(-1) {
|
||||||
WebSocket::t = token;
|
WebSocket::t = token;
|
||||||
WebSocket::i = intents;
|
WebSocket::i = intents;
|
||||||
@ -168,6 +154,18 @@ private:
|
|||||||
webSocket.start();
|
webSocket.start();
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
|
void sendPresenceUpdate(int statusType, const std::string& activityName) {
|
||||||
|
json prsUpdate = {
|
||||||
|
{"op", 3},
|
||||||
|
{"d", {
|
||||||
|
{"since", 9187921},
|
||||||
|
{"activities", json::array({{"name", activityName}, {"type", statusType}})},
|
||||||
|
{"status", statusType == 1 ? "online" : "idle"},
|
||||||
|
{"afk", false}
|
||||||
|
}}
|
||||||
|
};
|
||||||
|
webSocket.send(prsUpdate.dump());
|
||||||
|
}
|
||||||
static WebSocket* getInstance(const std::string& token = "", const int intents = 0, bool bot = true) {
|
static WebSocket* getInstance(const std::string& token = "", const int intents = 0, bool bot = true) {
|
||||||
if (!instance) {
|
if (!instance) {
|
||||||
if (token.empty() || intents == 0) {
|
if (token.empty() || intents == 0) {
|
||||||
@ -189,13 +187,13 @@ public:
|
|||||||
return WebSocket::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].second] = [handler](const json& message) {
|
||||||
json eventData = message.get<json>();
|
json eventData = message.get<json>();
|
||||||
handler(eventData);
|
handler(eventData);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
void once(const int event, std::function<void(const json&)> handler) {
|
void once(const int event, std::function<void(const json&)> handler) {
|
||||||
eventHandlers[events[event]] = [event, this, handler, isCalled = false](const json& message) mutable {
|
eventHandlers[events[event].second] = [event, this, handler, isCalled = false](const json& message) mutable {
|
||||||
if (isCalled == false) {
|
if (isCalled == false) {
|
||||||
isCalled = true;
|
isCalled = true;
|
||||||
const json& eventData = message.get<json>();
|
const json& eventData = message.get<json>();
|
||||||
@ -212,7 +210,7 @@ public:
|
|||||||
}).detach();
|
}).detach();
|
||||||
}
|
}
|
||||||
void stop() {
|
void stop() {
|
||||||
while (1) std::this_thread::sleep_for(1s);
|
connected = false;
|
||||||
}
|
}
|
||||||
void start() {
|
void start() {
|
||||||
while (1) std::this_thread::sleep_for(1ms);
|
while (1) std::this_thread::sleep_for(1ms);
|
||||||
|
@ -9,11 +9,13 @@
|
|||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <utils/types.hpp>
|
#include <utils/types.hpp>
|
||||||
|
#include <gateway/websocket.hpp>
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
class NetworkManager {
|
class NetworkManager {
|
||||||
private:
|
private:
|
||||||
|
WebSocket& web;
|
||||||
static NetworkManager* instance;
|
static NetworkManager* instance;
|
||||||
SSL_CTX* ctx;
|
SSL_CTX* ctx;
|
||||||
int sock = -1;
|
int sock = -1;
|
||||||
@ -55,7 +57,7 @@ private:
|
|||||||
}
|
}
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
}
|
}
|
||||||
NetworkManager() : ctx(nullptr), result("") {
|
NetworkManager() : ctx(nullptr), result(""), web(*WebSocket::getInstance()) {
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
ctx = SSL_CTX_new(TLS_client_method());
|
ctx = SSL_CTX_new(TLS_client_method());
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
@ -80,10 +82,10 @@ public:
|
|||||||
if (ctx) SSL_CTX_free(ctx);
|
if (ctx) SSL_CTX_free(ctx);
|
||||||
close(sock);
|
close(sock);
|
||||||
}
|
}
|
||||||
const std::string& request(const std::string& method, const std::string& path, const std::string& authorization, const std::string& data) {
|
const std::string& request(const std::string& method, const std::string& path, const std::string& data) {
|
||||||
auto net = [this, method, path, authorization, data, bytesRead = 0, request = std::string("")]() mutable -> void {
|
auto net = [this, method, path, data, bytesRead = 0, request = std::string("")]() mutable -> void {
|
||||||
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: " + web.getToken() + "\r\n";
|
||||||
request += "Content-Length: " + std::to_string(data.length()) + "\r\n";
|
request += "Content-Length: " + std::to_string(data.length()) + "\r\n";
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
request += "Connection: close\r\n\r\n";
|
request += "Connection: close\r\n\r\n";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user