djs_model/entitlement.rs
1//! [Entitlements](https://docs.discord.com/developers/resources/entitlement): a user's or
2//! guild's access to an app's premium offering (an [SKU](crate::sku)).
3
4use serde::{Deserialize, Serialize};
5
6use crate::{
7 id::{ApplicationId, EntitlementId, GuildId, SkuId, UserId},
8 macros::int_enum,
9 util::Timestamp,
10};
11
12/// An [entitlement](https://docs.discord.com/developers/resources/entitlement#entitlement-object).
13///
14/// discord.js counterpart: `Entitlement`.
15#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
16#[non_exhaustive]
17pub struct Entitlement {
18 /// The entitlement's ID.
19 pub id: EntitlementId,
20 /// The SKU it grants access to.
21 pub sku_id: SkuId,
22 /// The application the SKU belongs to.
23 pub application_id: ApplicationId,
24 /// The user who has access, for user subscriptions and items.
25 #[serde(default, skip_serializing_if = "Option::is_none")]
26 pub user_id: Option<UserId>,
27 /// How the entitlement was obtained.
28 #[serde(rename = "type")]
29 pub kind: EntitlementType,
30 /// Whether the entitlement was deleted.
31 pub deleted: bool,
32 /// When the entitlement becomes valid (`None` for test entitlements).
33 pub starts_at: Option<Timestamp>,
34 /// When the entitlement stops being valid (`None` for test entitlements).
35 pub ends_at: Option<Timestamp>,
36 /// The guild that has access, for guild subscriptions.
37 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub guild_id: Option<GuildId>,
39 /// For consumable items: whether the entitlement was consumed.
40 #[serde(default, skip_serializing_if = "core::ops::Not::not")]
41 pub consumed: bool,
42}
43
44impl Entitlement {
45 /// Whether the entitlement grants access at `now`: not deleted, not consumed, and within
46 /// its validity period (open ends count as unbounded).
47 ///
48 /// discord.js counterpart: `Entitlement#isActive`.
49 #[must_use]
50 pub fn is_active(&self, now: Timestamp) -> bool {
51 !self.deleted
52 && !self.consumed
53 && self.starts_at.is_none_or(|starts_at| starts_at <= now)
54 && self.ends_at.is_none_or(|ends_at| now < ends_at)
55 }
56}
57
58int_enum! {
59 /// [How an entitlement was obtained](https://docs.discord.com/developers/resources/entitlement#entitlement-object-entitlement-types).
60 ///
61 /// discord.js counterpart: `EntitlementType`.
62 pub enum EntitlementType: u8 {
63 /// Purchased by the user.
64 Purchase = 1,
65 /// From a Discord Nitro subscription.
66 PremiumSubscription = 2,
67 /// Gifted by the developer.
68 DeveloperGift = 3,
69 /// Purchased by a developer in the application's test mode.
70 TestModePurchase = 4,
71 /// Granted when the SKU was free.
72 FreePurchase = 5,
73 /// Gifted by another user.
74 UserGift = 6,
75 /// Claimed for free by a Nitro subscriber.
76 PremiumPurchase = 7,
77 /// Purchased as an app subscription.
78 ApplicationSubscription = 8,
79 }
80}
81
82int_enum! {
83 /// Who a [test entitlement](crate::rest::CreateTestEntitlement) is granted to.
84 ///
85 /// discord.js counterpart: `EntitlementOwnerType`.
86 pub enum EntitlementOwnerType: u8 {
87 /// A guild (for guild subscriptions).
88 Guild = 1,
89 /// A user (for user subscriptions).
90 User = 2,
91 }
92}