From 1cbfe59bf65b81d456e5176a161cd8ca81f4ac39 Mon Sep 17 00:00:00 2001 From: fluttershy Date: Mon, 20 Jan 2025 04:03:27 +0500 Subject: [PATCH] fixes --- include/includes.h | 1 + libs/api/Author.hpp | 26 +++++++++++++------------- libs/api/Bot.hpp | 8 ++++---- libs/api/Guild.hpp | 2 +- libs/api/User.hpp | 2 +- libs/utils/functions.hpp | 13 +++++++++++++ libs/utils/types.hpp | 3 --- sources/main.cpp | 2 +- 8 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 libs/utils/functions.hpp diff --git a/include/includes.h b/include/includes.h index 2823bd0..35a0ef4 100644 --- a/include/includes.h +++ b/include/includes.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #endif \ No newline at end of file diff --git a/libs/api/Author.hpp b/libs/api/Author.hpp index fd6bb70..a7f22d8 100644 --- a/libs/api/Author.hpp +++ b/libs/api/Author.hpp @@ -9,8 +9,16 @@ private: WebSocket& web; NetworkManager& req; public: - Author(const json& data) : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {}; - string content() { + Author(const json& data) : + data(data), + web(WebSocket::getInstance()), + req(NetworkManager::getInstance()), + channel_id(functions::isNull(data["d"]["channel_id"])), + global_name(functions::isNull(data["d"]["author"]["global_name"])), + id(data["d"]["author"]["id"]) + { + }; + string content() const { try { return data["d"]["content"]; } @@ -18,19 +26,11 @@ public: 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() { + const string channel_id, global_name, id; + string discriminator() const { return data["d"]["author"]["discriminator"]; } - bool isBot() { + bool isBot() const { try { return data["d"]["author"]["bot"]; } diff --git a/libs/api/Bot.hpp b/libs/api/Bot.hpp index c0948fa..bc31ed0 100644 --- a/libs/api/Bot.hpp +++ b/libs/api/Bot.hpp @@ -5,15 +5,15 @@ using std::string, std::cout, std::endl, nlohmann::json; template class Discord { private: + std::tuple...> net; + json data; void initializeNets(const json& data) { initializeNetsImpl(data, std::index_sequence_for{}); } templatevoid initializeNetsImpl(const json& data, std::index_sequence) { net = std::make_tuple(std::make_unique(data)...); } - json data; public: - std::tuple...> net; Discord(const json& data) : data(data) { initializeNets(data); } @@ -27,10 +27,10 @@ private: json data; public: Bot(const json& data) : data(data) {}; - string id() { + string id() const { return data["d"]["id"]; } - bool isBot() { + bool isBot() const { try { return data["d"]["bot"]; } diff --git a/libs/api/Guild.hpp b/libs/api/Guild.hpp index 510cd7f..d88eaae 100644 --- a/libs/api/Guild.hpp +++ b/libs/api/Guild.hpp @@ -8,7 +8,7 @@ private: json data; public: Guild(const json& data) : data(data) {} - string id() { + string id() const { return data["d"]["user"]["id"]; } }; diff --git a/libs/api/User.hpp b/libs/api/User.hpp index 02bfe38..f065679 100644 --- a/libs/api/User.hpp +++ b/libs/api/User.hpp @@ -28,7 +28,7 @@ public: virtual string me() { return req.request(HttpMethods::GET, DiscordEndpoints::details::latest + "/users/@me"); } - bool isBot() { + bool isBot() const { try { return data["d"]["author"]["bot"]; } diff --git a/libs/utils/functions.hpp b/libs/utils/functions.hpp new file mode 100644 index 0000000..ad19ed9 --- /dev/null +++ b/libs/utils/functions.hpp @@ -0,0 +1,13 @@ +#ifndef UTILS_FUNCTIONS_HPP_ +#define UTILS_FUNCTIONS_HPP_ +struct functions { + static inline std::string isNull(const nlohmann::json& str) { + try { + return str.is_null() ? str.dump() : str.get(); + } + catch (...) { + return "null"; + } + } +}; +#endif \ No newline at end of file diff --git a/libs/utils/types.hpp b/libs/utils/types.hpp index 5e84e84..13526ef 100644 --- a/libs/utils/types.hpp +++ b/libs/utils/types.hpp @@ -1,7 +1,4 @@ #ifndef UTILS_TYPES_HPP_ #define UTILS_TYPES_HPP_ -#define j(...) {{__VA_ARGS__}} -#define je(...) {__VA_ARGS__} -#define g(x, y) (std::get(y)) #define RELEASE #endif \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index 60a025d..25f547e 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -7,7 +7,7 @@ int main(int argc, char* argv[]) { cout << DiscordEndpoints::details::latest << endl; }); bot->on(GatewayEvents::MESSAGE_CREATE, [bot](const Discord& msg) { - msg.get<0>().send("939957962972229634", msg.get<0>().pack({ "content", msg.get<2>().content() })); + msg.get<0>().send("939957962972229634", msg.get<0>().pack({ "content", msg.get<2>().id })); }); bot->start(); return 0;