Skip to main content

djs_model/
subscription.rs

1//! [Subscriptions](https://docs.discord.com/developers/resources/subscription): a user's
2//! recurring payment for one of an app's subscription [SKUs](crate::sku).
3
4use compact_str::CompactString;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8    id::{EntitlementId, SkuId, SubscriptionId, UserId},
9    macros::int_enum,
10    util::Timestamp,
11};
12
13/// A [subscription](https://docs.discord.com/developers/resources/subscription#subscription-object).
14///
15/// Use the [`Entitlement`](crate::entitlement::Entitlement)s it grants to decide access;
16/// the subscription only describes the billing period.
17///
18/// discord.js counterpart: `Subscription`.
19#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
20#[non_exhaustive]
21pub struct Subscription {
22    /// The subscription's ID.
23    pub id: SubscriptionId,
24    /// The subscribed user.
25    pub user_id: UserId,
26    /// The SKUs subscribed to.
27    pub sku_ids: Vec<SkuId>,
28    /// The entitlements granted by the subscription.
29    pub entitlement_ids: Vec<EntitlementId>,
30    /// The SKUs the user will be subscribed to at renewal (`None` if unchanged).
31    #[serde(default)]
32    pub renewal_sku_ids: Option<Vec<SkuId>>,
33    /// The start of the current period.
34    pub current_period_start: Timestamp,
35    /// The end of the current period.
36    pub current_period_end: Timestamp,
37    /// The subscription's status.
38    pub status: SubscriptionStatus,
39    /// When the subscription was canceled.
40    #[serde(default)]
41    pub canceled_at: Option<Timestamp>,
42    /// The ISO 3166-1 alpha-2 country code of the payment source (only with a private
43    /// `OAuth2` scope).
44    #[serde(default, skip_serializing_if = "Option::is_none")]
45    pub country: Option<CompactString>,
46}
47
48int_enum! {
49    /// The [status of a subscription](https://docs.discord.com/developers/resources/subscription#subscription-statuses).
50    ///
51    /// discord.js counterpart: `SubscriptionStatus`.
52    pub enum SubscriptionStatus: u8 {
53        /// Active and scheduled to renew.
54        Active = 0,
55        /// Inactive and not being charged.
56        Inactive = 1,
57        /// Active but will not renew.
58        Ending = 2,
59    }
60}