From 8ffc955054e189fb0e078cfa504dd4bad6da394e Mon Sep 17 00:00:00 2001 From: Archie Jaskowicz Date: Thu, 13 Jun 2024 22:01:56 +0100 Subject: [PATCH] feat: Added consuming an entitlement --- include/dpp/cluster.h | 10 ++++++++++ include/dpp/cluster_coro_calls.h | 11 +++++++++++ include/dpp/cluster_sync_calls.h | 14 ++++++++++++++ src/dpp/cluster/entitlement.cpp | 4 ++++ src/dpp/cluster_coro_calls.cpp | 4 ++++ src/dpp/cluster_sync_calls.cpp | 4 ++++ 6 files changed, 47 insertions(+) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index ec4044964b..9b486bbd53 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -3780,6 +3780,16 @@ class DPP_EXPORT cluster { */ void entitlement_test_delete(snowflake entitlement_id, command_completion_event_t callback = utility::log_error()); + /** + * @brief For One-Time Purchase consumable SKUs, marks a given entitlement for the user as consumed. + * + * @see https://discord.com/developers/docs/monetization/entitlements#consume-an-entitlement + * @param entitlement_id The entitlement to mark as consumed. + * @param callback Function to call when the API call completes. + * On success the callback will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error(). + */ + void entitlement_consume(snowflake entitlement_id, command_completion_event_t callback = utility::log_error()); + /** * @brief Returns all SKUs for a given application. * @note Because of how Discord's SKU and subscription systems work, you will see two SKUs for your premium offering. diff --git a/include/dpp/cluster_coro_calls.h b/include/dpp/cluster_coro_calls.h index 1e4af10fe5..f46efaf581 100644 --- a/include/dpp/cluster_coro_calls.h +++ b/include/dpp/cluster_coro_calls.h @@ -751,6 +751,17 @@ */ [[nodiscard]] async co_entitlement_test_delete(snowflake entitlement_id); +/** + * @brief For One-Time Purchase consumable SKUs, marks a given entitlement for the user as consumed. + * + * @see dpp::cluster::entitlement_consume + * @see https://discord.com/developers/docs/monetization/entitlements#consume-an-entitlement + * @param entitlement_id The entitlement to mark as consumed. + * @return confirmation returned object on completion + * \memberof dpp::cluster + */ +[[nodiscard]] async co_entitlement_consume(snowflake entitlement_id); + /** * @brief Get the gateway information for the bot using the token * @see dpp::cluster::get_gateway_bot diff --git a/include/dpp/cluster_sync_calls.h b/include/dpp/cluster_sync_calls.h index e2844018e0..e288318e69 100644 --- a/include/dpp/cluster_sync_calls.h +++ b/include/dpp/cluster_sync_calls.h @@ -931,6 +931,20 @@ entitlement entitlement_test_create_sync(const class entitlement& new_entitlemen */ confirmation entitlement_test_delete_sync(snowflake entitlement_id); +/** + * @brief For One-Time Purchase consumable SKUs, marks a given entitlement for the user as consumed. + * + * @see dpp::cluster::entitlement_consume + * @see https://discord.com/developers/docs/monetization/entitlements#consume-an-entitlement + * @param entitlement_id The entitlement to mark as consumed. + * @return confirmation returned object on completion + * \memberof dpp::cluster + * @throw dpp::rest_exception upon failure to execute REST function + * @warning This function is a blocking (synchronous) call and should only be used from within a separate thread. + * Avoid direct use of this function inside an event handler. + */ +confirmation entitlement_consume_sync(snowflake entitlement_id); + /** * @brief Get the gateway information for the bot using the token * @see dpp::cluster::get_gateway_bot diff --git a/src/dpp/cluster/entitlement.cpp b/src/dpp/cluster/entitlement.cpp index 1783e79296..40a60152db 100644 --- a/src/dpp/cluster/entitlement.cpp +++ b/src/dpp/cluster/entitlement.cpp @@ -77,4 +77,8 @@ void cluster::entitlement_test_delete(const class snowflake entitlement_id, comm rest_request(this, API_PATH "/applications", me.id.str(), "entitlements/" + entitlement_id.str(), m_delete, "", callback); } +void cluster::entitlement_consume(const class snowflake entitlement_id, command_completion_event_t callback) { + rest_request(this, API_PATH "/applications", me.id.str(), "entitlements/" + entitlement_id.str() + "/consume", m_post, "", callback); +} + } // namespace dpp diff --git a/src/dpp/cluster_coro_calls.cpp b/src/dpp/cluster_coro_calls.cpp index dee4daef50..27e274a81e 100644 --- a/src/dpp/cluster_coro_calls.cpp +++ b/src/dpp/cluster_coro_calls.cpp @@ -275,6 +275,10 @@ async cluster::co_entitlement_test_delete(const class s return async{ this, static_cast(&cluster::entitlement_test_delete), entitlement_id }; } +async cluster::co_entitlement_consume(const class snowflake entitlement_id) { + return async{ this, static_cast(&cluster::entitlement_consume), entitlement_id }; +} + async cluster::co_get_gateway_bot() { return async{ this, static_cast(&cluster::get_gateway_bot) }; } diff --git a/src/dpp/cluster_sync_calls.cpp b/src/dpp/cluster_sync_calls.cpp index 368c8b8744..8372da2a93 100644 --- a/src/dpp/cluster_sync_calls.cpp +++ b/src/dpp/cluster_sync_calls.cpp @@ -273,6 +273,10 @@ confirmation cluster::entitlement_test_delete_sync(const class snowflake entitle return dpp::sync(this, static_cast(&cluster::entitlement_test_delete), entitlement_id); } +confirmation cluster::entitlement_consume_sync(const class snowflake entitlement_id) { + return dpp::sync(this, static_cast(&cluster::entitlement_consume), entitlement_id); +} + gateway cluster::get_gateway_bot_sync() { return dpp::sync(this, static_cast(&cluster::get_gateway_bot)); }