twilight_http/request/scheduled_event/
get_guild_scheduled_event_users.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
151
use crate::{
    client::Client,
    error::Error,
    request::{Request, TryIntoRequest},
    response::{marker::ListBody, Response, ResponseFuture},
    routing::Route,
};
use std::future::IntoFuture;
use twilight_model::{
    guild::scheduled_event::GuildScheduledEventUser,
    id::{
        marker::{GuildMarker, ScheduledEventMarker, UserMarker},
        Id,
    },
};
use twilight_validate::request::{
    scheduled_event_get_users as validate_scheduled_event_get_users, ValidationError,
};

struct GetGuildScheduledEventUsersFields {
    after: Option<Id<UserMarker>>,
    before: Option<Id<UserMarker>>,
    limit: Option<u16>,
    scheduled_event_id: Id<ScheduledEventMarker>,
    with_member: Option<bool>,
}

/// Get a list of users subscribed to a scheduled event.
///
/// Users are returned in ascending order by `user_id`. [`before`] and [`after`]
/// both take a user id. If both are specified, only [`before`] is respected.
/// The default [`limit`] is 100. See
/// [Discord Docs/Get Guild Scheduled Event Users].
///
/// [`after`]: GetGuildScheduledEventUsers::after
/// [`before`]: GetGuildScheduledEventUsers::before
/// [`limit`]: GetGuildScheduledEventUsers::limit
/// [Discord Docs/Get Guild Scheduled Event Users]: https://discord.com/developers/docs/resources/guild-scheduled-event#get-guild-scheduled-event-users
#[must_use = "requests must be configured and executed"]
pub struct GetGuildScheduledEventUsers<'a> {
    fields: Result<GetGuildScheduledEventUsersFields, ValidationError>,
    guild_id: Id<GuildMarker>,
    http: &'a Client,
}

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

    /// Get users after this user ID.
    ///
    /// This is incompatible with [`before`], and has no effect if [`before`] is
    /// also set.
    ///
    /// [`before`]: Self::before
    pub fn after(mut self, after: Id<UserMarker>) -> Self {
        if let Ok(fields) = self.fields.as_mut() {
            fields.after = Some(after);
        }

        self
    }

    /// Get users before this user ID.
    ///
    /// This is incompatible with [`after`].
    ///
    /// [`after`]: Self::after
    pub fn before(mut self, before: Id<UserMarker>) -> Self {
        if let Ok(fields) = self.fields.as_mut() {
            fields.before = Some(before);
        }

        self
    }

    /// Set the limit of users to return.
    ///
    /// If not specified, the default is 100.
    ///
    /// # Errors
    ///
    /// Returns an error of type [`ScheduledEventGetUsers`] if the limit is
    /// invalid.
    ///
    /// [`ScheduledEventGetUsers`]: twilight_validate::request::ValidationErrorType::ScheduledEventGetUsers
    pub fn limit(mut self, limit: u16) -> Self {
        self.fields = self.fields.and_then(|mut fields| {
            validate_scheduled_event_get_users(limit)?;
            fields.limit = Some(limit);

            Ok(fields)
        });

        self
    }

    /// Set whether to return member objects with each user.
    pub fn with_member(mut self, with_member: bool) -> Self {
        if let Ok(fields) = self.fields.as_mut() {
            fields.with_member = Some(with_member);
        }

        self
    }
}

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

    type IntoFuture = ResponseFuture<ListBody<GuildScheduledEventUser>>;

    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 GetGuildScheduledEventUsers<'_> {
    fn try_into_request(self) -> Result<Request, Error> {
        let fields = self.fields.map_err(Error::validation)?;

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