From d2171396f953564cf3393bd3c58fc3201639e882 Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Mon, 14 Oct 2024 22:43:37 -0400 Subject: [PATCH] Add Consume Entitlement endpoint and missing fields for SKUs and Entitlements --- src/http/client.rs | 22 ++++++++++++++++++++++ src/http/routing.rs | 4 ++++ src/model/monetization.rs | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/http/client.rs b/src/http/client.rs index 659e0c98c8f..a04f5a2cb03 100644 --- a/src/http/client.rs +++ b/src/http/client.rs @@ -3395,6 +3395,28 @@ impl Http { .await } + /// For a one-time purchase consumable SKU (of kind [`Consumable`]), marks the entitlement as + /// consumed. + /// + /// The entitlement will have its `consumed` field set to `true` when fetched using + /// [`Self::get_entitlements`]. + /// + /// [`Consumable`]: SkuKind::Consumable + pub async fn consume_entitlement(&self, entitlement_id: EntitlementId) -> Result<()> { + self.wind(204, Request { + body: None, + multipart: None, + headers: None, + method: LightMethod::Post, + route: Route::ConsumeEntitlement { + application_id: self.try_application_id()?, + entitlement_id, + }, + params: None, + }) + .await + } + #[allow(clippy::too_many_arguments)] /// Gets all entitlements for the current app, active and expired. pub async fn get_entitlements( diff --git a/src/http/routing.rs b/src/http/routing.rs index 47ce4331a18..31db50e004f 100644 --- a/src/http/routing.rs +++ b/src/http/routing.rs @@ -490,6 +490,10 @@ routes! ('a, { api!("/applications/{}/entitlements/{}", application_id, entitlement_id), Some(RatelimitingKind::PathAndId(application_id.into())); + ConsumeEntitlement { application_id: ApplicationId, entitlement_id: EntitlementId }, + api!("/applications/{}/entitlements/{}/consume", application_id, entitlement_id), + Some(RatelimitingKind::PathAndId(application_id.into())); + Entitlements { application_id: ApplicationId }, api!("/applications/{}/entitlements", application_id), Some(RatelimitingKind::PathAndId(application_id.into())); diff --git a/src/model/monetization.rs b/src/model/monetization.rs index b2c5c8b6a97..2821dcf0216 100644 --- a/src/model/monetization.rs +++ b/src/model/monetization.rs @@ -1,7 +1,7 @@ #[cfg(feature = "model")] use crate::builder::{Builder as _, GetEntitlements}; #[cfg(feature = "model")] -use crate::http::CacheHttp; +use crate::http::{CacheHttp, Http}; use crate::model::prelude::*; /// A premium offering that can be made available to an application's users and guilds. @@ -46,6 +46,10 @@ enum_number! { #[serde(from = "u8", into = "u8")] #[non_exhaustive] pub enum SkuKind { + /// A durable one-time purchase. + Durable = 2, + /// A consumable one-time purchase. + Consumable = 3, /// Represents a recurring subscription. Subscription = 5, /// A system-generated group for each SKU created of type [`SkuKind::Subscription`]. @@ -98,6 +102,8 @@ pub struct Entitlement { pub ends_at: Option, /// The ID of the guild that is granted access to the SKU. pub guild_id: Option, + /// For consumable items, whether or not the entitlement has been consumed. + pub consumed: Option, } impl Entitlement { @@ -110,6 +116,21 @@ impl Entitlement { ) } + /// For a one-time purchase consumable SKU (of kind [`Consumable`]), marks the entitlement as + /// consumed. On success, the [`consumed`] field will be set to `Some(true)`. + /// + /// # Errors + /// + /// Will fail if the corresponding SKU is not of kind [`Consumable`]. + /// + /// [`Consumable`]: SkuKind::Consumable + /// [`consumed`]: Entitlement::consumed + pub async fn consume(&mut self, http: &Http) -> Result<()> { + http.consume_entitlement(self.id).await?; + self.consumed = Some(true); + Ok(()) + } + /// Returns all entitlements for the current application, active and expired. /// /// # Errors @@ -133,6 +154,20 @@ enum_number! { #[serde(from = "u8", into = "u8")] #[non_exhaustive] pub enum EntitlementKind { + /// Entitlement was purchased by a user. + Purchase = 1, + /// Entitlement for a Discord Nitro subscription. + PremiumSubscription = 2, + /// Entitlement was gifted by an app developer. + DeveloperGift = 3, + /// Entitlement was purchased by a developer in application test mode. + TestModePurchase = 4, + /// Entitlement was granted when the corresponding SKU was free. + FreePurchase = 5, + /// Entitlement was gifted by another user. + UserGift = 6, + /// Entitlement was claimed by user for free as a Nitro Subscriber. + PremiumPurchase = 7, /// Entitlement was purchased as an app subscription. ApplicationSubscription = 8, _ => Unknown(u8),