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_
#define API_BOT_HPP_
#include <includes.hpp>
#include <variant>
template<typename...Args>
class Discord {
private:
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 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)...);
}
public:
Discord(const nlohmann::json& data) : data(data) {
initializeNets(data);
initializeCtx(data, std::index_sequence_for<Args...>{});
}
template<class T>
auto& get() const {
[[deprecated("Use ctx() instead")]] auto& get() const {
return *std::get<std::unique_ptr<T>>(net);
}
const auto& ctx() const {
return net;
}
};
class Bot {
private:

View File

@ -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<User>& b) {
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) {
if (!ctx.get<Author>().isBot) {
ctx.get<Message>().send("939957962972229634", ctx.get<Message>().pack("content", ctx.get<Author>().avatar));
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author, Message::Api>& msg) {
const auto& [message, user, author, _] = msg.ctx();
if (!author->isBot) {
message->send("939957962972229634", message->pack("content", author->avatar));
}
});
bot->start();