Skip to main content

twilight_http/request/guild/
create_guild_prune.rs

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