diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ext/cache/mod.rs | 6 | ||||
| -rw-r--r-- | src/model/guild.rs | 43 |
2 files changed, 37 insertions, 12 deletions
diff --git a/src/ext/cache/mod.rs b/src/ext/cache/mod.rs index 2778d6b..39baaf0 100644 --- a/src/ext/cache/mod.rs +++ b/src/ext/cache/mod.rs @@ -188,7 +188,7 @@ pub struct Cache { } impl Cache { - /// Calculates the number of [`Member`]s that have not had data received. + /// Fetches the number of [`Member`]s that have not had data received. /// /// The important detail to note here is that this is the number of /// _member_s that have not had data downloaded. A single [`User`] may have @@ -214,7 +214,7 @@ impl Cache { total } - /// Calculates a vector of all [`PrivateChannel`] and [`Group`] Ids that are + /// Fetches a vector of all [`PrivateChannel`] and [`Group`] Ids that are /// stored in the cache. /// /// # Examples @@ -232,7 +232,7 @@ impl Cache { .collect() } - /// Calculates a vector of all [`Guild`]s' Ids that are stored in the cache. + /// Fetches a vector of all [`Guild`]s' Ids that are stored in the cache. /// /// Note that if you are utilizing multiple [`Shard`]s, then the guilds /// retrieved over all shards are included in this count -- not just the diff --git a/src/model/guild.rs b/src/model/guild.rs index 6c5fab2..f9b56dd 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -14,7 +14,7 @@ use super::utils::{ }; use super::*; use ::internal::prelude::*; -use ::utils::decode_array; +use ::utils::{Colour, decode_array}; #[cfg(feature = "methods")] use serde_json::builder::ObjectBuilder; @@ -847,22 +847,47 @@ impl Member { delete_message_days) } - /// Calculates the member's display name. + /// Determines the member's display name. /// /// The nickname takes priority over the member's username if it exists. pub fn display_name(&self) -> &str { self.nick.as_ref().unwrap_or(&self.user.name) } - /// Calculates the member's colour. - pub fn colour(&self) -> Option<::utils::Colour> { - let roles = self.roles.clone(); - for n in &roles { - if let Some(r) = n.find() { - return Some(r.colour); + /// Determines the member's colour. + /// + /// If the member has no role with a colour override - or the member's guild + /// data does not exist in the cache - then the value of [`Colour::default`] + /// is returned. + /// + /// [`Colour::default`]: ../utils/struct.Colour.html#method.default + #[cfg(all(feature = "cache", feature = "methods"))] + pub fn colour(&self) -> Colour { + let default = Colour::default(); + let guild_id = match self.find_guild() { + Ok(guild_id) => guild_id, + Err(_why) => return default, + }; + + let cache = CACHE.read().unwrap(); + let guild = match cache.guilds.get(&guild_id) { + Some(guild) => guild, + None => return default, + }; + + let mut roles = self.roles + .iter() + .filter_map(|id| guild.roles.get(id)) + .collect::<Vec<&Role>>(); + roles.sort_by(|a, b| b.cmp(a)); + + for role in roles { + if role.colour.value != default.value { + return role.colour; } } - None + + default } /// Edits the member with the given data. See [`Context::edit_member`] for |