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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// Newtype for bot ids.
#[derive(Debug)]
pub struct BotId(pub u64);
/// Newtype for user ids.
#[derive(Debug)]
pub struct UserId(pub u64);
/// Newtype for guild ids.
#[derive(Debug)]
pub struct GuildId(pub u64);

/// Basic user information returned by [`Client::votes`](../struct.Client.html#method.votes).
#[derive(Debug, Deserialize)]
pub struct User {
    pub id: UserId,
    pub username: String,
    pub discriminator: String,
    pub avatar: Option<String>,
}

/// Detailed user information returned by [`Client::user`](../struct.Client.html#method.user).
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DetailedUser {
    pub id: UserId,
    pub username: String,
    pub discriminator: String,
    pub avatar: Option<String>,
    #[serde(rename = "defAvatar")]
    pub default_avatar: String,
    pub bio: Option<String>,
    pub banner: Option<String>,
    pub social: Social,
    pub color: Option<String>,
    pub supporter: bool,
    pub certified_dev: bool,
    #[serde(rename = "mod")]
    pub mod_: bool,
    pub web_mod: bool,
    pub admin: bool,
}

/// Social media accounts of the user.
#[derive(Debug, Deserialize)]
pub struct Social {
    pub github: String,
    pub instagram: String,
    pub reddit: String,
    pub twitter: String,
    pub youtube: String,
}

/// Information about a bot.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Bot {
    pub id: BotId,
    pub username: String,
    pub discriminator: String,
    pub avatar: Option<String>,
    #[serde(rename = "defAvatar")]
    pub default_avatar: String,
    pub clientid: String,
    pub lib: String,
    pub prefix: String,
    #[serde(rename = "shortdesc")]
    pub short_desc: String,
    #[serde(rename = "longdesc")]
    pub long_desc: Option<String>,
    pub tags: Vec<String>,
    pub website: Option<String>,
    pub support: Option<String>,
    pub github: Option<String>,
    pub owners: Vec<UserId>,
    pub guilds: Vec<GuildId>,
    pub invite: Option<String>,
    pub date: String,
    pub certified_bot: bool,
    pub vanity: Option<String>,
    pub shards: Vec<u64>,
    pub points: u64,
    pub monthly_points: u64,
}

/// Bot's sharding stats.
#[derive(Debug, Deserialize)]
pub struct Stats {
    pub server_count: Option<u64>,
    pub shards: Vec<u64>,
    pub shard_count: Option<u64>,
}

/// Used to update one or more sharding stats.
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum ShardStats {
    Cumulative {
        server_count: u64,
        shard_count: Option<u64>,
    },
    Shard {
        server_count: u64,
        shard_id: u64,
        shard_count: u64,
    },
    Shards {
        shards: Vec<u64>,
    },
}

/// Used for filtering the bot search.
pub struct Filter(pub(crate) HashMap<&'static str, String>);

impl Default for Filter {
    fn default() -> Filter {
        Filter::new()
    }
}

impl Filter {
    pub fn new() -> Filter {
        Filter(HashMap::with_capacity(4))
    }

    pub fn limit(mut self, mut limit: u16) -> Filter {
        if limit > 500 {
            limit = 500;
        }
        self.0.insert("limit", limit.to_string());
        self
    }

    pub fn offset(mut self, offset: u16) -> Filter {
        self.0.insert("offset", offset.to_string());
        self
    }

    pub fn sort<T: AsRef<str>>(mut self, field: T, ascending: bool) -> Filter {
        let mut buf = String::new();
        if !ascending {
            buf.push('-');
        }
        buf.push_str(field.as_ref());
        self.0.insert("sort", buf);
        self
    }

    /// Search string. Example: `lib:serenity mod`
    pub fn search<T: ToString>(mut self, search: T) -> Filter {
        self.0.insert("search", search.to_string());
        self
    }
}

/// Search result returned by [`Client::search`](../struct.Client.html#method.search).
#[derive(Debug, Deserialize)]
pub struct Listing {
    pub results: Vec<Bot>,
    pub limit: u64,
    pub offset: u64,
    pub count: u64,
    pub total: u64,
}

/// Vote received via webhook.
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Webhook {
    pub bot: BotId,
    pub user: UserId,
    #[serde(rename = "type")]
    pub kind: WebhookType,
    pub is_weekend: bool,
    pub query: Option<String>,
}

/// Type of vote received via webhook.
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum WebhookType {
    Upvote,
    Test,
}

impl Webhook {
    pub fn is_test(&self) -> bool {
        match self.kind {
            WebhookType::Test => true,
            _ => false,
        }
    }
}

impl ::std::ops::Index<usize> for Listing {
    type Output = Bot;

    fn index(&self, index: usize) -> &Self::Output {
        &self.results[index]
    }
}

impl IntoIterator for Listing {
    type Item = Bot;
    type IntoIter = ::std::vec::IntoIter<Bot>;

    fn into_iter(self) -> Self::IntoIter {
        self.results.into_iter()
    }
}

impl<'a> IntoIterator for &'a Listing {
    type Item = &'a Bot;
    type IntoIter = ::std::slice::Iter<'a, Bot>;

    fn into_iter(self) -> Self::IntoIter {
        self.results.iter()
    }
}

#[derive(Deserialize)]
pub(crate) struct UserVoted {
    pub voted: u64,
}

#[derive(Deserialize)]
#[serde(rename = "kebab-case")]
pub(crate) struct Ratelimit {
    pub retry_after: u32,
}

macro_rules! impl_snowflake {
    ($($type:ty),*) => {
        $(
            impl $type {
                pub fn as_u64(&self) -> u64 {
                    self.0
                }
            }

            impl ::std::fmt::Display for $type {
                fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                    self.0.fmt(f)
                }
            }

            impl From<u64> for $type {
                fn from(v: u64) -> Self {
                    Self(v)
                }
            }

            impl<'de> ::serde::de::Deserialize<'de> for $type {
                fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
                where
                    D: ::serde::de::Deserializer<'de>,
                {
                    struct Visitor;

                    impl<'de> ::serde::de::Visitor<'de> for Visitor {
                        type Value = $type;

                        fn expecting(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                            f.write_str("identifier")
                        }

                        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                        where
                            E: ::serde::de::Error,
                        {
                            v.parse::<u64>().map(Into::into).map_err(|_| {
                                E::custom(format!("invalid {}: value {}", stringify!(u64), v))
                            })
                        }
                    }

                    deserializer.deserialize_str(Visitor)
                }
            }

            impl ::serde::ser::Serialize for $type {
                fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
                where
                    S: ::serde::ser::Serializer,
                {
                    serializer.serialize_str(&self.0.to_string())
                }
            }
        )*
    };
}

impl_snowflake!(BotId, GuildId, UserId);