ctx instead get

This commit is contained in:
fluttershy 2025-01-25 17:21:44 +05:00
parent 2ee04ba9ca
commit 4e6d1f7998
2 changed files with 13 additions and 12 deletions

View File

@ -1,27 +1,26 @@
#ifndef API_BOT_HPP_ #ifndef API_BOT_HPP_
#define API_BOT_HPP_ #define API_BOT_HPP_
#include <includes.hpp> #include <includes.hpp>
#include <variant>
template<typename...Args> template<typename...Args>
class Discord { class Discord {
private: private:
std::tuple<std::unique_ptr<Args>...> net; std::tuple<std::unique_ptr<Args>...> net;
nlohmann::json data; nlohmann::json data;
void initializeNets(const nlohmann::json& data) {
initializeNetsImpl(data, std::index_sequence_for<Args...>{});
}
template<unsigned long... Is> template<unsigned long... Is>
void initializeNetsImpl(const nlohmann::json& data, std::index_sequence<Is...>) { void initializeCtx(const nlohmann::json& data, std::index_sequence<Is...>) {
net = std::make_tuple(std::make_unique<Args>(data)...); net = std::make_tuple(std::make_unique<Args>(data)...);
} }
public: public:
Discord(const nlohmann::json& data) : data(data) { Discord(const nlohmann::json& data) : data(data) {
initializeNets(data); initializeCtx(data, std::index_sequence_for<Args...>{});
} }
template<class T> template<class T>
auto& get() const { [[deprecated("Use ctx() instead")]] auto& get() const {
return *std::get<std::unique_ptr<T>>(net); return *std::get<std::unique_ptr<T>>(net);
} }
const auto& ctx() const {
return net;
}
}; };
class Bot { class Bot {
private: private:

View File

@ -3,12 +3,14 @@
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], GatewayIntents::AllIntents); WebSocket* bot = &WebSocket::getInstance(argv[2], GatewayIntents::AllIntents);
bot->on(GatewayEvents::READY, [](const nlohmann::json&) { bot->on(GatewayEvents::READY, [](const Discord<User>& b) {
std::cout << DiscordEndpoints::details::latest << std::endl; auto& [user] = b.ctx();
std::cout << nlohmann::json::parse(user->me())["username"].get<std::string>() << std::endl;
}); });
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author, Message::Api>& ctx) { bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author, Message::Api>& msg) {
if (!ctx.get<Author>().isBot) { const auto& [message, user, author, _] = msg.ctx();
ctx.get<Message>().send("939957962972229634", ctx.get<Message>().pack("content", ctx.get<Author>().avatar)); if (!author->isBot) {
message->send("939957962972229634", message->pack("content", author->avatar));
} }
}); });
bot->start(); bot->start();