cmake flags, some fixes in api

This commit is contained in:
fluttershy 2025-01-25 11:24:56 +05:00
parent 802478eab4
commit 4a230f8491
16 changed files with 173 additions and 172 deletions

View File

@ -31,11 +31,11 @@ endif()
if(CMAKE_BUILD_TYPE)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "-march=native -O0 -pipe")
set(CMAKE_CXX_FLAGS "-march=native -O0 -pipe -Wall -Werror")
add_definitions(-DDEBUG)
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
add_definitions(-DRELEASE)
set(CMAKE_CXX_FLAGS "-march=native -O2 -pipe")
set(CMAKE_CXX_FLAGS "-march=native -O2 -pipe -Wall -Werror")
endif()
endif()
@ -43,15 +43,11 @@ message(STATUS "${CMAKE_BUILD_TYPE}")
add_executable(${PROJECT_NAME} ${SOURCE})
#add_library(sparkles STATIC ${SOURCE})
#add_executable(tests ${TESTS}/tests.cpp)
add_executable(tests ${TESTS}/tests.cpp)
#enable_testing()
enable_testing()
add_test(NAME network COMMAND tests)
#target_include_directories(sparkles PRIVATE ${LIBS} ${INCLUDE} ${IXWEBSOCKET_INCLUDE_DIR} ${CURL_INCLUDE_DIRS})
#target_link_libraries(sparkles PRIVATE ${IXWEBSOCKET_LIBRARIES} ${CURL_LIBRARIES})
add_test(NAME NetworkManagerTest COMMAND tests)
target_include_directories(${PROJECT_NAME} PRIVATE
${LIBS}
@ -65,15 +61,23 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
${CURL_LIBRARIES}
)
#target_include_directories(tests PRIVATE
# ${LIBS}
# ${INCLUDE}
# ${IXWEBSOCKET_INCLUDE_DIR}
# ${CURL_INCLUDE_DIRS}
#)
target_include_directories(tests PRIVATE
${LIBS}
${INCLUDE}
${IXWEBSOCKET_INCLUDE_DIR}
${CURL_INCLUDE_DIRS}
)
<<<<<<< HEAD
#target_link_libraries(tests gtest
# ${IXWEBSOCKET_LIBRARIES}
# ${CURL_LIBRARIES}
# gtest_main
#)
#)
=======
target_link_libraries(tests gtest
${IXWEBSOCKET_LIBRARIES}
${CURL_LIBRARIES}
gtest_main
)
>>>>>>> tests

View File

@ -4,6 +4,7 @@
#include <utils/json.hpp>
#include <utils/log.hpp>
#include <utils/enums.hpp>
#include <utils/functions.hpp>
#include <iostream>
#include <string>
#endif

View File

@ -2,56 +2,32 @@
#define API_AUTHOR_HPP_
#include <includes.h>
#include <net.h>
using std::string, std::cout, std::endl, nlohmann::json;
class Author {
private:
json data;
nlohmann::json data;
const nlohmann::json &d;
WebSocket& web;
NetworkManager& req;
public:
Author(const json& data) : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {}
string content() {
try {
return data["d"]["content"];
}
catch (...) {
return "";
}
}
string channel_id() {
return data["d"]["channel_id"];
}
string id() {
return data["d"]["author"]["id"];
}
string global_name() {
return data["d"]["author"]["global_name"].dump();
}
string discriminator() {
return data["d"]["author"]["discriminator"];
}
bool isBot() {
try {
return data["d"]["author"]["bot"];
}
catch (...) {
return 0;
}
}
string guild_id() {
return data["d"]["guild_id"];
}
string msg_id() {
return data["d"]["id"];
}
bool isPinned() {
return data["d"]["pinned"];
}
string avatar() {
return data["d"]["author"]["avatar"];
}
string send(const json& msg) {
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<string>() + "/messages", msg.dump());
const std::string channel_id, global_name, id, content, avatar, guild_id, discriminator, message_id;
bool isPinned, isBot;
Author(const nlohmann::json& data) :
data(data),
d(data["d"]),
web(WebSocket::getInstance()),
req(NetworkManager::getInstance()),
channel_id(functions::isNull(d["channel_id"])),
global_name(functions::isNull(d["author"]["global_name"])),
id(d["author"]["id"]),
content(d["content"]),
avatar(data["d"]["author"]["avatar"]),
guild_id(data["d"]["guild_id"]),
discriminator(data["d"]["author"]["discriminator"]),
message_id(data["d"]["id"]),
isPinned(data["d"]["pinned"]),
isBot(d["author"].contains("bot") ? d["author"]["bot"].get<bool>() : false) {};
std::string send(const nlohmann::json& msg) {
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<std::string>() + "/messages", msg.dump());
}
};
#endif

