Skip to content

Commit d217139

Browse files
committed
Add Consume Entitlement endpoint and missing fields for SKUs and Entitlements
1 parent 77ac985 commit d217139

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/http/client.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,28 @@ impl Http {
33953395
.await
33963396
}
33973397

3398+
/// For a one-time purchase consumable SKU (of kind [`Consumable`]), marks the entitlement as
3399+
/// consumed.
3400+
///
3401+
/// The entitlement will have its `consumed` field set to `true` when fetched using
3402+
/// [`Self::get_entitlements`].
3403+
///
3404+
/// [`Consumable`]: SkuKind::Consumable
3405+
pub async fn consume_entitlement(&self, entitlement_id: EntitlementId) -> Result<()> {
3406+
self.wind(204, Request {
3407+
body: None,
3408+
multipart: None,
3409+
headers: None,
3410+
method: LightMethod::Post,
3411+
route: Route::ConsumeEntitlement {
3412+
application_id: self.try_application_id()?,
3413+
entitlement_id,
3414+
},
3415+
params: None,
3416+
})
3417+
.await
3418+
}
3419+
33983420
#[allow(clippy::too_many_arguments)]
33993421
/// Gets all entitlements for the current app, active and expired.
34003422
pub async fn get_entitlements(

src/http/routing.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ routes! ('a, {
490490
api!("/applications/{}/entitlements/{}", application_id, entitlement_id),
491491
Some(RatelimitingKind::PathAndId(application_id.into()));
492492

493+
ConsumeEntitlement { application_id: ApplicationId, entitlement_id: EntitlementId },
494+
api!("/applications/{}/entitlements/{}/consume", application_id, entitlement_id),
495+
Some(RatelimitingKind::PathAndId(application_id.into()));
496+
493497
Entitlements { application_id: ApplicationId },
494498
api!("/applications/{}/entitlements", application_id),
495499
Some(RatelimitingKind::PathAndId(application_id.into()));

src/model/monetization.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "model")]
22
use crate::builder::{Builder as _, GetEntitlements};
33
#[cfg(feature = "model")]
4-
use crate::http::CacheHttp;
4+
use crate::http::{CacheHttp, Http};
55
use crate::model::prelude::*;
66

77
/// A premium offering that can be made available to an application's users and guilds.
@@ -46,6 +46,10 @@ enum_number! {
4646
#[serde(from = "u8", into = "u8")]
4747
#[non_exhaustive]
4848
pub enum SkuKind {
49+
/// A durable one-time purchase.
50+
Durable = 2,
51+
/// A consumable one-time purchase.
52+
Consumable = 3,
4953
/// Represents a recurring subscription.
5054
Subscription = 5,
5155
/// A system-generated group for each SKU created of type [`SkuKind::Subscription`].
@@ -98,6 +102,8 @@ pub struct Entitlement {
98102
pub ends_at: Option<Timestamp>,
99103
/// The ID of the guild that is granted access to the SKU.
100104
pub guild_id: Option<GuildId>,
105+
/// For consumable items, whether or not the entitlement has been consumed.
106+
pub consumed: Option<bool>,
101107
}
102108

103109
impl Entitlement {
@@ -110,6 +116,21 @@ impl Entitlement {
110116
)
111117
}
112118

119+
/// For a one-time purchase consumable SKU (of kind [`Consumable`]), marks the entitlement as
120+
/// consumed. On success, the [`consumed`] field will be set to `Some(true)`.
121+
///
122+
/// # Errors
123+
///
124+
/// Will fail if the corresponding SKU is not of kind [`Consumable`].
125+
///
126+
/// [`Consumable`]: SkuKind::Consumable
127+
/// [`consumed`]: Entitlement::consumed
128+
pub async fn consume(&mut self, http: &Http) -> Result<()> {
129+
http.consume_entitlement(self.id).await?;
130+
self.consumed = Some(true);
131+
Ok(())
132+
}
133+
113134
/// Returns all entitlements for the current application, active and expired.
114135
///
115136
/// # Errors
@@ -133,6 +154,20 @@ enum_number! {
133154
#[serde(from = "u8", into = "u8")]
134155
#[non_exhaustive]
135156
pub enum EntitlementKind {
157+
/// Entitlement was purchased by a user.
158+
Purchase = 1,
159+
/// Entitlement for a Discord Nitro subscription.
160+
PremiumSubscription = 2,
161+
/// Entitlement was gifted by an app developer.
162+
DeveloperGift = 3,
163+
/// Entitlement was purchased by a developer in application test mode.
164+
TestModePurchase = 4,
165+
/// Entitlement was granted when the corresponding SKU was free.
166+
FreePurchase = 5,
167+
/// Entitlement was gifted by another user.
168+
UserGift = 6,
169+
/// Entitlement was claimed by user for free as a Nitro Subscriber.
170+
PremiumPurchase = 7,
136171
/// Entitlement was purchased as an app subscription.
137172
ApplicationSubscription = 8,
138173
_ => Unknown(u8),

0 commit comments

Comments
 (0)