twilight_http/request/guild/ban/
get_bans.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::{
    client::Client,
    error::Error,
    request::{Request, TryIntoRequest},
    response::{marker::ListBody, Response, ResponseFuture},
    routing::Route,
};
use std::future::IntoFuture;
use twilight_model::{
    guild::Ban,
    id::{
        marker::{GuildMarker, UserMarker},
        Id,
    },
};
use twilight_validate::request::{
    get_guild_bans_limit as validate_get_guild_bans_limit, ValidationError,
};

struct GetBansFields {
    after: Option<Id<UserMarker>>,
    before: Option<Id<UserMarker>>,
    limit: Option<u16>,
}

/// Retrieve the bans for a guild.
///
/// # Examples
///
/// Retrieve the first 25 bans of a guild after a particular user ID:
///
/// ```no_run
/// use std::env;
/// use twilight_http::Client;
/// use twilight_model::id::Id;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new(env::var("DISCORD_TOKEN")?);
///
/// let guild_id = Id::new(1);
/// let user_id = Id::new(2);
///
/// let response = client.bans(guild_id).after(user_id).limit(25).await?;
/// let bans = response.models().await?;
///
/// for ban in bans {
///     println!("{} was banned for: {:?}", ban.user.name, ban.reason);
/// }
/// # Ok(()) }
/// ```
#[must_use = "requests must be configured and executed"]
pub struct GetBans<'a> {
    fields: Result<GetBansFields, ValidationError>,
    guild_id: Id<GuildMarker>,
    http: &'a Client,
}

impl<'a> GetBans<'a> {
    pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
        Self {
            fields: Ok(GetBansFields {
                after: None,
                before: None,
                limit: None,
            }),
            guild_id,
            http,
        }
    }

    /// Set the user ID after which to retrieve bans.
    ///
    /// Mutually exclusive with [`before`]. If both are provided then [`before`]
    /// is respected.
    ///
    /// [`before`]: Self::before
    pub fn after(mut self, user_id: Id<UserMarker>) -> Self {
        if let Ok(fields) = self.fields.as_mut() {
            fields.after = Some(user_id);
        }

        self
    }

    /// Set the user ID before which to retrieve bans.
    ///
    /// Mutually exclusive with [`after`]. If both are provided then [`before`]
    /// is respected.
    ///
    /// [`after`]: Self::after
    /// [`before`]: Self::before
    pub fn before(mut self, user_id: Id<UserMarker>) -> Self {
        if let Ok(fields) = self.fields.as_mut() {
            fields.before = Some(user_id);
        }

        self
    }

    /// Set the maximum number of bans to retrieve.
    ///
    /// Defaults to Discord's default.
    ///
    /// Refer to [Discord Docs/Get Guild Bans] for more information.
    ///
    /// # Errors
    ///
    /// Returns an error of type [`GetGuildBans`] if the limit is invalid.
    ///
    /// [`GetGuildBans`]: twilight_validate::request::ValidationErrorType::GetGuildBans
    pub fn limit(mut self, limit: u16) -> Self {
        self.fields = self.fields.and_then(|mut fields| {
            validate_get_guild_bans_limit(limit)?;
            fields.limit.replace(limit);

            Ok(fields)
        });

        self
    }
}

impl IntoFuture for GetBans<'_> {
    type Output = Result<Response<ListBody<Ban>>, Error>;

    type IntoFuture = ResponseFuture<ListBody<Ban>>;

    fn into_future(self) -> Self::IntoFuture {
        let http = self.http;

        match self.try_into_request() {
            Ok(request) => http.request(request),
            Err(source) => ResponseFuture::error(source),
        }
    }
}

impl TryIntoRequest for GetBans<'_> {
    fn try_into_request(self) -> Result<Request, Error> {
        let fields = self.fields.map_err(Error::validation)?;

        Ok(Request::from_route(&Route::GetBansWithParameters {
            after: fields.after.map(Id::get),
            before: fields.before.map(Id::get),
            limit: fields.limit,
            guild_id: self.guild_id.get(),
        }))
    }
}