diff --git a/module/Library/Discord/Misc.cpp b/module/Library/Discord/Misc.cpp index 0b47aaa0..00e2c6be 100644 --- a/module/Library/Discord/Misc.cpp +++ b/module/Library/Discord/Misc.cpp @@ -10,6 +10,7 @@ SQMOD_DECL_TYPENAME(SqDpUptime, _SC("SqDiscordUptime")) SQMOD_DECL_TYPENAME(SqDpIconHash, _SC("SqDiscordIconHash")) SQMOD_DECL_TYPENAME(SqDpVoiceState, _SC("SqDiscordVoiceState")) SQMOD_DECL_TYPENAME(SqDpEmoji, _SC("SqDiscordEmoji")) +SQMOD_DECL_TYPENAME(SqDpCommandConfirmation, _SC("SqDiscordCommandConfirmation")) // ------------------------------------------------------------------------------------------------ void Register_Discord_Misc(HSQUIRRELVM vm, Table & ns) @@ -112,6 +113,17 @@ void Register_Discord_Misc(HSQUIRRELVM vm, Table & ns) .Func(_SC("BuildJSON"), &DpEmoji::BuildJSON_) .Func(_SC("LoadImage"), &DpEmoji::LoadImage) ); + // -------------------------------------------------------------------------------------------- + ns.Bind(_SC("CommandConfirmation"), + Class< DpCommandConfirmation, NoConstructor< DpCommandConfirmation > >(vm, SqDpCommandConfirmation::Str) + // Meta-methods + .SquirrelFunc(_SC("_typename"), &SqDpCommandConfirmation::Fn) + // Member Properties + .Prop(_SC("Valid"), &DpCommandConfirmation::IsValid) + .Prop(_SC("HttpBody"), &DpCommandConfirmation::GetHttpBody) + // Member Methods + //.Func(_SC("SetName"), &DpCommandConfirmation::ApplyName) + ); } } // Namespace:: SqMod diff --git a/module/Library/Discord/Misc.hpp b/module/Library/Discord/Misc.hpp index 1f632a3b..c04d4664 100644 --- a/module/Library/Discord/Misc.hpp +++ b/module/Library/Discord/Misc.hpp @@ -390,4 +390,104 @@ struct DpEmoji SQMOD_NODISCARD std::string GetMention() const { return Valid().get_mention(); } }; +/* ------------------------------------------------------------------------------------------------ + * The results of a REST call wrapped in a convenient struct. +*/ +struct DpCommandConfirmation +{ + using Ptr = std::unique_ptr< dpp::confirmation_callback_t >; + /* -------------------------------------------------------------------------------------------- + * Referenced confirmation callback instance. + */ + Ptr mPtr{nullptr}; + /* -------------------------------------------------------------------------------------------- + * Whether the referenced pointer is owned. + */ + bool mOwned{false}; + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + DpCommandConfirmation() noexcept = default; + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + explicit DpCommandConfirmation(Ptr::pointer ptr, bool owned = false) noexcept + : mPtr(ptr), mOwned(owned) + { } + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + explicit DpCommandConfirmation(Ptr && ptr) noexcept + : mPtr(std::forward< Ptr >(ptr)), mOwned(true) + { } + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + explicit DpCommandConfirmation(const Ptr::element_type & o) noexcept + : DpCommandConfirmation(new Ptr::element_type(o), true) + { } + /* -------------------------------------------------------------------------------------------- + * Move constructor. + */ + explicit DpCommandConfirmation(Ptr::element_type && o) noexcept + : DpCommandConfirmation(new Ptr::element_type(std::forward< Ptr::element_type >(o)), true) + { } + /* -------------------------------------------------------------------------------------------- + * Copy constructor (disabled). + */ + DpCommandConfirmation(const DpCommandConfirmation & o) = delete; + /* -------------------------------------------------------------------------------------------- + * Move constructor. + */ + DpCommandConfirmation(DpCommandConfirmation && o) noexcept = default; + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~DpCommandConfirmation() noexcept { Cleanup(); } + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator (disabled). + */ + DpCommandConfirmation & operator = (const DpCommandConfirmation & o) = delete; + /* -------------------------------------------------------------------------------------------- + * Move assignment operator. + */ + DpCommandConfirmation & operator = (DpCommandConfirmation && o) noexcept + { + if (this != &o) { + Cleanup(); + // Transfer members values + mPtr = std::move(o.mPtr); + mOwned = o.mOwned; + } + return *this; + } + /* -------------------------------------------------------------------------------------------- + * Release any referenced resources and default to an empty/invalid state. + */ + void Cleanup() + { + // Do we own this to try delete it? + if (!mOwned && mPtr) { + // Not our job, simply forget about it + [[maybe_unused]] auto p = mPtr.release(); + } else mPtr.reset(); // We own this so delete the instance + } + /* -------------------------------------------------------------------------------------------- + * Validate the managed handle. + */ + void Validate() const { if (!mPtr) STHROWF("Invalid discord confirmation callback handle"); } + /* -------------------------------------------------------------------------------------------- + * Validate the managed handle and retrieve a const reference to it. + */ + SQMOD_NODISCARD Ptr::element_type & Valid() const { Validate(); return *mPtr; } + /* -------------------------------------------------------------------------------------------- + * Check whether a valid instance is managed. + */ + SQMOD_NODISCARD bool IsValid() const { return static_cast< bool >(mPtr); } + /* -------------------------------------------------------------------------------------------- + * Retrieve the raw body string of the HTTP result. + */ + SQMOD_NODISCARD std::string & GetHttpBody() const { return Valid().http_info.body; } +}; + } // Namespace:: SqMod