From bd4e3e7e8f7a88e4d2ca5c151a7be060fbd1205c Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Thu, 19 Oct 2023 10:51:27 +0100 Subject: [PATCH] feat: updated integration to now contain more information --- include/dpp/integration.h | 112 ++++++++++++++++++++------------------ src/dpp/integration.cpp | 71 ++++++++++++++---------- 2 files changed, 100 insertions(+), 83 deletions(-) diff --git a/include/dpp/integration.h b/include/dpp/integration.h index 6310c78bdb..0cb04327d3 100644 --- a/include/dpp/integration.h +++ b/include/dpp/integration.h @@ -26,6 +26,7 @@ #include #include #include +#include namespace dpp { @@ -47,41 +48,37 @@ enum integration_type { * @brief Integration flags */ enum integration_flags { - /// Integration enabled - if_enabled = 0b00000001, - /// Integration syncing - if_syncing = 0b00000010, - /// Emoji integration - if_emoticons = 0b00000100, - /// Integration revoked - if_revoked = 0b00001000, - /// Kick users when their subscription expires - if_expire_kick = 0b00010000, + if_enabled = 0b00000001, //!< is this integration enabled + if_syncing = 0b00000010, //!< is this integration syncing @warning This is not provided for discord bot integrations. + if_emoticons = 0b00000100, //!< whether emoticons should be synced for this integration (twitch only currently) @warning This is not provided for discord bot integrations. + if_revoked = 0b00001000, //!< has this integration been revoked @warning This is not provided for discord bot integrations. + if_expire_kick = 0b00010000, //!< kick user when their subscription expires, otherwise only remove the role that is specified by `role_id`. @warning This is not provided for discord bot integrations. }; /** * @brief An application that has been integrated */ struct DPP_EXPORT integration_app { - /// Integration id - snowflake id; - /// Name - std::string name; - /// Icon - std::string icon; - /// Description - std::string description; - /// Integration summary @deprecated Removed by Discord - std::string summary; - /// Pointer to bot user - class user* bot; + snowflake id; //!< the id of the app + std::string name; //!< the name of the app + utility::iconhash icon; //!< the icon hash of the app + std::string description; //!< the description of the app + class user* bot; //!< the bot associated with this application +}; + +/** + * @brief The account information for an integration. + */ +struct DPP_EXPORT integration_account { + snowflake id; //!< id of the account + std::string name; //!< name of the account }; /** * @brief Represents an integration on a guild, e.g. a connection to twitch. */ class DPP_EXPORT integration : public managed, public json_interface { -protected: + protected: friend struct json_interface; /** Read class values from json object @@ -96,29 +93,18 @@ class DPP_EXPORT integration : public managed, public json_interface scopes; //!< the scopes the application has been authorized for /** Default constructor */ integration(); @@ -126,15 +112,35 @@ class DPP_EXPORT integration : public managed, public json_interface { -protected: + protected: friend struct json_interface; /** Read class values from json object @@ -151,7 +157,7 @@ class DPP_EXPORT connection : public json_interface { */ connection& fill_from_json_impl(nlohmann::json* j); -public: + public: std::string id; //!< id of the connection account std::string name; //!< the username of the connection account std::string type; //!< the service of the connection (twitch, youtube, discord, or guild_subscription) diff --git a/src/dpp/integration.cpp b/src/dpp/integration.cpp index 7f0570e8a7..f17239dd8a 100644 --- a/src/dpp/integration.cpp +++ b/src/dpp/integration.cpp @@ -31,18 +31,7 @@ namespace dpp { using json = nlohmann::json; -integration::integration() : - managed(), - type(i_twitch), - flags(0), - role_id(0), - user_id(0), - expire_grace_period(0), - synced_at(0), - subscriber_count(0) -{ - app.id = 0; - app.bot = nullptr; +integration::integration() : managed(), type(i_twitch), flags(0), role_id(0), expire_grace_period(0), synced_at(0), subscriber_count(0) { } integration& integration::fill_from_json_impl(nlohmann::json* j) @@ -54,51 +43,74 @@ integration& integration::fill_from_json_impl(nlohmann::json* j) { "discord", i_discord }, { "guild_subscription", i_guild_subscription } }; - this->id = snowflake_not_null(j, "id"); - this->name = string_not_null(j, "name"); + + set_snowflake_not_null(j, "id", id); + set_string_not_null(j, "name", name); this->type = type_map[string_not_null(j, "type")]; + if (bool_not_null(j, "enabled")) { this->flags |= if_enabled; } if (bool_not_null(j, "syncing")) { this->flags |= if_syncing; } + + set_snowflake_not_null(j, "role_id", role_id); + if (bool_not_null(j, "enable_emoticons")) { this->flags |= if_emoticons; } - if (bool_not_null(j, "revoked")) { - this->flags |= if_revoked; - } if (int8_not_null(j, "expire_behavior")) { this->flags |= if_expire_kick; } - this->expire_grace_period = int32_not_null(j, "expire_grace_period"); - if (j->contains("user")) { - auto t = (*j)["user"]; - this->user_id = snowflake_not_null(&t, "user_id"); + + set_int32_not_null(j, "expire_grace_period", expire_grace_period); + + if(j->contains("user")) { + user_obj = user().fill_from_json(&((*j)["user"])); + } + + /* Should never be null, but best to check in-case they maybe change it */ + if(j->contains("account")) { + auto & ac = (*j)["account"]; + set_snowflake_not_null(&ac, "id", account.id); + set_string_not_null(&ac, "name", account.name); } + + set_ts_not_null(j, "synced_at", synced_at); + set_int32_not_null(j, "subscriber_count", subscriber_count); + + if (bool_not_null(j, "revoked")) { + this->flags |= if_revoked; + } + if (j->contains("application")) { auto & t = (*j)["application"]; - this->app.id = snowflake_not_null(&t, "id"); + set_snowflake_not_null(&t, "id", app.id); + set_string_not_null(&t, "name", app.name); + set_string_not_null(&t, "description", app.description); + set_iconhash_not_null(&t, "icon", app.icon); if (t.find("bot") != t.end()) { auto & b = t["bot"]; this->app.bot = dpp::find_user(snowflake_not_null(&b, "id")); } } - this->subscriber_count = int32_not_null(j, "subscriber_count"); - this->account_id = string_not_null(&((*j)["account"]), "id"); - this->account_name = string_not_null(&((*j)["account"]), "name"); + if(j->contains("scopes")) { + for (const auto& scope : (*j)["scopes"]) { + this->scopes.push_back(to_string(scope)); + } + } return *this; } json integration::to_json_impl(bool with_id) const { return json({ - { "expire_behavior", (flags & if_expire_kick) ? 1 : 0 }, - { "expire_grace_period", expire_grace_period }, - { "enable_emoticons", emoticons_enabled() } - }).dump(); + { "expire_behavior", (flags & if_expire_kick) ? 1 : 0 }, + { "expire_grace_period", expire_grace_period }, + { "enable_emoticons", emoticons_enabled() } + }).dump(); } bool integration::emoticons_enabled() const { @@ -138,5 +150,4 @@ connection& connection::fill_from_json_impl(nlohmann::json* j) { return *this; } - } // namespace dpp