aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-07-08 00:43:09 +0200
committeracdenisSK <[email protected]>2017-07-08 00:43:09 +0200
commit420f9bdaa5a5022ff1d769f1d44a689a6fea12a4 (patch)
tree03a2e6002dfd626a8827cc43d24fa49263c107a5 /src
parentActually, use `unreachable!` instead of `panic!` (diff)
downloadserenity-420f9bdaa5a5022ff1d769f1d44a689a6fea12a4.tar.xz
serenity-420f9bdaa5a5022ff1d769f1d44a689a6fea12a4.zip
Implement attaching reasons to bans
Diffstat (limited to 'src')
-rw-r--r--src/http/mod.rs7
-rw-r--r--src/model/guild/guild_id.rs28
-rw-r--r--src/model/guild/member.rs46
3 files changed, 71 insertions, 10 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs
index 1ab2441..382f264 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 08538b1..68212c7 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<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.
diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs
index 444d4af..ebf99d0 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<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 {
@@ -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.