Compare commits
3 Commits
004327b4c2
...
e9efb4b12c
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e9efb4b12c | ||
![]() |
8b35de51fc | ||
![]() |
d113d303a2 |
@ -183,12 +183,12 @@ public:
|
|||||||
int getIntents() const {
|
int getIntents() const {
|
||||||
return WebSocket::intents;
|
return WebSocket::intents;
|
||||||
}
|
}
|
||||||
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].second] = [handler](const json& message) {
|
eventHandlers[events[event].second] = [handler](const json& message) {
|
||||||
handler(message.get<json>());
|
handler(message.get<json>());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
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].second] = [event, handler, isCalled = false](const json& message) mutable {
|
eventHandlers[events[event].second] = [event, handler, isCalled = false](const json& message) mutable {
|
||||||
isCalled == false ? isCalled = true : 0, handler(message.get<json>());
|
isCalled == false ? isCalled = true : 0, handler(message.get<json>());
|
||||||
};
|
};
|
||||||
|
@ -68,31 +68,22 @@ private:
|
|||||||
createSSLConnection("discord.com", "443");
|
createSSLConnection("discord.com", "443");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string parseJson(const std::string& response) {
|
std::string extract(const std::string& httpResponse) {
|
||||||
unsigned long jsonStart = response.find("\r\n\r\n");
|
unsigned long pos = httpResponse.find("\r\n\r\n");
|
||||||
if (jsonStart == std::string::npos) {
|
if (pos != std::string::npos) {
|
||||||
return "";
|
std::string json, chunkSizeStr;
|
||||||
}
|
std::istringstream stream(httpResponse.substr(pos + 4));
|
||||||
jsonStart += 4;
|
while (std::getline(stream, chunkSizeStr)) {
|
||||||
std::string jsonString = response.substr(jsonStart);
|
unsigned long chunkSize = std::stoul(chunkSizeStr, nullptr, 16);
|
||||||
unsigned long jsonBegin = jsonString.find('['), jsonEnd = jsonString.rfind(']');
|
if (chunkSize == 0) break;
|
||||||
if (jsonBegin == std::string::npos || jsonEnd == std::string::npos || jsonEnd < jsonBegin) {
|
std::string chunk(chunkSize, '\0');
|
||||||
return "";
|
stream.read(&chunk[0], chunkSize);
|
||||||
}
|
json.append(chunk);
|
||||||
jsonString = jsonString.substr(jsonBegin, jsonEnd - jsonBegin + 1);
|
stream.ignore(2);
|
||||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\r'), jsonString.end());
|
}
|
||||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\t'), jsonString.end());
|
return json;
|
||||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\n'), jsonString.end());
|
|
||||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\\'), jsonString.end());
|
|
||||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\''), jsonString.end());
|
|
||||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), ' '), jsonString.end());
|
|
||||||
try {
|
|
||||||
nlohmann::json jsonData = nlohmann::json::parse(jsonString);
|
|
||||||
return jsonData.dump();
|
|
||||||
}
|
|
||||||
catch (const nlohmann::json::parse_error& e) {
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
static NetworkManager& getInstance() {
|
static NetworkManager& getInstance() {
|
||||||
@ -104,37 +95,38 @@ public:
|
|||||||
if (ctx) SSL_CTX_free(ctx);
|
if (ctx) SSL_CTX_free(ctx);
|
||||||
close(sock);
|
close(sock);
|
||||||
}
|
}
|
||||||
std::string request(const std::string& method, const std::string& path, const std::string& data = "") {
|
std::string request(const std::string& method, const std::string& path, const std::string& data = "", bool closeConnection = true) {
|
||||||
#ifdef DEBUG
|
if (closeConnection) createSSLConnection("discord.com", "443");
|
||||||
createSSLConnection("discord.com", "443");
|
auto net = [this, method, path, data, closeConnection, result = std::string("")]() mutable -> std::string {
|
||||||
#endif
|
std::string request = method + " " + path + " HTTP/1.1\r\n";
|
||||||
auto net = [this, method, path, data, result = std::string("")]() mutable -> std::string {
|
request += "Host: discord.com\r\n";
|
||||||
std::string request = method + " " + path + " HTTP/1.1\r\nHost: discord.com\r\nAccept: application/json\r\nContent-Type: application/json\r\n";
|
request += "Accept: application/json\r\n";
|
||||||
request += "Authorization: " + web.getToken() + "\r\nContent-Length: " + std::to_string(data.length()) + "\r\n";
|
request += "Content-Type: application/json\r\n";
|
||||||
#ifdef DEBUG
|
request += "Authorization: " + web.getToken() + "\r\n";
|
||||||
request += "Connection: close\r\n\r\n";
|
request += "Content-Length: " + std::to_string(data.length()) + "\r\n";
|
||||||
#elif defined(RELEASE)
|
if (closeConnection) {
|
||||||
request += "Connection: keep-alive\r\n\r\n";
|
request += "Connection: close\r\n\r\n";
|
||||||
#endif
|
} else {
|
||||||
|
request += "Connection: keep-alive\r\n\r\n";
|
||||||
|
}
|
||||||
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) {
|
||||||
Logs::create(ERROR, NETWORK, "Failed to send request");
|
Logs::create(ERROR, NETWORK, "Failed to send request");
|
||||||
handleSSLInitErrors();
|
handleSSLInitErrors();
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
Logs::create(INFO, NETWORK, "Request " + method + " " + path);
|
Logs::create(INFO, NETWORK, "Request " + method + " " + path);
|
||||||
#ifdef DEBUG
|
if (closeConnection) {
|
||||||
std::vector<char> buffer(1024);
|
std::vector<char> buffer(1024);
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
while ((bytesRead = SSL_read(ssl.get(), buffer.data(), buffer.size())) > 0) {
|
while ((bytesRead = SSL_read(ssl.get(), buffer.data(), buffer.size())) > 0) {
|
||||||
result.append(buffer.data(), bytesRead);
|
result.append(buffer.data(), bytesRead);
|
||||||
if (bytesRead == buffer.size()) {
|
if (bytesRead == buffer.size()) {
|
||||||
buffer.resize(buffer.size() * 2);
|
buffer.resize(buffer.size() * 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return parseJson(result);
|
return extract(result);
|
||||||
#elif defined(RELEASE)
|
|
||||||
return result;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
return net();
|
return net();
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,8 @@ enum GatewayIntents {
|
|||||||
GuildScheduledEvents = 65536,
|
GuildScheduledEvents = 65536,
|
||||||
GuildVoiceStates = 128,
|
GuildVoiceStates = 128,
|
||||||
GuildWebhooks = 32,
|
GuildWebhooks = 32,
|
||||||
MessageContent = 32768
|
MessageContent = 32768,
|
||||||
|
AllIntents = 131071
|
||||||
};
|
};
|
||||||
enum UserStatus {
|
enum UserStatus {
|
||||||
Offline,
|
Offline,
|
||||||
|
@ -11,6 +11,7 @@ enum type { WEBSOCKET, NETWORK };
|
|||||||
class Logs {
|
class Logs {
|
||||||
public:
|
public:
|
||||||
static void create(level lvl, type t, const std::string& message) {
|
static void create(level lvl, type t, const std::string& message) {
|
||||||
|
#ifdef DEBUG
|
||||||
std::string color;
|
std::string color;
|
||||||
switch (lvl) {
|
switch (lvl) {
|
||||||
case INFO:
|
case INFO:
|
||||||
@ -27,6 +28,7 @@ public:
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[37m" << message << "\033[0m" << std::endl;
|
std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[37m" << message << "\033[0m" << std::endl;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
static std::string getCurrentTime() {
|
static std::string getCurrentTime() {
|
||||||
|
@ -2,12 +2,17 @@
|
|||||||
#include <includes.h>
|
#include <includes.h>
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (argc != 3) return -1;
|
if (argc != 3) return -1;
|
||||||
WebSocket* bot = &WebSocket::getInstance(argv[2], 131071);
|
WebSocket* bot = &WebSocket::getInstance(argv[2], GatewayIntents::AllIntents);
|
||||||
|
bot->on(GatewayEvents::READY, [](const Bot<Message, User, Author>& msg) {
|
||||||
|
if (!g(2, msg.net)->isBot()) {
|
||||||
|
cout << g(0, msg.net)->send("939957962972229634", j("content", "asd")) << endl;
|
||||||
|
}
|
||||||
|
});
|
||||||
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Bot<Message, User, Author>& msg) {
|
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Bot<Message, User, Author>& msg) {
|
||||||
if (!g(2, msg.net)->isBot()) {
|
if (!g(2, msg.net)->isBot()) {
|
||||||
g(0, msg.net)->send("939957962972229634", j("content", g(2, msg.net)->content()));
|
cout << g(0, msg.net)->send("939957962972229634", j("content", g(2, msg.net)->content())) << endl;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
bot->start();
|
bot->start();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Reference in New Issue
Block a user