forked from serenity-rs/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add builder for
Http::get_entitlements
(serenity-rs#2987)
Closes serenity-rs#2983.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#[cfg(feature = "http")] | ||
use crate::http::CacheHttp; | ||
use crate::internal::prelude::Result; | ||
use crate::model::id::{EntitlementId, GuildId, SkuId, UserId}; | ||
use crate::model::monetization::Entitlement; | ||
|
||
/// Builds a request to fetch active and ended [`Entitlement`]s. | ||
/// | ||
/// This is a helper for [`Http::get_entitlements`] used via [`Entitlement::list`]. | ||
/// | ||
/// [`Http::get_entitlements`]: crate::http::Http::get_entitlements | ||
#[derive(Clone, Debug, Default)] | ||
#[must_use] | ||
pub struct GetEntitlements { | ||
user_id: Option<UserId>, | ||
sku_ids: Option<Vec<SkuId>>, | ||
before: Option<EntitlementId>, | ||
after: Option<EntitlementId>, | ||
limit: Option<u8>, | ||
guild_id: Option<GuildId>, | ||
exclude_ended: Option<bool>, | ||
} | ||
|
||
impl GetEntitlements { | ||
/// Filters the returned entitlements by the given [`UserId`]. | ||
pub fn user_id(mut self, user_id: UserId) -> Self { | ||
self.user_id = Some(user_id); | ||
self | ||
} | ||
|
||
/// Filters the returned entitlements by the given [`SkuId`]s. | ||
pub fn sku_ids(mut self, sku_ids: Vec<SkuId>) -> Self { | ||
self.sku_ids = Some(sku_ids); | ||
self | ||
} | ||
|
||
/// Filters the returned entitlements to before the given [`EntitlementId`]. | ||
pub fn before(mut self, before: EntitlementId) -> Self { | ||
self.before = Some(before); | ||
self | ||
} | ||
|
||
/// Filters the returned entitlements to after the given [`EntitlementId`]. | ||
pub fn after(mut self, after: EntitlementId) -> Self { | ||
self.after = Some(after); | ||
self | ||
} | ||
|
||
/// Limits the number of entitlements that may be returned. | ||
/// | ||
/// This is limited to `0..=100`. | ||
pub fn limit(mut self, limit: u8) -> Self { | ||
self.limit = Some(limit); | ||
self | ||
} | ||
|
||
/// Filters the returned entitlements by the given [`GuildId`]. | ||
pub fn guild_id(mut self, guild_id: GuildId) -> Self { | ||
self.guild_id = Some(guild_id); | ||
self | ||
} | ||
|
||
/// Filters the returned entitlements to only active entitlements, if `true`. | ||
pub fn exclude_ended(mut self, exclude_ended: bool) -> Self { | ||
self.exclude_ended = Some(exclude_ended); | ||
self | ||
} | ||
} | ||
|
||
#[cfg(feature = "http")] | ||
#[async_trait::async_trait] | ||
impl super::Builder for GetEntitlements { | ||
type Context<'ctx> = (); | ||
type Built = Vec<Entitlement>; | ||
|
||
async fn execute( | ||
self, | ||
cache_http: impl CacheHttp, | ||
_: Self::Context<'_>, | ||
) -> Result<Self::Built> { | ||
let http = cache_http.http(); | ||
http.get_entitlements( | ||
self.user_id, | ||
self.sku_ids, | ||
self.before, | ||
self.after, | ||
self.limit, | ||
self.guild_id, | ||
self.exclude_ended, | ||
) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters