aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2017-01-08 19:57:08 -0800
committerAustin Hellyer <[email protected]>2017-01-08 19:57:08 -0800
commite85e901062e8b9ea717ec6c6253c9c7a300448d3 (patch)
tree0c1dea105cf99c06699d8cea8c8999a77ed7d26d /src
parentSimplify Reaction::delete() (diff)
downloadserenity-e85e901062e8b9ea717ec6c6253c9c7a300448d3.tar.xz
serenity-e85e901062e8b9ea717ec6c6253c9c7a300448d3.zip
Add User::default_avatar_url() method
Diffstat (limited to 'src')
-rw-r--r--src/error.rs10
-rw-r--r--src/model/user.rs31
-rw-r--r--src/utils/macros.rs10
3 files changed, 51 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
index 41bda57..6e9f4f1 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -4,6 +4,7 @@ use serde_json::Value;
use std::io::Error as IoError;
use std::error::Error as StdError;
use std::fmt::{self, Display, Error as FormatError};
+use std::num::ParseIntError;
use websocket::result::WebSocketError;
use ::client::gateway::GatewayError;
use ::client::ClientError;
@@ -55,6 +56,8 @@ pub enum Error {
Io(IoError),
/// An error from the `serde_json` crate.
Json(JsonError),
+ /// An error occurred while parsing an integer.
+ Num(ParseIntError),
/// Some other error. This is only used for "Expected value <TYPE>" errors,
/// when a more detailed error can not be easily provided via the
/// [`Error::Decode`] variant.
@@ -99,6 +102,12 @@ impl From<JsonError> for Error {
}
}
+impl From<ParseIntError> for Error {
+ fn from(e: ParseIntError) -> Error {
+ Error::Num(e)
+ }
+}
+
#[cfg(feature="voice")]
impl From<OpusError> for Error {
fn from(e: OpusError) -> Error {
@@ -136,6 +145,7 @@ impl StdError for Error {
Error::Hyper(ref inner) => inner.description(),
Error::Io(ref inner) => inner.description(),
Error::Json(ref inner) => inner.description(),
+ Error::Num(ref inner) => inner.description(),
Error::Url(ref inner) => inner,
Error::WebSocket(ref inner) => inner.description(),
#[cfg(feature="voice")]
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.
diff --git a/src/utils/macros.rs b/src/utils/macros.rs
index 51e016d..3032dc3 100644
--- a/src/utils/macros.rs
+++ b/src/utils/macros.rs
@@ -28,6 +28,16 @@ macro_rules! cdn {
concat!("https://cdn.discordapp.com", $e)
}
}
+
+macro_rules! base {
+ ($e:expr) => {
+ concat!("https://discordapp.com", $e)
+ };
+ ($e:expr, $($rest:tt)*) => {
+ format!(base!($e), $($rest)*)
+ };
+}
+
macro_rules! api {
($e:expr) => {
concat!("https://discordapp.com/api/v6", $e)