This commit is contained in:
fluttershy 2025-01-27 22:02:21 +05:00
parent 84d6b3d71c
commit 9205584e70
6 changed files with 69 additions and 25 deletions

View File

@ -4,30 +4,40 @@
#include <net.hpp> #include <net.hpp>
class Author { class Author {
private: private:
const nlohmann::json& data, &d; const nlohmann::json& data;
WebSocket& web; WebSocket& web;
NetworkManager& req; NetworkManager& req;
public: public:
struct Channel {
std::string channel_id;
std::string global_name;
std::string id;
std::string content;
std::string avatar;
std::string guild_id;
std::string discriminator;
std::string message_id;
bool isPinned;
bool isBot;
} authorData;
const std::string channel_id, global_name, id, content, avatar, guild_id, discriminator, message_id; const std::string channel_id, global_name, id, content, avatar, guild_id, discriminator, message_id;
bool isPinned, isBot; bool isPinned, isBot;
Author(const nlohmann::json& data) : Author(const nlohmann::json& data)
data(data), : data(data),
d(data["d"]),
web(WebSocket::getInstance()), web(WebSocket::getInstance()),
req(NetworkManager::getInstance()), req(NetworkManager::getInstance()),
channel_id(functions::isNull(d["channel_id"])), channel_id(functions::testValue<std::string>(data, { "d", "channel_id" }).value_or("")),
global_name(functions::isNull(d["author"]["global_name"])), global_name(functions::testValue<std::string>(data, { "d", "author", "global_name" }).value_or("")),
id(d["author"]["id"]), id(functions::testValue<std::string>(data, { "d", "author", "id" }).value_or("")),
content(d["content"]), content(functions::testValue<std::string>(data, { "d", "content" }).value_or("")),
avatar(d["author"]["avatar"]), avatar(functions::testValue<std::string>(data, { "d", "author", "avatar" }).value_or("")),
guild_id(d["guild_id"]), guild_id(functions::testValue<std::string>(data, { "d", "guild_id" }).value_or("")),
discriminator(d["author"]["discriminator"]), discriminator(functions::testValue<std::string>(data, { "d", "author", "discriminator" }).value_or("")),
message_id(d["id"]), message_id(functions::testValue<std::string>(data, { "d", "id" }).value_or("")),
isPinned(d["pinned"]), isPinned(functions::testValue<bool>(data, { "d", "pinned" }).value_or(false)),
isBot(d["author"].contains("bot") ? d["author"]["bot"].get<bool>() : false) { isBot(functions::testValue<bool>(data, { "d", "author", "bot" }).value_or(false)) {}
};
std::string send(const nlohmann::json& msg) { std::string send(const nlohmann::json& msg) {
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + d["channel_id"].get<std::string>() + "/messages", msg.dump()); return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<std::string>() + "/messages", msg.dump());
} }
}; };
#endif #endif

View File

@ -9,8 +9,13 @@ private:
WebSocket& web; WebSocket& web;
NetworkManager& req; NetworkManager& req;
public: public:
User(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()), User(const nlohmann::json& data) : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()),
isBot(data["d"]["user"].contains("bot") ? data["d"]["user"]["bot"].get<bool>() : false) {} isBot(false) {
auto botValue = functions::testValue<bool>(data, { "d", "user", "bot" });
if (botValue.has_value()) {
isBot = botValue.value();
}
}
bool isBot; bool isBot;
/* /*
nlohmann::json extract(const std::vector<std::string>& keys) { nlohmann::json extract(const std::vector<std::string>& keys) {

View File

@ -103,6 +103,7 @@ private:
}).detach(); }).detach();
break; break;
case GatewayOpcodes::Dispatch: case GatewayOpcodes::Dispatch:
Log::force_create(INFO, WEBSOCKET, res.dump());
emit(res["t"].get<std::string>(), res); emit(res["t"].get<std::string>(), res);
break; break;
default: default:

View File

@ -1,8 +1,38 @@
#ifndef UTILS_FUNCTIONS_HPP_ #ifndef UTILS_FUNCTIONS_HPP_
#define UTILS_FUNCTIONS_HPP_ #define UTILS_FUNCTIONS_HPP_
#include <optional>
struct functions { struct functions {
static inline std::string isNull(const nlohmann::json& str) { static inline std::string isNull(const nlohmann::json& str) {
return str.is_null() ? str.dump() : str.get<std::string>(); return str.is_null() ? str.dump() : str.get<std::string>();
} }
template<typename T>
static std::optional<T> testValue(const nlohmann::json& data, const std::vector<std::string>& keys) {
const nlohmann::json* current = &data;
for (const auto& key : keys) {
if (current->contains(key)) {
current = &(*current)[key];
} else {
return std::nullopt;
}
}
if constexpr (std::is_same_v<T, bool>) {
if (current->is_boolean()) {
return current->get<bool>();
}
} else if constexpr (std::is_same_v<T, int>) {
if (current->is_number_integer()) {
return current->get<int>();
}
} else if constexpr (std::is_same_v<T, double>) {
if (current->is_number_float()) {
return current->get<double>();
}
} else if constexpr (std::is_same_v<T, std::string>) {
if (current->is_string()) {
return current->get<std::string>();
}
}
return std::nullopt;
}
}; };
#endif #endif

View File

@ -34,12 +34,10 @@ int main(int argc, char* argv[]) {
std::cout << nlohmann::json::parse(user->me())["username"].get<std::string>() << std::endl; std::cout << nlohmann::json::parse(user->me())["username"].get<std::string>() << std::endl;
}); });
bot->on(GatewayEvents::MESSAGE_CREATE, [bot](const Discord<Message, Author>& msg) { bot->on(GatewayEvents::MESSAGE_CREATE, [bot](const Discord<Message, User, Author>& msg) {
auto& [message, author] = msg.ctx(); auto& [message, user, author] = msg.ctx();
if (!author->isBot) { message->send("939957962972229634", message->pack("content", std::to_string(user->isBot)));
message->send("939957962972229634", message->pack("content", author->content)); bot->getEvents();
bot->getEvents();
}
}); });
bot->start(); bot->start();
return 0; return 0;