Skip to main content

djs_rest/endpoints/
entitlement.rs

1//! [Entitlement endpoints](https://docs.discord.com/developers/resources/entitlement): which
2//! users and guilds have access to an app's SKUs, consuming one-time purchases and test
3//! entitlements.
4
5use djs_model::{
6    entitlement::Entitlement,
7    id::{ApplicationId, EntitlementId},
8    rest::{CreateTestEntitlement, ListEntitlements},
9};
10
11use crate::client::boxed;
12use crate::{Error, Request, Rest, Route};
13
14impl Rest {
15    /// [List Entitlements](https://docs.discord.com/developers/resources/entitlement#list-entitlements):
16    /// returns a page of the application's entitlements, active and ended (100 by default).
17    ///
18    /// discord.js counterpart: `EntitlementManager#fetch`.
19    ///
20    /// # Examples
21    ///
22    /// ```no_run
23    /// # async fn example(rest: djs_rest::Rest, application_id: djs_model::id::ApplicationId, user_id: djs_model::id::UserId) -> Result<(), djs_rest::Error> {
24    /// use djs_model::{rest::ListEntitlements, util::Timestamp};
25    ///
26    /// let mut query = ListEntitlements::default();
27    /// query.user_id = Some(user_id);
28    /// query.exclude_ended = Some(true);
29    /// let entitlements = rest.list_entitlements(application_id, &query).await?;
30    /// let premium = entitlements
31    ///     .iter()
32    ///     .any(|entitlement| entitlement.is_active(Timestamp::now()));
33    /// # Ok(())
34    /// # }
35    /// ```
36    ///
37    /// # Errors
38    ///
39    /// See [`Rest::request`].
40    pub fn list_entitlements<'a>(
41        &'a self,
42        application_id: ApplicationId,
43        query: &'a ListEntitlements,
44    ) -> impl Future<Output = Result<Vec<Entitlement>, Error>> + Send + 'a {
45        boxed(async move {
46            self.call(Request::new(Route::ListEntitlements { application_id }).query(query))
47                .await
48        })
49    }
50
51    /// [Get Entitlement](https://docs.discord.com/developers/resources/entitlement#get-entitlement):
52    /// returns one entitlement.
53    ///
54    /// discord.js counterpart: none.
55    ///
56    /// # Errors
57    ///
58    /// See [`Rest::request`]; `10029` Unknown Entitlement.
59    pub fn get_entitlement(
60        &self,
61        application_id: ApplicationId,
62        entitlement_id: EntitlementId,
63    ) -> impl Future<Output = Result<Entitlement, Error>> + Send + '_ {
64        boxed(async move {
65            self.call(Request::new(Route::GetEntitlement {
66                application_id,
67                entitlement_id,
68            }))
69            .await
70        })
71    }
72
73    /// [Consume an Entitlement](https://docs.discord.com/developers/resources/entitlement#consume-an-entitlement):
74    /// marks the entitlement to a consumable one-time purchase as used; it then has
75    /// [`consumed`](Entitlement::consumed) set.
76    ///
77    /// discord.js counterpart: `Entitlement#consume`, `EntitlementManager#consume`.
78    ///
79    /// # Errors
80    ///
81    /// See [`Rest::request`]; `10029` Unknown Entitlement; `40018` for an entitlement to an
82    /// SKU that is not consumable.
83    pub fn consume_entitlement(
84        &self,
85        application_id: ApplicationId,
86        entitlement_id: EntitlementId,
87    ) -> impl Future<Output = Result<(), Error>> + Send + '_ {
88        boxed(async move {
89            self.call(Request::new(Route::ConsumeEntitlement {
90                application_id,
91                entitlement_id,
92            }))
93            .await
94        })
95    }
96
97    /// [Create Test Entitlement](https://docs.discord.com/developers/resources/entitlement#create-test-entitlement):
98    /// grants a guild or user access to an SKU for testing, without an end. Discord returns
99    /// it without [`starts_at`](Entitlement::starts_at) and
100    /// [`ends_at`](Entitlement::ends_at).
101    ///
102    /// discord.js counterpart: `EntitlementManager#createTest`.
103    ///
104    /// # Examples
105    ///
106    /// ```no_run
107    /// # async fn example(rest: djs_rest::Rest, application_id: djs_model::id::ApplicationId, sku_id: djs_model::id::SkuId, guild_id: djs_model::id::GuildId) -> Result<(), djs_rest::Error> {
108    /// use djs_model::rest::CreateTestEntitlement;
109    ///
110    /// let body = CreateTestEntitlement::guild(sku_id, guild_id);
111    /// let entitlement = rest.create_test_entitlement(application_id, &body).await?;
112    /// println!("Test entitlement {}", entitlement.id);
113    /// # Ok(())
114    /// # }
115    /// ```
116    ///
117    /// # Errors
118    ///
119    /// See [`Rest::request`]; `40074` when the owner already has the entitlement.
120    pub fn create_test_entitlement<'a>(
121        &'a self,
122        application_id: ApplicationId,
123        body: &'a CreateTestEntitlement,
124    ) -> impl Future<Output = Result<Entitlement, Error>> + Send + 'a {
125        boxed(async move {
126            self.call(Request::new(Route::CreateTestEntitlement { application_id }).json(body)?)
127                .await
128        })
129    }
130
131    /// [Delete Test Entitlement](https://docs.discord.com/developers/resources/entitlement#delete-test-entitlement):
132    /// removes a test entitlement.
133    ///
134    /// discord.js counterpart: `EntitlementManager#deleteTest`.
135    ///
136    /// # Errors
137    ///
138    /// See [`Rest::request`]; `10029` Unknown Entitlement; `40019` for an entitlement that is
139    /// not a test entitlement.
140    pub fn delete_test_entitlement(
141        &self,
142        application_id: ApplicationId,
143        entitlement_id: EntitlementId,
144    ) -> impl Future<Output = Result<(), Error>> + Send + '_ {
145        boxed(async move {
146            self.call(Request::new(Route::DeleteTestEntitlement {
147                application_id,
148                entitlement_id,
149            }))
150            .await
151        })
152    }
153}