Skip to main content

twilight_http/request/guild/
get_guild_prune_count.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{Request, TryIntoRequest},
5    response::{Response, ResponseFuture},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::{
10    guild::GuildPrune,
11    id::{
12        Id,
13        marker::{GuildMarker, RoleMarker},
14    },
15};
16use twilight_validate::request::{ValidationError, guild_prune_days as validate_guild_prune_days};
17
18struct GetGuildPruneCountFields<'a> {
19    days: Option<u16>,
20    include_roles: &'a [Id<RoleMarker>],
21}
22
23/// Get the counts of guild members to be pruned.
24///
25/// # Permissions
26///
27/// If the target guild has the
28/// [`PruneRequiresAdmin`][`GuildFeature::PruneRequiresAdmin`] feature enabled
29/// then this requires the [`ADMINISTRATOR`][`Permissions::ADMINISTRATOR`] guild
30/// permission; otherwise, this requires both the
31/// [`MANAGE_GUILD`][`Permissions::MANAGE_GUILD`] and
32/// [`KICK_MEMBERS`][`Permissions::KICK_MEMBERS`] permissions.
33///
34/// [`GuildFeature::PruneRequiresAdmin`]: twilight_model::guild::GuildFeature::PruneRequiresAdmin
35/// [`Permissions::ADMINISTRATOR`]: twilight_model::guild::Permissions::ADMINISTRATOR
36/// [`Permissions::KICK_MEMBERS`]: twilight_model::guild::Permissions::KICK_MEMBERS
37/// [`Permissions::MANAGE_GUILD`]: twilight_model::guild::Permissions::MANAGE_GUILD
38#[must_use = "requests must be configured and executed"]
39pub struct GetGuildPruneCount<'a> {
40    fields: Result<GetGuildPruneCountFields<'a>, ValidationError>,
41    guild_id: Id<GuildMarker>,
42    http: &'a Client,
43}
44
45impl<'a> GetGuildPruneCount<'a> {
46    pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
47        Self {
48            fields: Ok(GetGuildPruneCountFields {
49                days: None,
50                include_roles: &[],
51            }),
52            guild_id,
53            http,
54        }
55    }
56
57    /// Set the number of days that a user must be inactive before being
58    /// able to be pruned.
59    ///
60    /// The number of days must be greater than 0, and less than or equal to 30.
61    ///
62    /// # Errors
63    ///
64    /// Returns an error of type [`GuildPruneDays`] if the number of days is 0
65    /// or more than 30.
66    ///
67    /// [`GuildPruneDays`]: twilight_validate::request::ValidationErrorType::GuildPruneDays
68    pub fn days(mut self, days: u16) -> Self {
69        self.fields = self.fields.and_then(|mut fields| {
70            validate_guild_prune_days(days)?;
71            fields.days = Some(days);
72
73            Ok(fields)
74        });
75
76        self
77    }
78
79    /// List of roles to include when calculating prune count
80    pub const fn include_roles(mut self, roles: &'a [Id<RoleMarker>]) -> Self {
81        if let Ok(fields) = self.fields.as_mut() {
82            fields.include_roles = roles;
83        }
84
85        self
86    }
87}
88
89impl IntoFuture for GetGuildPruneCount<'_> {
90    type Output = Result<Response<GuildPrune>, Error>;
91
92    type IntoFuture = ResponseFuture<GuildPrune>;
93
94    fn into_future(self) -> Self::IntoFuture {
95        let http = self.http;
96
97        match self.try_into_request() {
98            Ok(request) => http.request(request),
99            Err(source) => ResponseFuture::error(source),
100        }
101    }
102}
103
104impl TryIntoRequest for GetGuildPruneCount<'_> {
105    fn try_into_request(self) -> Result<Request, Error> {
106        let fields = self.fields.map_err(Error::validation)?;
107
108        Ok(Request::from_route(&Route::GetGuildPruneCount {
109            days: fields.days,
110            guild_id: self.guild_id.get(),
111            include_roles: fields.include_roles,
112        }))
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::GetGuildPruneCount;
119    use crate::{Client, request::TryIntoRequest};
120    use twilight_model::id::Id;
121
122    #[test]
123    fn days() {
124        fn days_valid(days: u16) -> bool {
125            let client = Client::new(String::new());
126
127            GetGuildPruneCount::new(&client, Id::new(1))
128                .days(days)
129                .try_into_request()
130                .is_ok()
131        }
132
133        assert!(!days_valid(0));
134        assert!(days_valid(1));
135        assert!(!days_valid(u16::MAX));
136    }
137}