From 4e6d1f7998f0557540c9945832da9870c0d1c4a0 Mon Sep 17 00:00:00 2001 From: fluttershy Date: Sat, 25 Jan 2025 17:21:44 +0500 Subject: [PATCH] ctx instead get --- libs/api/Bot.hpp | 13 ++++++------- sources/main.cpp | 12 +++++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/libs/api/Bot.hpp b/libs/api/Bot.hpp index 7a95b43..f558026 100644 --- a/libs/api/Bot.hpp +++ b/libs/api/Bot.hpp @@ -1,27 +1,26 @@ #ifndef API_BOT_HPP_ #define API_BOT_HPP_ #include -#include template class Discord { private: std::tuple...> net; nlohmann::json data; - void initializeNets(const nlohmann::json& data) { - initializeNetsImpl(data, std::index_sequence_for{}); - } template - void initializeNetsImpl(const nlohmann::json& data, std::index_sequence) { + void initializeCtx(const nlohmann::json& data, std::index_sequence) { net = std::make_tuple(std::make_unique(data)...); } public: Discord(const nlohmann::json& data) : data(data) { - initializeNets(data); + initializeCtx(data, std::index_sequence_for{}); } template - auto& get() const { + [[deprecated("Use ctx() instead")]] auto& get() const { return *std::get>(net); } + const auto& ctx() const { + return net; + } }; class Bot { private: diff --git a/sources/main.cpp b/sources/main.cpp index d8b1567..9449ffb 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -3,12 +3,14 @@ int main(int argc, char* argv[]) { if (argc != 3) return -1; WebSocket* bot = &WebSocket::getInstance(argv[2], GatewayIntents::AllIntents); - bot->on(GatewayEvents::READY, [](const nlohmann::json&) { - std::cout << DiscordEndpoints::details::latest << std::endl; + bot->on(GatewayEvents::READY, [](const Discord& b) { + auto& [user] = b.ctx(); + std::cout << nlohmann::json::parse(user->me())["username"].get() << std::endl; }); - bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord& ctx) { - if (!ctx.get().isBot) { - ctx.get().send("939957962972229634", ctx.get().pack("content", ctx.get().avatar)); + bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord& msg) { + const auto& [message, user, author, _] = msg.ctx(); + if (!author->isBot) { + message->send("939957962972229634", message->pack("content", author->avatar)); } }); bot->start();