aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-05-02 22:27:42 -0700
committerZeyla Hellyer <[email protected]>2017-05-02 22:27:42 -0700
commit83b1d967f4cc2040f94d67dd987302347f227d6a (patch)
tree4bdeb48fd8a15ddd874f9c16a1ab90b3cbb78dd9 /src
parentReturn preset from Member::find_guild if possible (diff)
downloadserenity-83b1d967f4cc2040f94d67dd987302347f227d6a.tar.xz
serenity-83b1d967f4cc2040f94d67dd987302347f227d6a.zip
Add missing Member::kick shortcut
A shortcut for kicking a member directly was missing, so add it in. This method performs permission checking.
Diffstat (limited to 'src')
-rw-r--r--src/model/guild/member.rs63
1 files changed, 62 insertions, 1 deletions
diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs
index 5da7d7c..ddb0822 100644
--- a/src/model/guild/member.rs
+++ b/src/model/guild/member.rs
@@ -89,7 +89,7 @@ impl Member {
/// Ban the member from its guild, deleting the last X number of
/// days' worth of messages.
///
- /// **Note**: Requires the [Ban Members] role.
+ /// **Note**: Requires the [Ban Members] permission.
///
/// # Errors
///
@@ -196,6 +196,67 @@ impl Member {
Err(Error::Client(ClientError::GuildNotFound))
}
+ /// Kick the member from the guild.
+ ///
+ /// **Note**: Requires the [Kick Members] permission.
+ ///
+ /// # Examples
+ ///
+ /// Kick a member from its guild:
+ ///
+ /// ```rust,ignore
+ /// // assuming a `member` has already been bound
+ /// match member.kick() {
+ /// Ok(()) => println!("Successfully kicked member"),
+ /// Err(Error::Client(ClientError::GuildNotFound)) => {
+ /// println!("Couldn't determine guild of member");
+ /// },
+ /// Err(Error::Client(ClientError::InvalidPermissions(missing_perms))) => {
+ /// println!("Didn't have permissions; missing: {:?}", missing_perms);
+ /// },
+ /// _ => {},
+ /// }
+ /// ```
+ ///
+ /// # Errors
+ ///
+ /// Returns a [`ClientError::GuildNotFound`] if the Id of the member's guild
+ /// could not be determined.
+ ///
+ /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`]
+ /// if the current user does not have permission to perform the kick.
+ ///
+ /// [`ClientError::GuildNotFound`]: ../client/enum.ClientError.html#variant.GuildNotFound
+ /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions
+ /// [Kick Members]: permissions/constant.KICK_MEMBERS.html
+ pub fn kick(&self) -> Result<()> {
+ let guild_id;
+
+ #[cfg(feature="cache")]
+ {
+ guild_id = self.find_guild()?;
+ let req = permissions::KICK_MEMBERS;
+
+ let has_perms = CACHE
+ .read()
+ .unwrap()
+ .guilds
+ .get(&guild_id)
+ .map(|guild| guild.read().unwrap().has_perms(req));
+
+ if let Some(Ok(false)) = has_perms {
+ return Err(Error::Client(ClientError::InvalidPermissions(req)));
+ }
+ }
+
+ #[cfg(not(feature="cache"))]
+ {
+ guild_id = self.guild_id.ok_or_else(|| Error::Client(ClientError::GuildNotFound))?;
+ }
+
+ guild_id.kick(self.user.read().unwrap().id)
+ }
+
/// Removes a [`Role`] from the member, editing its roles in-place if the
/// request was successful.
///