This repository has been archived on 2025-03-15. You can view files and clone it, but cannot push or open issues or pull requests.
sparkle/libs/api/Bot.hpp

41 lines
1.0 KiB
C++
Raw Normal View History

2024-12-31 02:20:56 +05:00
#ifndef API_BOT_HPP_
#define API_BOT_HPP_
2025-01-25 13:22:59 +05:00
#include <includes.hpp>
2024-12-31 02:20:56 +05:00
template<typename...Args>
2025-01-19 04:35:20 +05:00
class Discord {
2024-12-31 02:20:56 +05:00
private:
2025-01-25 11:24:56 +05:00
std::tuple<std::unique_ptr<Args>...> net;
nlohmann::json data;
2024-12-31 02:20:56 +05:00
template<unsigned long... Is>
2025-01-25 17:21:44 +05:00
void initializeCtx(const nlohmann::json& data, std::index_sequence<Is...>) {
2024-12-31 02:20:56 +05:00
net = std::make_tuple(std::make_unique<Args>(data)...);
}
public:
2025-01-25 11:24:56 +05:00
Discord(const nlohmann::json& data) : data(data) {
2025-01-25 17:21:44 +05:00
initializeCtx(data, std::index_sequence_for<Args...>{});
2024-12-31 02:20:56 +05:00
}
2025-01-25 15:46:27 +05:00
template<class T>
2025-01-25 17:21:44 +05:00
[[deprecated("Use ctx() instead")]] auto& get() const {
2025-01-25 15:46:27 +05:00
return *std::get<std::unique_ptr<T>>(net);
2025-01-25 11:24:56 +05:00
}
2025-01-25 17:21:44 +05:00
const auto& ctx() const {
return net;
}
2025-01-06 22:18:30 +05:00
};
2025-01-19 04:35:20 +05:00
class Bot {
2025-01-06 22:18:30 +05:00
private:
2025-01-25 11:24:56 +05:00
nlohmann::json data;
2025-01-06 22:18:30 +05:00
public:
2025-01-25 11:24:56 +05:00
Bot(const nlohmann::json& data) : data(data) {};
std::string id() const {
2025-01-06 22:18:30 +05:00
return data["d"]["id"];
2024-12-31 02:20:56 +05:00
}
2025-01-25 11:24:56 +05:00
bool isBot() const {
2024-12-31 02:20:56 +05:00
try {
2025-01-06 22:18:30 +05:00
return data["d"]["bot"];
2025-01-25 13:22:59 +05:00
} catch (...) {
2024-12-31 02:20:56 +05:00
return false;
}
}
};
#endif