spectacles_model/
user.rs

1use crate::Snowflake;
2
3#[derive(Clone, Debug, Default, Deserialize, Serialize)]
4/// Represents a User on Discord.
5pub struct User {
6    /// The Snowflake ID of this user.
7    pub id: Snowflake,
8    /// The username of this user.
9    pub username: String,
10    /// The four-digit number following the user's username.
11    pub discriminator: String,
12    /// The user's avatar hash, if they have one.
13    pub avatar: Option<String>,
14    /// Whether or not this user is a bot.
15    #[serde(default)]
16    pub bot: bool,
17    /// Whether or not this user has two factor authentication on their account.
18    #[serde(default)]
19    pub mfa_enabled: bool,
20    /// The user's email. Only available on user accounts.
21    #[serde(default)]
22    pub email: Option<String>
23}
24
25impl User {
26    pub fn get_avatar_url(&self, format: &str) -> String {
27        if let Some(ref h) = self.avatar {
28            format!("https://cdn.discordapp.com/avatars/{}/{}.{}", self.id.0, h, format)
29        } else {
30            let avatars = vec![
31                "6debd47ed13483642cf09e832ed0bc1b",
32                "322c936a8c8be1b803cd94861bdfa868",
33                "dd4dbc0016779df1378e7812eabaa04d",
34                "0e291f67c9274a1abdddeb3fd919cbaa",
35                "1cbd08c76f8af6dddce02c5138971129",
36            ];
37            let hash = avatars[self.discriminator.parse::<usize>().unwrap() % avatars.len()];
38
39            format!("https://cdn.discordapp.com/avatars/{}/{}.{}", self.id.0, hash, format)
40        }
41    }
42}
43
44impl ToString for User {
45    fn to_string(&self) -> String {
46        format!("<@{}>", self.id.0)
47    }
48}