Skip to content

Commit 709dbcf

Browse files
authored
Add Consume Entitlement endpoint and missing fields for SKUs and Entitlements (#3020)
1 parent a1f2441 commit 709dbcf

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/http/client.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,6 +3445,28 @@ impl Http {
34453445
.await
34463446
}
34473447

3448+
/// For a one-time purchase consumable SKU (of kind [`Consumable`]), marks the entitlement as
3449+
/// consumed.
3450+
///
3451+
/// The entitlement will have its `consumed` field set to `true` when fetched using
3452+
/// [`Self::get_entitlements`].
3453+
///
3454+
/// [`Consumable`]: SkuKind::Consumable
3455+
pub async fn consume_entitlement(&self, entitlement_id: EntitlementId) -> Result<()> {
3456+
self.wind(204, Request {
3457+
body: None,
3458+
multipart: None,
3459+
headers: None,
3460+
method: LightMethod::Post,
3461+
route: Route::ConsumeEntitlement {
3462+
application_id: self.try_application_id()?,
3463+
entitlement_id,
3464+
},
3465+
params: None,
3466+
})
3467+
.await
3468+
}
3469+
34483470
#[allow(clippy::too_many_arguments)]
34493471
/// Gets all entitlements for the current app, active and expired.
34503472
pub async fn get_entitlements(

src/http/routing.rs

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

513+
ConsumeEntitlement { application_id: ApplicationId, entitlement_id: EntitlementId },
514+
api!("/applications/{}/entitlements/{}/consume", application_id, entitlement_id),
515+
Some(RatelimitingKind::PathAndId(application_id.into()));
516+
513517
Entitlements { application_id: ApplicationId },
514518
api!("/applications/{}/entitlements", application_id),
515519
Some(RatelimitingKind::PathAndId(application_id.into()));

src/model/monetization.rs

Lines changed: 37 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,22 @@ 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+
#[cfg(feature = "model")]
129+
pub async fn consume(&mut self, http: &Http) -> Result<()> {
130+
http.consume_entitlement(self.id).await?;
131+
self.consumed = Some(true);
132+
Ok(())
133+
}
134+
113135
/// Returns all entitlements for the current application, active and expired.
114136
///
115137
/// # Errors
@@ -133,6 +155,20 @@ enum_number! {
133155
#[serde(from = "u8", into = "u8")]
134156
#[non_exhaustive]
135157
pub enum EntitlementKind {
158+
/// Entitlement was purchased by a user.
159+
Purchase = 1,
160+
/// Entitlement for a Discord Nitro subscription.
161+
PremiumSubscription = 2,
162+
/// Entitlement was gifted by an app developer.
163+
DeveloperGift = 3,
164+
/// Entitlement was purchased by a developer in application test mode.
165+
TestModePurchase = 4,
166+
/// Entitlement was granted when the corresponding SKU was free.
167+
FreePurchase = 5,
168+
/// Entitlement was gifted by another user.
169+
UserGift = 6,
170+
/// Entitlement was claimed by user for free as a Nitro Subscriber.
171+
PremiumPurchase = 7,
136172
/// Entitlement was purchased as an app subscription.
137173
ApplicationSubscription = 8,
138174
_ => Unknown(u8),

0 commit comments

Comments
 (0)