Skip to main content

djs_model/rest/
entitlement.rs

1//! Bodies and queries of the [entitlement endpoints](https://docs.discord.com/developers/resources/entitlement).
2
3use serde::Serialize;
4
5use crate::{
6    entitlement::EntitlementOwnerType,
7    id::{EntitlementId, GenericId, GuildId, SkuId, UserId},
8};
9
10/// The query of [List Entitlements](https://docs.discord.com/developers/resources/entitlement#list-entitlements).
11///
12/// discord.js counterpart: the options of `EntitlementManager#fetch`.
13///
14/// # Examples
15///
16/// ```
17/// use djs_model::{id::UserId, rest::ListEntitlements};
18///
19/// // The active entitlements of one user.
20/// let mut query = ListEntitlements::default();
21/// query.user_id = Some(UserId::new(7));
22/// query.exclude_ended = Some(true);
23/// ```
24#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
25#[non_exhaustive]
26pub struct ListEntitlements {
27    /// Only the entitlements of this user.
28    pub user_id: Option<UserId>,
29    /// Only the entitlements to these SKUs (sent comma-separated; none means all).
30    pub sku_ids: Vec<SkuId>,
31    /// Entitlements before this one.
32    pub before: Option<EntitlementId>,
33    /// Entitlements after this one.
34    pub after: Option<EntitlementId>,
35    /// How many entitlements (1-100; Discord's default is 100).
36    pub limit: Option<u8>,
37    /// Only the entitlements of this guild.
38    pub guild_id: Option<GuildId>,
39    /// Whether to leave out entitlements that have ended (Discord's default: `false`).
40    pub exclude_ended: Option<bool>,
41    /// Whether to leave out deleted entitlements (Discord's default: `true`).
42    pub exclude_deleted: Option<bool>,
43}
44
45/// The body of [Create Test Entitlement](https://docs.discord.com/developers/resources/entitlement#create-test-entitlement).
46///
47/// Built with [`guild`](Self::guild) or [`user`](Self::user), which set the owner and its
48/// type (D-130).
49///
50/// discord.js counterpart: the argument of `EntitlementManager#createTest`.
51///
52/// # Examples
53///
54/// ```
55/// use djs_model::{
56///     id::{GuildId, SkuId},
57///     rest::CreateTestEntitlement,
58/// };
59///
60/// let body = CreateTestEntitlement::guild(SkuId::new(10), GuildId::new(20));
61/// assert_eq!(
62///     serde_json::to_string(&body)?,
63///     r#"{"sku_id":"10","owner_id":"20","owner_type":1}"#
64/// );
65/// # Ok::<(), serde_json::Error>(())
66/// ```
67#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
68#[non_exhaustive]
69pub struct CreateTestEntitlement {
70    /// The SKU to grant access to.
71    pub sku_id: SkuId,
72    /// The guild or user that gets access.
73    pub owner_id: GenericId,
74    /// Whether [`owner_id`](Self::owner_id) is a guild or a user.
75    pub owner_type: EntitlementOwnerType,
76}
77
78impl CreateTestEntitlement {
79    /// A test entitlement granting `guild_id` access to a guild subscription SKU.
80    ///
81    /// discord.js counterpart: `EntitlementManager#createTest` with `GuildEntitlementCreateOptions`.
82    #[must_use]
83    pub const fn guild(sku_id: SkuId, guild_id: GuildId) -> Self {
84        Self {
85            sku_id,
86            owner_id: guild_id.cast(),
87            owner_type: EntitlementOwnerType::Guild,
88        }
89    }
90
91    /// A test entitlement granting `user_id` access to a user subscription SKU.
92    ///
93    /// discord.js counterpart: `EntitlementManager#createTest` with `UserEntitlementCreateOptions`.
94    #[must_use]
95    pub const fn user(sku_id: SkuId, user_id: UserId) -> Self {
96        Self {
97            sku_id,
98            owner_id: user_id.cast(),
99            owner_type: EntitlementOwnerType::User,
100        }
101    }
102}