From 39a28d3bf5d7005c3549a09542d27c08660f49cb Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 14 Jun 2017 18:27:16 +0200 Subject: [PATCH] Add `Member::permissions` --- src/error.rs | 6 ++++++ src/model/guild/member.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/error.rs b/src/error.rs index 6aa65e7fb1d..a9484d514b3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -139,6 +139,12 @@ impl From for Error { } } +impl From for Error { + fn from(e: ModelError) -> Error { + Error::Model(e) + } +} + #[cfg(feature="voice")] impl From for Error { fn from(e: OpusError) -> Error { diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 69ba59fe7ff..a9937064f48 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -218,6 +218,33 @@ impl Member { self.guild_id.kick(self.user.read().unwrap().id) } + /// Returns the permissions for the member. + /// + /// # Examples + /// + /// ```rust,ignore + /// // assuming there's a `member` variable gotten from anything. + /// println!("The permission bits for the member are: {}", member.permissions().expect("permissions").bits); + /// ``` + /// + /// # Errors + /// + /// Returns a [`ModelError::GuildNotFound`] if the guild the member's in could not be + /// found in the cache. + /// + /// [`ModelError::GuildNotFound`]: enum.ModelError.html#variant.GuildNotFound + #[cfg(feature="cache")] + pub fn permissions(&self) -> Result { + let guild = match self.guild_id.find() { + Some(guild) => guild, + None => return Err(From::from(ModelError::GuildNotFound)), + }; + + let guild = guild.read().unwrap(); + + Ok(guild.permissions_for(ChannelId(guild.id.0), self.user.read().unwrap().id)) + } + /// Removes a [`Role`] from the member, editing its roles in-place if the /// request was successful. ///