View File

@ -1,33 +1,36 @@
#ifndef API_BOT_HPP_
#define API_BOT_HPP_
#include <includes.h>
using std::string, std::cout, std::endl, nlohmann::json;
template<typename...Args>
class Discord {
private:
void initializeNets(const json& data) {
std::tuple<std::unique_ptr<Args>...> net;
nlohmann::json data;
void initializeNets(const nlohmann::json& data) {
initializeNetsImpl(data, std::index_sequence_for<Args...>{});
}
template<unsigned long... Is>
void initializeNetsImpl(const json& data, std::index_sequence<Is...>) {
void initializeNetsImpl(const nlohmann::json& data, std::index_sequence<Is...>) {
net = std::make_tuple(std::make_unique<Args>(data)...);
}
json data;
public:
std::tuple<std::unique_ptr<Args>...> net;
Discord(const json& data) : data(data) {
Discord(const nlohmann::json& data) : data(data) {
initializeNets(data);
}
template<unsigned long Index>
auto& get() const {
return *std::get<Index>(net);
}
};
class Bot {
private:
json data;
nlohmann::json data;
public:
Bot(const json& data) : data(data) {};
string id() {
Bot(const nlohmann::json& data) : data(data) {};
std::string id() const {
return data["d"]["id"];
}
bool isBot() {
bool isBot() const {
try {
return data["d"]["bot"];
}

View File

@ -2,16 +2,15 @@
#define API_CHANNEL_HPP_
#include <includes.h>
#include <net.h>
using std::string, std::cout, std::endl, nlohmann::json;
class Channel {
private:
json data;
nlohmann::json data;
WebSocket& web;
NetworkManager& req;
public:
Channel(const json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {}
string send(const json& msg) {
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<string>() + "/messages", msg.dump());
Channel(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {}
std::string send(const nlohmann::json& msg) {
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<std::string>() + "/messages", msg.dump());
}
};
#endif

View File

@ -1,58 +1,57 @@
#ifndef API_EMBED_HPP_
#define API_EMBED_HPP_
#include <includes.h>
using std::string, std::cout, std::endl, nlohmann::json;
class EmbedBuilder {
private:
json embed = {
nlohmann::json embed = {
{"content", ""},
{"tts", false},
{"embeds", json::array()}
{"embeds", nlohmann::json::array()}
}, data;
public:
EmbedBuilder(const json& data) : data(data) {
EmbedBuilder(const nlohmann::json& data) : data(data) {
embed = {
{"content", ""},
{"tts", false},
{"embeds", json::array()}
{"embeds", nlohmann::json::array()}
};
}
EmbedBuilder& addTitle(const string& title) {
EmbedBuilder& addTitle(const std::string& title) {
if (embed["embeds"].size() == 0) {
embed["embeds"].push_back(json::object());
embed["embeds"].emplace_back(nlohmann::json::object());
}
embed["embeds"].back()["title"] = title;
return *this;
}
EmbedBuilder& addDescription(const string& desc) {
EmbedBuilder& addDescription(const std::string& desc) {
if (embed["embeds"].size() == 0) {
embed["embeds"].push_back(json::object());
embed["embeds"].emplace_back(nlohmann::json::object());
}
embed["embeds"].back()["description"] = desc;
return *this;
}
EmbedBuilder& addColor(const string& color) {
EmbedBuilder& addColor(const std::string& color) {
if (embed["embeds"].size() == 0) {
embed["embeds"].push_back(json::object());
embed["embeds"].emplace_back(nlohmann::json::object());
}
embed["embeds"].back()["color"] = color;
return *this;
}
EmbedBuilder& addUrl(const string& url) {
EmbedBuilder& addUrl(const std::string& url) {
if (embed["embeds"].size() == 0) {
embed["embeds"].push_back(json::object());
embed["embeds"].emplace_back(nlohmann::json::object());
}
embed["embeds"].back()["url"] = url;
return *this;
}
EmbedBuilder& addImage(const string& imageurl) {
EmbedBuilder& addImage(const std::string& imageurl) {
if (embed["embeds"].size() == 0) {
embed["embeds"].push_back(json::object());
embed["embeds"].emplace_back(nlohmann::json::object());
}
embed["embeds"].back()["image"] = { {"url", imageurl} };
return *this;
}
json getEmbed() const {
nlohmann::json getEmbed() const {
return embed;
}
};

View File

@ -1,14 +1,13 @@
#ifndef API_GUILD_HPP_
#define API_GUILD_HPP_
#include <includes.h>
//#include <net.h>
using std::string, std::cout, std::endl, nlohmann::json;
#include <net.h>
class Guild {
private:
json data;
nlohmann::json data;
public:
Guild(const json& data) : data(data) {}
string id() {
Guild(const nlohmann::json& data) : data(data) {}
std::string id() const {
return data["d"]["user"]["id"];
}
};

View File

@ -2,18 +2,18 @@
#define API_MESSAGE_HPP_
#include <includes.h>
#include <net.h>
using std::string, std::cout, std::endl, nlohmann::json;
class Message {
private:
json data;
nlohmann::json data;
WebSocket& web;
NetworkManager& req;
public:
Message(const json& data) : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {};
string send(const string& id, const json& msg) {
Message(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {};
std::string send(const std::string& id, const nlohmann::json& msg) {
std::cout << id << msg << std::endl;
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + id + "/messages", msg.dump());
}
string getMessages(const string& id, const string& count, const string& before = "") {
std::string getMessages(const std::string& id, const std::string& count, const std::string& before = "") {
if (before.empty()) {
return req.request(HttpMethods::GET, DiscordEndpoints::details::latest + "/channels/" + id + "/messages?limit=" + count);
}
@ -22,8 +22,22 @@ public:
}
return "";
}
string deleteMessage(const string& channel_id, const string& message_id) {
std::string deleteMessage(const std::string& channel_id, const std::string& message_id) {
return req.request(HttpMethods::DELETE, DiscordEndpoints::details::latest + "/channels/" + channel_id + "/messages/" + message_id);
}
nlohmann::json pack(const nlohmann::json& key, const std::string& value) const {
return {{ key, value }};
}
struct Api {
nlohmann::json data;
NetworkManager& req;
Api(const nlohmann::json& json = "") : data(json), req(NetworkManager::getInstance()) {};
unsigned latency() const {
return req.getLatency();
}
};
std::string str() const {
return std::string(data);
}
};
#endif

View File

@ -3,18 +3,18 @@
#include <includes.h>
#include <net.h>
#include <vector>
using std::string, std::cout, std::endl, nlohmann::json;
class User {
private:
json data;
nlohmann::json data;
WebSocket& web;
NetworkManager& req;
public:
User(const json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {}
json extract(const std::vector<string>& keys) {
std::vector<string> d = { "d" };
User(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {}
#ifdef DEBUG
nlohmann::json extract(const std::vector<std::string>& keys) {
std::vector<std::string> d = { "d" };
d.insert(d.end(), keys.begin(), keys.end());
json current = data;
nlohmann::json current = data;
for (const auto& key : d) {
if (current.contains(key)) {
current = current[key];
@ -25,10 +25,11 @@ public:
}
return current;
}
virtual string me() {
#endif
std::string me() {
return req.request(HttpMethods::GET, DiscordEndpoints::details::latest + "/users/@me");
}
bool isBot() {
bool isBot() const {
try {
return data["d"]["author"]["bot"];
}

View File

@ -2,10 +2,8 @@
#define GATEWAY_WEBSOCKET_HPP_
#include <includes.h>
#include <thread>
#include <chrono>
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
using std::string, std::cout, std::endl, nlohmann::json;
using namespace std::chrono;
using namespace std::chrono_literals;
class WebSocket {
@ -14,11 +12,11 @@ private:
int intents;
std::string token;
ix::WebSocket webSocket;
json payload = { {"op", 1},{"d", nullptr} }, id;
std::unordered_map<std::string, std::function<void(const json&)>> eventHandlers;
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(const WebSocket&) = delete;
WebSocket(const std::string& token, const int& intents, bool& isBot) {
WebSocket(const std::string& token, const int& intents, const bool& isBot) {
WebSocket::token = token;
WebSocket::intents = intents;
WebSocket::isBot = isBot;
@ -34,7 +32,7 @@ private:
}},
//{"compress", 1},
{"presence", {
{"activities", json::array({
{"activities", nlohmann::json::array({
{
//{"name", "asdsadsadsadsa"},
//{"type", 2}
@ -50,9 +48,9 @@ private:
webSocket.setUrl("wss://gateway.discord.gg/?v=10&encoding=json");
webSocket.setHandshakeTimeout(5);
Log::create(INFO, WEBSOCKET, "ixwebsocket init");
webSocket.setOnMessageCallback([this, res = json(), heartbeat_interval = 0, connected = false](const ix::WebSocketMessagePtr& msg) mutable {
webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 0, connected = false](const ix::WebSocketMessagePtr& msg) mutable {
if (msg->type == ix::WebSocketMessageType::Message) {
res = json::parse(msg->str);
res = nlohmann::json::parse(msg->str);
Log::create(INFO, WEBSOCKET, res["op"].dump() + " " + res["t"].dump());
switch (res["op"].get<int>()) {
case 10:
@ -106,14 +104,14 @@ public:
int getIntents() const {
return WebSocket::intents;
}
void on(const string& event, std::function<void(const json&)> handler) {
eventHandlers[event] = [handler](const json& message) {
handler(message.get<json>());
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 string& event, std::function<void(const json&)> handler) {
eventHandlers[event] = [event, handler, isCalled = false](const json& message) mutable {
isCalled == false ? isCalled = true : 0, handler(message.get<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 == false ? isCalled = true : 0, handler(message.get<nlohmann::json>());
};
}
void start() {

View File

@ -3,12 +3,12 @@
#include <includes.h>
#include <gateway/Websocket.hpp>
#include <curl/curl.h>
using std::string, std::cout, std::endl, nlohmann::json;
class NetworkManager {
private:
CURL* curl;
CURLcode res;
WebSocket& web;
std::chrono::duration<double, std::milli> duration;
NetworkManager& operator=(const NetworkManager&) = delete;
NetworkManager(const NetworkManager&) = delete;
NetworkManager() : web(WebSocket::getInstance()) {
@ -16,7 +16,7 @@ private:
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (!curl) {
Log::create(ERROR, NETWORK, "Failed to initialize CURL");
Log::create(CRITICAL, NETWORK, "Failed to initialize CURL");
abort();
}
}
@ -36,13 +36,17 @@ public:
static NetworkManager instance;
return instance;
}
unsigned long getLatency() const {
return duration.count();
}
std::string request(const std::string& method, const std::string& path, const std::string& data = "") {
std::string response;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, path.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_slist* headers = curl_slist_append(headers, "Accept: application/json");
curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, ("Authorization: " + web.getToken()).c_str());
headers = curl_slist_append(headers, "User-Agent: DiscordBot");
@ -50,9 +54,14 @@ public:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY);
auto start = std::chrono::steady_clock::now();
if ((res = curl_easy_perform(curl)) != 0) Log::create(ERROR, NETWORK, "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res)));
curl_slist_free_all(headers);
duration = std::chrono::steady_clock::now() - start;
}
#ifdef DEBUG
Log::create(INFO, NETWORK, response);
#endif
return response;
}
};

View File

@ -84,23 +84,23 @@ struct HttpMethods {
static constexpr const char* OPTIONS = "OPTIONS";
};
struct ApiVersion {
static inline std::string api = "/api/";
static inline std::string v10 = "v10";
static inline std::string v9 = "v9";
static inline std::string v8 = "v8";
static inline std::string v7 = "v7";
static inline std::string v6 = "v6";
static inline std::string current = api + v6;
static inline std::string latest = api + v10;
static const inline std::string api = "/api/";
static const inline std::string v10 = "v10";
static const inline std::string v9 = "v9";
static const inline std::string v8 = "v8";
static const inline std::string v7 = "v7";
static const inline std::string v6 = "v6";
static const inline std::string current = api + v6;
static const inline std::string latest = api + v10;
};
struct DiscordEndpoints {
static inline std::string main_scheme = "https://";
static inline std::string discord = main_scheme + "discord.com";
static inline std::string images = main_scheme + "cdn.discord.com";
static inline std::string media = main_scheme + "media.discord.com";
static const inline std::string main_scheme = "https://";
static const inline std::string discord = main_scheme + "discord.com";
static const inline std::string images = main_scheme + "cdn.discord.com";
static const inline std::string media = main_scheme + "media.discord.com";
struct details {
static inline std::string current = DiscordEndpoints::discord + ApiVersion::current;
static inline std::string latest = DiscordEndpoints::discord + ApiVersion::latest;
static const inline std::string current = DiscordEndpoints::discord + ApiVersion::current;
static const inline std::string latest = DiscordEndpoints::discord + ApiVersion::latest;
};
};
enum GatewayIntents {

8
libs/utils/functions.hpp Normal file
View File

@ -0,0 +1,8 @@
#ifndef UTILS_FUNCTIONS_HPP_
#define UTILS_FUNCTIONS_HPP_
struct functions {
static inline std::string isNull(const nlohmann::json& str) {
return str.is_null() ? str.dump() : str.get<std::string>();
}
};
#endif

View File

@ -5,7 +5,6 @@
#include <iostream>
#include <iomanip>
#include <ctime>
using std::setfill, std::setw;
enum level { INFO, WARNING, ERROR, CRITICAL };
enum type { WEBSOCKET, NETWORK, API };
class Log {
@ -14,21 +13,11 @@ public:
#ifdef DEBUG
std::string color;
switch (lvl) {
case INFO:
color = "\033[34;1m";
break;
case WARNING:
color = "\033[33;1m";
break;
case ERROR:
color = "\033[31;1m";
break;
case CRITICAL:
color = "\033[31;1;2m";
break;
default:
color = "\033[0m";
break;
case INFO: color = "\033[34;1m"; break;
case WARNING: color = "\033[33;1m"; break;
case ERROR: color = "\033[31;1m"; break;
case CRITICAL: color = "\033[31;1;2m"; break;
default: color = "\033[0m"; break;
}
std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[0m" << message << "\033[0m" << std::endl;
#endif
@ -38,7 +27,10 @@ private:
std::time_t now_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm* timer = std::localtime(&now_c);
std::ostringstream oss;
oss << setfill('0') << setw(2) << timer->tm_hour << ":" << setfill('0') << setw(2) << timer->tm_min << ":" << setfill('0') << setw(2) << timer->tm_sec;
oss << std::setfill('0') << std::setw(2)
<< timer->tm_hour << ":" << std::setfill('0') << std::setw(2)
<< timer->tm_min << ":" << std::setfill('0') << std::setw(2)
<< timer->tm_sec;
return oss.str();
}
static std::string str(const level& lvl) {

View File

@ -4,10 +4,13 @@ int main(int argc, char* argv[]) {
if (argc != 3) return -1;
WebSocket* bot = &WebSocket::getInstance(argv[2], GatewayIntents::AllIntents);
bot->on(GatewayEvents::READY, [](const Discord<Message, User>& a) {
cout << DiscordEndpoints::details::latest << endl;
std::cout << DiscordEndpoints::details::latest << std::endl;
});
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author>& msg) {
g(0, msg.net)->send("939957962972229634", j("content", g(2, msg.net)->content()));
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author, Message::Api>& msg) {
if (msg.get<2>().isBot == false) {
msg.get<0>().send("939957962972229634", msg.get<0>().pack("content", std::to_string(msg.get<3>().latency()) + "ms"));
msg.get<0>().send("939957962972229634", msg.get<0>().pack("content", msg.get<2>().avatar));
}
});
bot->start();
return 0;

View File

@ -1,12 +1,7 @@
#include <gtest/gtest.h>
int add(int a, int b) {
return a + b;
}
TEST(AddTest, PositiveNumbers) {
EXPECT_EQ(add(1, 2), 3);
EXPECT_EQ(add(2, 3), 5);
}
TEST(AddTest, NegativeNumbers) {
EXPECT_EQ(add(-1, -1), -2);
EXPECT_EQ(add(-1, 1), 0);
#include <includes.h>
#include <net.h>
TEST(NetworkManagerTest, RequestReturnsExpectedValue) {
NetworkManager& networkManager = NetworkManager::getInstance();
EXPECT_FALSE(networkManager.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/939957962972229634/messages", { "content", "test" }).empty());
}