|
| 1 | +// |
| 2 | +// invite.hpp |
| 3 | +// ********** |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Sharon Fox (sharon at xandium dot io) |
| 6 | +// |
| 7 | +// Distributed under the MIT License. (See accompanying file LICENSE) |
| 8 | +// |
| 9 | + |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include "aegis/config.hpp" |
| 13 | +#include "aegis/snowflake.hpp" |
| 14 | +#include <nlohmann/json.hpp> |
| 15 | + |
| 16 | +namespace aegis |
| 17 | +{ |
| 18 | + |
| 19 | +namespace gateway |
| 20 | +{ |
| 21 | + |
| 22 | +namespace objects |
| 23 | +{ |
| 24 | + |
| 25 | +enum target_user_type { |
| 26 | + STREAM = 1 |
| 27 | +}; |
| 28 | + |
| 29 | +struct invite_metadata_t { |
| 30 | + int32_t uses; /**< # of times this invite has been used */ |
| 31 | + int32_t max_uses; /**< Max # of times this invite can be used */ |
| 32 | + int32_t max_age; /**< Duration (in seconds) after which the invite expires */ |
| 33 | + bool temporary = false; /**< Whether this invite only grants temporary membership */ |
| 34 | + std::string created_at; /**< ISO8601 timestamp of when this invite was created */ |
| 35 | +}; |
| 36 | + |
| 37 | +struct invite; |
| 38 | + |
| 39 | +/// \cond TEMPLATES |
| 40 | +void from_json(const nlohmann::json& j, invite& m); |
| 41 | +void to_json(nlohmann::json& j, const invite& m); |
| 42 | +/// \endcond |
| 43 | + |
| 44 | +/// Discord Invite Object |
| 45 | +struct invite |
| 46 | +{ |
| 47 | + invite(const std::string& _json, aegis::core* bot) noexcept |
| 48 | + { |
| 49 | + from_json(nlohmann::json::parse(_json), *this); |
| 50 | + } |
| 51 | + |
| 52 | + invite(const nlohmann::json& _json, aegis::core* bot) noexcept |
| 53 | + { |
| 54 | + from_json(_json, *this); |
| 55 | + } |
| 56 | + |
| 57 | + invite(aegis::core* bot) noexcept {} |
| 58 | + |
| 59 | + invite() noexcept {} |
| 60 | + |
| 61 | + std::string code; /**< Invite code (unique ID) */ |
| 62 | + snowflake _guild; /**< Guild this invite is for */ |
| 63 | + snowflake _channel; /**< Channel this invite is for */ |
| 64 | + snowflake inviter; /**< User that created the invite */ |
| 65 | + snowflake target_user; /**< Target user for this invite */ |
| 66 | + target_user_type target_type = STREAM; /**< Type of user target for this invite */ |
| 67 | + int32_t approximate_presence_count = 0; /**< Approximate count of online members of guild, requires target_user be set */ |
| 68 | + int32_t approximate_member_count = 0; /**< Approximate count of total members of guild */ |
| 69 | + invite_metadata_t metadata; /**< Extra information about invite */ |
| 70 | +}; |
| 71 | + |
| 72 | +/// \cond TEMPLATES |
| 73 | +inline void from_json(const nlohmann::json& j, invite& m) |
| 74 | +{ |
| 75 | + m.code = j["code"]; |
| 76 | + if (j.count("guild") && !j["guild"].is_null()) |
| 77 | + m._guild = j["guild"]["id"]; |
| 78 | + if (j.count("channel") && !j["channel"].is_null()) |
| 79 | + m._channel = j["channel"]["id"]; |
| 80 | + if (j.count("inviter") && !j["inviter"].is_null()) |
| 81 | + m.inviter = j["inviter"]["id"]; |
| 82 | + if (j.count("target_user") && !j["target_user"].is_null()) |
| 83 | + m.target_user = j["target_user"]["id"]; |
| 84 | + if (j.count("target_user_type") && !j["target_user_type"].is_null()) |
| 85 | + m.target_type = j["target_user_type"]; |
| 86 | + if (j.count("approximate_presence_count") && !j["approximate_presence_count"].is_null()) |
| 87 | + m.approximate_presence_count = j["approximate_presence_count"]; |
| 88 | + if (j.count("approximate_member_count") && !j["approximate_member_count"].is_null()) |
| 89 | + m.approximate_member_count = j["approximate_member_count"]; |
| 90 | + |
| 91 | + // Metadata |
| 92 | + if (j.count("uses") && !j["uses"].is_null()) |
| 93 | + m.metadata.uses = j["uses"]; |
| 94 | + if (j.count("max_uses") && !j["max_uses"].is_null()) |
| 95 | + m.metadata.max_uses = j["max_uses"]; |
| 96 | + if (j.count("max_age") && !j["max_age"].is_null()) |
| 97 | + m.metadata.max_age = j["max_age"]; |
| 98 | + if (j.count("temporary") && !j["temporary"].is_null()) |
| 99 | + m.metadata.temporary = j["temporary"]; |
| 100 | + if (j.count("created_at") && !j["created_at"].is_null()) |
| 101 | + m.metadata.created_at = j["created_at"]; |
| 102 | +} |
| 103 | + |
| 104 | +inline void to_json(nlohmann::json& j, const invite& m) |
| 105 | +{ |
| 106 | + j["code"] = m.code; |
| 107 | + j["guild"] = m._guild; |
| 108 | + j["channel"] = m._channel; |
| 109 | + j["inviter"] = m.inviter; |
| 110 | + j["target_user"] = m.target_user; |
| 111 | + j["target_user_type"] = m.target_type; |
| 112 | + j["approximate_presence_count"] = m.approximate_presence_count; |
| 113 | + j["approximate_member_count"] = m.approximate_member_count; |
| 114 | + |
| 115 | + // Metadata |
| 116 | + j["uses"] = m.metadata.uses; |
| 117 | + j["max_uses"] = m.metadata.max_uses; |
| 118 | + j["max_age"] = m.metadata.max_age; |
| 119 | + j["temporary"] = m.metadata.temporary; |
| 120 | + j["created_at"] = m.metadata.created_at; |
| 121 | +} |
| 122 | +/// \endcond |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +} |
0 commit comments