Skip to content

Commit

Permalink
Implement attaching reasons to bans
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Jul 7, 2017
1 parent aa307b1 commit 420f9bd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
28 changes: 23 additions & 5 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::{Display, Formatter, Result as FmtResult};
use ::model::*;
use ::model::guild::BanOptions;

#[cfg(feature="cache")]
use ::CACHE;
Expand Down Expand Up @@ -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<U: Into<UserId>>(&self, user: U, delete_message_days: u8)
pub fn ban<U: Into<UserId>, BO: Into<BanOptions>>(&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.
Expand Down
46 changes: 44 additions & 2 deletions src/model/guild/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> 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<String> 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 {
Expand Down Expand Up @@ -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<BO: Into<BanOptions>>(&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.
Expand Down

0 comments on commit 420f9bd

Please sign in to comment.