Skip to main content

twilight_http/request/guild/
create_guild_prune.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{self, AuditLogReason, Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::{
11    guild::GuildPrune,
12    id::{
13        Id,
14        marker::{GuildMarker, RoleMarker},
15    },
16};
17use twilight_validate::request::{
18    ValidationError, audit_reason as validate_audit_reason,
19    guild_prune_days as validate_guild_prune_days,
20};
21
22struct CreateGuildPruneFields<'a> {
23    compute_prune_count: Option<bool>,
24    days: Option<u16>,
25    include_roles: &'a [Id<RoleMarker>],
26}
27
28/// Begin a guild prune.
29///
30/// See [Discord Docs/Begin Guild Prune].
31///
32/// [Discord Docs/Begin Guild Prune]: https://discord.com/developers/docs/resources/guild#begin-guild-prune
33#[must_use = "requests must be configured and executed"]
34pub struct CreateGuildPrune<'a> {
35    fields: Result<CreateGuildPruneFields<'a>, ValidationError>,
36    guild_id: Id<GuildMarker>,
37    http: &'a Client,
38    reason: Result<Option<&'a str>, ValidationError>,
39}
40
41impl<'a> CreateGuildPrune<'a> {
42    pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
43        Self {
44            fields: Ok(CreateGuildPruneFields {
45                compute_prune_count: None,
46                days: None,
47                include_roles: &[],
48            }),
49            guild_id,
50            http,
51            reason: Ok(None),
52        }
53    }
54
55    /// List of roles to include when pruning.
56    pub const fn include_roles(mut self, roles: &'a [Id<RoleMarker>]) -> Self {
57        if let Ok(fields) = self.fields.as_mut() {
58            fields.include_roles = roles;
59        }
60
61        self
62    }
63
64    /// Return the amount of pruned members. Discouraged for large guilds.
65    pub const fn compute_prune_count(mut self, compute_prune_count: bool) -> Self {
66        if let Ok(fields) = self.fields.as_mut() {
67            fields.compute_prune_count = Some(compute_prune_count);
68        }
69
70        self
71    }
72
73    /// Set the number of days that a user must be inactive before being pruned.
74    ///
75    /// The number of days must be greater than 0.
76    ///
77    /// # Errors
78    ///
79    /// Returns an error of type [`GuildPruneDays`] if the number of days is 0
80    /// or more than 30.
81    ///
82    /// [`GuildPruneDays`]: twilight_validate::request::ValidationErrorType::GuildPruneDays
83    pub fn days(mut self, days: u16) -> Self {
84        self.fields = self.fields.and_then(|mut fields| {
85            validate_guild_prune_days(days)?;
86            fields.days = Some(days);
87
88            Ok(fields)
89        });
90
91        self
92    }
93}
94
95impl<'a> AuditLogReason<'a> for CreateGuildPrune<'a> {
96    fn reason(mut self, reason: &'a str) -> Self {
97        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
98
99        self
100    }
101}
102
103#[cfg(not(target_os = "wasi"))]
104impl IntoFuture for CreateGuildPrune<'_> {
105    type Output = Result<Response<GuildPrune>, Error>;
106
107    type IntoFuture = ResponseFuture<GuildPrune>;
108
109    fn into_future(self) -> Self::IntoFuture {
110        let http = self.http;
111
112        match self.try_into_request() {
113            Ok(request) => http.request(request),
114            Err(source) => ResponseFuture::error(source),
115        }
116    }
117}
118
119impl TryIntoRequest for CreateGuildPrune<'_> {
120    fn try_into_request(self) -> Result<Request, Error> {
121        let fields = self.fields.map_err(Error::validation)?;
122        let mut request = Request::builder(&Route::CreateGuildPrune {
123            compute_prune_count: fields.compute_prune_count,
124            days: fields.days,
125            guild_id: self.guild_id.get(),
126            include_roles: fields.include_roles,
127        });
128
129        if let Some(reason) = self.reason.map_err(Error::validation)? {
130            request = request.headers(request::audit_header(reason)?);
131        }
132
133        request.build()
134    }
135}