From a19a171e0d13369b251a8c542d12cf77de03cfe6 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sat, 11 Sep 2021 21:18:45 +0300 Subject: [PATCH] Expose user type. --- module/Library/DPPTy.cpp | 38 ++++++- module/Library/DPPTy.hpp | 228 +++++++++++++++++++++++++++++++++++---- 2 files changed, 243 insertions(+), 23 deletions(-) diff --git a/module/Library/DPPTy.cpp b/module/Library/DPPTy.cpp index b9b12fb8..557d9e60 100644 --- a/module/Library/DPPTy.cpp +++ b/module/Library/DPPTy.cpp @@ -1,9 +1,6 @@ // ------------------------------------------------------------------------------------------------ #include "Library/DPPTy.hpp" -// ------------------------------------------------------------------------------------------------ -#include - // ------------------------------------------------------------------------------------------------ #include @@ -17,6 +14,7 @@ SQMOD_DECL_TYPENAME(SqDppUptime, _SC("SqDppUptime")) SQMOD_DECL_TYPENAME(SqDppActivity, _SC("SqDppActivity")) SQMOD_DECL_TYPENAME(SqDppPresence, _SC("SqDppPresence")) SQMOD_DECL_TYPENAME(SqDppVoiceState, _SC("SqDppVoiceState")) +SQMOD_DECL_TYPENAME(SqDppUser, _SC("SqDppUser")) SQMOD_DECL_TYPENAME(SqDppGuildMember, _SC("SqDppGuildMember")) SQMOD_DECL_TYPENAME(SqDppGuild, _SC("SqDppGuild")) @@ -148,6 +146,40 @@ void Register_DPPTy(HSQUIRRELVM vm, Table & ns) .Prop(_SC("IsSupressed"), &DpVoiceState::IsSupressed) ); // -------------------------------------------------------------------------------------------- + ns.Bind(_SC("User"), + Class< DpUser, NoConstructor< DpUser > >(vm, SqDppUser::Str) + // Meta-methods + .SquirrelFunc(_SC("_typename"), &SqDppUser::Fn) + // Member Properties + .Prop(_SC("Valid"), &DpUser::IsValid) + .Prop(_SC("Username"), &DpUser::GetUsername) + .Prop(_SC("Discriminator"), &DpUser::GetDiscriminator) + .Prop(_SC("Avatar"), &DpUser::GetAvatar) + .Prop(_SC("Flags"), &DpUser::GetFlags) + .Prop(_SC("RefCount"), &DpUser::GetRefCount) + .Prop(_SC("AvatarURL"), &DpUser::GetAvatarURL) + .Prop(_SC("IsBot"), &DpUser::IsBot) + .Prop(_SC("IsSystem"), &DpUser::IsSystem) + .Prop(_SC("IsMfaEnabled"), &DpUser::IsMfaEnabled) + .Prop(_SC("IsVerified"), &DpUser::IsVerified) + .Prop(_SC("HasNitroFull"), &DpUser::HasNitroFull) + .Prop(_SC("HasNitroClassic"), &DpUser::HasNitroClassic) + .Prop(_SC("IsDiscordEmployee"), &DpUser::IsDiscordEmployee) + .Prop(_SC("IsPartneredOwner"), &DpUser::IsPartneredOwner) + .Prop(_SC("HasHypesquadEvents"), &DpUser::HasHypesquadEvents) + .Prop(_SC("IsBughunter1"), &DpUser::IsBughunter1) + .Prop(_SC("IsHouseBravery"), &DpUser::IsHouseBravery) + .Prop(_SC("IsHouseBrilliance"), &DpUser::IsHouseBrilliance) + .Prop(_SC("IsHouseBalanace"), &DpUser::IsHouseBalanace) + .Prop(_SC("IsEarlySupporter"), &DpUser::IsEarlySupporter) + .Prop(_SC("IsTeamUser"), &DpUser::IsTeamUser) + .Prop(_SC("IsBughunter2"), &DpUser::IsBughunter2) + .Prop(_SC("IsVerifiedBot"), &DpUser::IsVerifiedBot) + .Prop(_SC("IsVerifiedBotDev"), &DpUser::IsVerifiedBotDev) + .Prop(_SC("IsCertifiedDoderator"), &DpUser::IsCertifiedDoderator) + .Prop(_SC("HasAnimatedIcon"), &DpUser::HasAnimatedIcon) + ); + // -------------------------------------------------------------------------------------------- ns.Bind(_SC("GuildMember"), Class< DpGuildMember, NoConstructor< DpGuildMember > >(vm, SqDppGuildMember::Str) // Meta-methods diff --git a/module/Library/DPPTy.hpp b/module/Library/DPPTy.hpp index 0e0d1b69..26497acf 100644 --- a/module/Library/DPPTy.hpp +++ b/module/Library/DPPTy.hpp @@ -601,6 +601,194 @@ struct DpVoiceState SQMOD_NODISCARD bool IsSupressed() const { return Valid().is_supressed(); } }; +/* ------------------------------------------------------------------------------------------------ + * Represents a user on discord. May or may not be a member of a DpGuild. +*/ +struct DpUser +{ + using Ptr = std::unique_ptr< dpp::user >; + /* -------------------------------------------------------------------------------------------- + * Referenced voice state instance. + */ + Ptr mPtr{nullptr}; + /* -------------------------------------------------------------------------------------------- + * Wether the referenced pointer is owned. + */ + bool mOwned{false}; + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + DpUser() noexcept = default; + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + explicit DpUser(Ptr::pointer ptr, bool owned = false) noexcept + : mPtr(ptr), mOwned(owned) + { } + /* -------------------------------------------------------------------------------------------- + * Copy constructor. + */ + explicit DpUser(const Ptr::element_type & o) noexcept + : DpUser(new Ptr::element_type(o), true) + { } + /* -------------------------------------------------------------------------------------------- + * Move constructor. + */ + explicit DpUser(Ptr::element_type && o) noexcept + : DpUser(new Ptr::element_type(std::forward< Ptr::element_type >(o)), true) + { } + /* -------------------------------------------------------------------------------------------- + * Copy constructor (disabled). + */ + DpUser(const DpUser & o) = delete; + /* -------------------------------------------------------------------------------------------- + * Move constructor. + */ + DpUser(DpUser && o) noexcept = default; + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~DpUser() noexcept + { + // Do we own this to try delete it? + if (!mOwned && mPtr) [[maybe_unused]] auto p = mPtr.release(); + } + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator (disabled). + */ + DpUser & operator = (const DpUser & o) = delete; + /* -------------------------------------------------------------------------------------------- + * Move assignment operator. + */ + DpUser & operator = (DpUser && o) noexcept + { + if (this != &o) { + // Do we own this to try delete it? + if (!mOwned && mPtr) [[maybe_unused]] auto p = mPtr.release(); + // Transfer members values + mPtr = std::move(o.mPtr); + mOwned = o.mOwned; + } + return *this; + } + /* -------------------------------------------------------------------------------------------- + * Validate the managed handle. + */ + void Validate() const { if (!mPtr) STHROWF("Invalid discord user handle"); } + /* -------------------------------------------------------------------------------------------- + * Validate the managed handle and retrieve a const reference to it. + */ + SQMOD_NODISCARD Ptr::element_type & Valid() const { Validate(); return *mPtr; } + /* -------------------------------------------------------------------------------------------- + * Check wether a valid instance is managed. + */ + SQMOD_NODISCARD bool IsValid() const { return static_cast< bool >(mPtr); } + /* -------------------------------------------------------------------------------------------- + * Retrieve user discord username. + */ + SQMOD_NODISCARD const std::string & GetUsername() const { return Valid().username; } + /* -------------------------------------------------------------------------------------------- + * Retrieve user discriminator (aka tag), 4 digits usually displayed with leading zeroes. + */ + SQMOD_NODISCARD SQInteger GetDiscriminator() const { return static_cast< SQInteger >(Valid().discriminator); } + /* -------------------------------------------------------------------------------------------- + * Retrieve user avatar hash. + */ + SQMOD_NODISCARD const dpp::utility::iconhash & GetAvatar() const { return Valid().avatar; } + /* -------------------------------------------------------------------------------------------- + * Retrieve user flags built from a bitmask of values in dpp::user_flags. + */ + SQMOD_NODISCARD SQInteger GetFlags() const { return static_cast< SQInteger >(Valid().flags); } + /* -------------------------------------------------------------------------------------------- + * Retrieve user feference count of how many guilds this user is in. + */ + SQMOD_NODISCARD SQInteger GetRefCount() const { return static_cast< SQInteger >(Valid().refcount); } + /* -------------------------------------------------------------------------------------------- + * Retrieve the avatar url of the user object. + */ + SQMOD_NODISCARD std::string GetAvatarURL() const { return Valid().get_avatar_url(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is a bot. + */ + SQMOD_NODISCARD bool IsBot() const { return Valid().is_bot(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is a system user (Clyde). + */ + SQMOD_NODISCARD bool IsSystem() const { return Valid().is_system(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has multi-factor authentication enabled. + */ + SQMOD_NODISCARD bool IsMfaEnabled() const { return Valid().is_mfa_enabled(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has verified account. + */ + SQMOD_NODISCARD bool IsVerified() const { return Valid().is_verified(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has full nitro. + */ + SQMOD_NODISCARD bool HasNitroFull() const { return Valid().has_nitro_full(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has nitro classic. + */ + SQMOD_NODISCARD bool HasNitroClassic() const { return Valid().has_nitro_classic(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is a discord employee. + */ + SQMOD_NODISCARD bool IsDiscordEmployee() const { return Valid().is_discord_employee(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user owns a partnered server. + */ + SQMOD_NODISCARD bool IsPartneredOwner() const { return Valid().is_partnered_owner(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has hype-squad events. + */ + SQMOD_NODISCARD bool HasHypesquadEvents() const { return Valid().has_hypesquad_events(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has the bug-hunter level 1 badge. + */ + SQMOD_NODISCARD bool IsBughunter1() const { return Valid().is_bughunter_1(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is in house bravery. + */ + SQMOD_NODISCARD bool IsHouseBravery() const { return Valid().is_house_bravery(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is in house brilliance. + */ + SQMOD_NODISCARD bool IsHouseBrilliance() const { return Valid().is_house_brilliance(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is in house balance. + */ + SQMOD_NODISCARD bool IsHouseBalanace() const { return Valid().is_house_balanace(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is an early supporter. + */ + SQMOD_NODISCARD bool IsEarlySupporter() const { return Valid().is_early_supporter(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is a team user. + */ + SQMOD_NODISCARD bool IsTeamUser() const { return Valid().is_team_user(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has the bug-hunter level 2 badge. + */ + SQMOD_NODISCARD bool IsBughunter2() const { return Valid().is_bughunter_2(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has the verified bot badge. + */ + SQMOD_NODISCARD bool IsVerifiedBot() const { return Valid().is_verified_bot(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is an early verified bot developer. + */ + SQMOD_NODISCARD bool IsVerifiedBotDev() const { return Valid().is_verified_bot_dev(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user is a certified moderator. + */ + SQMOD_NODISCARD bool IsCertifiedDoderator() const { return Valid().is_certified_moderator(); } + /* -------------------------------------------------------------------------------------------- + * Check wether the user has an animated icon. + */ + SQMOD_NODISCARD bool HasAnimatedIcon() const { return Valid().has_animated_icon(); } +}; + /* ------------------------------------------------------------------------------------------------ * Represents a guild on Discord (AKA a server). */ @@ -1009,83 +1197,83 @@ struct DpGuild * Connect to a voice channel another guild member is in. * Returns true if the user specified is in a voice channel, false if they aren't. */ - SQMOD_NODISCARD bool ConnectMemberVoice(SQInteger user_id) { return Valid().connect_member_voice(user_id); } + SQMOD_NODISCARD bool ConnectMemberVoice(SQInteger user_id) const { return Valid().connect_member_voice(user_id); } /* -------------------------------------------------------------------------------------------- * Is a large server (>250 users). */ - SQMOD_NODISCARD bool IsLarge() { return Valid().is_large(); } + SQMOD_NODISCARD bool IsLarge() const { return Valid().is_large(); } /* -------------------------------------------------------------------------------------------- * Is unavailable due to outage (most other fields will be blank or outdated). */ - SQMOD_NODISCARD bool IsUnavailable() { return Valid().is_unavailable(); } + SQMOD_NODISCARD bool IsUnavailable() const { return Valid().is_unavailable(); } /* -------------------------------------------------------------------------------------------- * Widget is enabled for this server. */ - SQMOD_NODISCARD bool WidgetEnabled() { return Valid().widget_enabled(); } + SQMOD_NODISCARD bool WidgetEnabled() const { return Valid().widget_enabled(); } /* -------------------------------------------------------------------------------------------- * Guild has an invite splash. */ - SQMOD_NODISCARD bool HasInviteSplash() { return Valid().has_invite_splash(); } + SQMOD_NODISCARD bool HasInviteSplash() const { return Valid().has_invite_splash(); } /* -------------------------------------------------------------------------------------------- * Guild has VIP regions. */ - SQMOD_NODISCARD bool HasVipRegions() { return Valid().has_vip_regions(); } + SQMOD_NODISCARD bool HasVipRegions() const { return Valid().has_vip_regions(); } /* -------------------------------------------------------------------------------------------- * Guild can have a vanity URL. */ - SQMOD_NODISCARD bool HasVanityURL() { return Valid().has_vanity_url(); } + SQMOD_NODISCARD bool HasVanityURL() const { return Valid().has_vanity_url(); } /* -------------------------------------------------------------------------------------------- * Guild is a verified server. */ - SQMOD_NODISCARD bool IsVerified() { return Valid().is_verified(); } + SQMOD_NODISCARD bool IsVerified() const { return Valid().is_verified(); } /* -------------------------------------------------------------------------------------------- * Guild is a discord partner server. */ - SQMOD_NODISCARD bool IsPartnered() { return Valid().is_partnered(); } + SQMOD_NODISCARD bool IsPartnered() const { return Valid().is_partnered(); } /* -------------------------------------------------------------------------------------------- * Guild has enabled community. */ - SQMOD_NODISCARD bool IsCommunity() { return Valid().is_community(); } + SQMOD_NODISCARD bool IsCommunity() const { return Valid().is_community(); } /* -------------------------------------------------------------------------------------------- * Guild has enabled commerce channels. */ - SQMOD_NODISCARD bool HasCommerce() { return Valid().has_commerce(); } + SQMOD_NODISCARD bool HasCommerce() const { return Valid().has_commerce(); } /* -------------------------------------------------------------------------------------------- * Guild has news channel. */ - SQMOD_NODISCARD bool HasNews() { return Valid().has_news(); } + SQMOD_NODISCARD bool HasNews() const { return Valid().has_news(); } /* -------------------------------------------------------------------------------------------- * Guild is discoverable. */ - SQMOD_NODISCARD bool IsDiscoverable() { return Valid().is_discoverable(); } + SQMOD_NODISCARD bool IsDiscoverable() const { return Valid().is_discoverable(); } /* -------------------------------------------------------------------------------------------- * Guild is featureable. */ - SQMOD_NODISCARD bool IsFeatureable() { return Valid().is_featureable(); } + SQMOD_NODISCARD bool IsFeatureable() const { return Valid().is_featureable(); } /* -------------------------------------------------------------------------------------------- * Guild is allowed an animated icon. */ - SQMOD_NODISCARD bool HasAnimatedIcon() { return Valid().has_animated_icon(); } + SQMOD_NODISCARD bool HasAnimatedIcon() const { return Valid().has_animated_icon(); } /* -------------------------------------------------------------------------------------------- * Guild has a banner image. */ - SQMOD_NODISCARD bool BasBanner() { return Valid().has_banner(); } + SQMOD_NODISCARD bool BasBanner() const { return Valid().has_banner(); } /* -------------------------------------------------------------------------------------------- * Guild has enabled welcome screen. */ - SQMOD_NODISCARD bool WelcomeScreenEnabled() { return Valid().is_welcome_screen_enabled(); } + SQMOD_NODISCARD bool WelcomeScreenEnabled() const { return Valid().is_welcome_screen_enabled(); } /* -------------------------------------------------------------------------------------------- * Guild has enabled membership screening. */ - SQMOD_NODISCARD bool HasMemberVerificationGate() { return Valid().has_member_verification_gate(); } + SQMOD_NODISCARD bool HasMemberVerificationGate() const { return Valid().has_member_verification_gate(); } /* -------------------------------------------------------------------------------------------- * Guild has preview enabled. */ - SQMOD_NODISCARD bool IsPreviewEnabled() { return Valid().is_preview_enabled(); } + SQMOD_NODISCARD bool IsPreviewEnabled() const { return Valid().is_preview_enabled(); } /* -------------------------------------------------------------------------------------------- * Server icon is actually an animated gif. */ - SQMOD_NODISCARD bool HasAnimatedIconHash() { return Valid().has_animated_icon_hash(); } + SQMOD_NODISCARD bool HasAnimatedIconHash() const { return Valid().has_animated_icon_hash(); } }; } // Namespace:: SqMod