diff --git a/src/http/mod.rs b/src/http/mod.rs index 1ab244197d8..382f264fb48 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -132,13 +132,14 @@ pub fn add_member_role(guild_id: u64, user_id: u64, role_id: u64) -> Result<()> /// [`Guild`]: ../model/struct.Guild.html /// [`User`]: ../model/struct.User.html /// [Ban Members]: ../model/permissions/constant.BAN_MEMBERS.html -pub fn ban_user(guild_id: u64, user_id: u64, delete_message_days: u8) -> Result<()> { +pub fn ban_user(guild_id: u64, user_id: u64, delete_message_days: u8, reason: &str) -> Result<()> { verify(204, request!(Route::GuildsIdBansUserId(guild_id), put, - "/guilds/{}/bans/{}?delete_message_days={}", + "/guilds/{}/bans/{}?delete_message_days={}&reason={}", guild_id, user_id, - delete_message_days)) + delete_message_days, + reason)) } /// Broadcasts that the current user is typing in the given [`Channel`]. diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 08538b1e761..68212c7a517 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -1,5 +1,6 @@ use std::fmt::{Display, Formatter, Result as FmtResult}; use ::model::*; +use ::model::guild::BanOptions; #[cfg(feature="cache")] use ::CACHE; @@ -45,13 +46,30 @@ impl GuildId { /// [`Guild::ban`]: struct.Guild.html#method.ban /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html - pub fn ban>(&self, user: U, delete_message_days: u8) + pub fn ban, BO: Into>(&self, user: U, ban_options: BO) -> Result<()> { - if delete_message_days > 7 { - return Err(Error::Model(ModelError::DeleteMessageDaysAmount(delete_message_days))); + + use self::BanOptions::*; + + match ban_options.into() { + DeleteMessageDays(dmd) => { + if dmd > 7 { + return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); + } + + http::ban_user(self.0, user.into().0, dmd, "") + }, + DMDReason(dmd, reason) => { + if dmd > 7 { + return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); + } + + http::ban_user(self.0, user.into().0, dmd, &*reason) + }, + Reason(reason) => { + http::ban_user(self.0, user.into().0, 0, &*reason) + }, } - - http::ban_user(self.0, user.into().0, delete_message_days) } /// Gets a list of the guild's bans. diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 444d4af128c..ebf99d047ad 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -16,6 +16,36 @@ use ::builder::EditMember; #[cfg(all(feature="cache", feature="model", feature="utils"))] use ::utils::Colour; +pub enum BanOptions { + DeleteMessageDays(u8), + DMDReason(u8, String), + Reason(String), +} + +impl From for BanOptions { + fn from(dmd: u8) -> Self { + BanOptions::DeleteMessageDays(dmd) + } +} + +impl<'a> From<&'a str> for BanOptions { + fn from(reason: &'a str) -> Self { + BanOptions::Reason(reason.to_string()) + } +} + +impl From for BanOptions { + fn from(reason: String) -> Self { + BanOptions::Reason(reason) + } +} + +impl<'a> From<(u8, &'a str)> for BanOptions { + fn from(dmdreason: (u8, &'a str)) -> Self { + BanOptions::DMDReason(dmdreason.0, dmdreason.1.to_string()) + } +} + /// Information about a member of a guild. #[derive(Clone, Debug, Deserialize)] pub struct Member { @@ -102,8 +132,20 @@ impl Member { /// /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[cfg(feature="cache")] - pub fn ban(&self, delete_message_days: u8) -> Result<()> { - http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, delete_message_days) + pub fn ban>(&self, ban_options: BO) -> Result<()> { + use self::BanOptions::*; + + match ban_options.into() { + DeleteMessageDays(dmd) => { + http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, dmd, "") + }, + DMDReason(dmd, reason) => { + http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, dmd, &*reason) + }, + Reason(reason) => { + http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, 0, &*reason) + }, + } } /// Determines the member's colour.