diff options
| author | Austin Hellyer <[email protected]> | 2017-01-08 19:57:08 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2017-01-08 19:57:08 -0800 |
| commit | e85e901062e8b9ea717ec6c6253c9c7a300448d3 (patch) | |
| tree | 0c1dea105cf99c06699d8cea8c8999a77ed7d26d /src/model/user.rs | |
| parent | Simplify Reaction::delete() (diff) | |
| download | serenity-e85e901062e8b9ea717ec6c6253c9c7a300448d3.tar.xz serenity-e85e901062e8b9ea717ec6c6253c9c7a300448d3.zip | |
Add User::default_avatar_url() method
Diffstat (limited to 'src/model/user.rs')
| -rw-r--r-- | src/model/user.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/model/user.rs b/src/model/user.rs index d0b2f10..b1a1f16 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -2,6 +2,7 @@ use std::fmt; use super::utils::{into_map, into_string, remove}; use super::{ CurrentUser, + DefaultAvatar, FriendSourceFlags, GuildContainer, GuildId, @@ -139,6 +140,36 @@ impl User { self.id.created_at() } + /// Returns the formatted URL to the user's default avatar URL. + /// + /// This will produce a PNG URL. + /// + /// # Errors + /// + /// Returns an [`Error::Num`] if there was an error parsing the + /// discriminator. Theoretically this is not possible. + /// + /// Returns an [`Error::Other`] if the remainder of the calculation + /// `discriminator % 5` can not be matched. This is also probably not going + /// to occur. + /// + /// [`Error::Num`]: ../enum.Error.html#variant.Num + /// [`Error::Other`]: ../enum.Error.html#variant.Other + pub fn default_avatar_url(&self) -> Result<String> { + Ok(base!("/assets/{}.png", match self.discriminator.parse::<u16>()? % 5u16 { + 0 => DefaultAvatar::Blurple.name().to_owned(), + 1 => DefaultAvatar::Grey.name().to_owned(), + 2 => DefaultAvatar::Green.name().to_owned(), + 3 => DefaultAvatar::Orange.name().to_owned(), + 4 => DefaultAvatar::Red.name().to_owned(), + other => { + error!("Reached impossible default avatar match: {}", other); + + return Err(Error::Other("Reached impossible default match")); + }, + })) + } + /// Send a direct message to a user. This will create or retrieve the /// PrivateChannel over REST if one is not already in the cache, and then /// send a message to it. |