From 8628b2ae80e0b74030afbcd80fc5b1f87599e977 Mon Sep 17 00:00:00 2001 From: "Alex M. M" Date: Wed, 6 May 2020 12:44:02 +0200 Subject: [PATCH] Adjust the ban methods to be consistent with the kick methods (#852) --- src/model/error.rs | 2 +- src/model/guild/guild_id.rs | 36 ++++++++++------- src/model/guild/member.rs | 67 ++++++-------------------------- src/model/guild/mod.rs | 27 +++++++++---- src/model/guild/partial_guild.rs | 27 ++++++++----- 5 files changed, 72 insertions(+), 87 deletions(-) diff --git a/src/model/error.rs b/src/model/error.rs index ebc1260c0c2..3886cbcaa31 100644 --- a/src/model/error.rs +++ b/src/model/error.rs @@ -41,7 +41,7 @@ use super::Permissions; /// return; /// } /// -/// match guild_id.ban(&context.http, user, &8) { +/// match guild_id.ban(&context.http, user, 7) { /// Ok(()) => { /// // Ban successful. /// }, diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 1a735bd9beb..b9f86968bf1 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -9,8 +9,6 @@ use crate::builder::{EditGuild, EditMember, EditRole}; #[cfg(feature = "model")] use crate::internal::prelude::*; #[cfg(feature = "model")] -use crate::model::guild::BanOptions; -#[cfg(feature = "model")] use crate::utils; #[cfg(feature = "http")] use crate::http::Http; @@ -21,8 +19,8 @@ use serde_json::json; #[cfg(feature = "model")] impl GuildId { - /// Ban a [`User`] from the guild. All messages by the - /// user within the last given number of days given will be deleted. + /// Ban a [`User`] from the guild, deleting a number of + /// days' worth of messages (`dmd`) between the range 0 and 7. /// /// Refer to the documentation for [`Guild::ban`] for more information. /// @@ -50,15 +48,25 @@ impl GuildId { /// [Ban Members]: ../permissions/struct.Permissions.html#associatedconstant.BAN_MEMBERS #[cfg(feature = "http")] #[inline] - pub fn ban(self, http: impl AsRef, user: U, ban_options: &BO) -> Result<()> - where U: Into, BO: BanOptions { - self._ban(&http, user.into(), (ban_options.dmd(), ban_options.reason())) + pub fn ban(self, http: impl AsRef, user: impl Into, dmd: u8) -> Result<()> { + self._ban_with_reason(http, user.into(), dmd, "") } + /// Ban a [`User`] from the guild with a reason. Refer to [`ban`] to further documentation. + /// + /// [`User`]: ../user/struct.User.html + /// [`ban`]: #method.ban #[cfg(feature = "http")] - fn _ban(self, http: impl AsRef, user: UserId, ban_options: (u8, &str)) -> Result<()> { - let (dmd, reason) = ban_options; + #[inline] + pub fn ban_with_reason(self, http: impl AsRef, + user: impl Into, + dmd: u8, + reason: impl AsRef) -> Result<()> { + self._ban_with_reason(http, user.into(), dmd, reason.as_ref()) + } + #[cfg(feature = "http")] + fn _ban_with_reason(self, http: impl AsRef, user: UserId, dmd: u8, reason: &str) -> Result<()> { if dmd > 7 { return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); } @@ -77,16 +85,16 @@ impl GuildId { /// [Ban Members]: ../permissions/struct.Permissions.html#associatedconstant.BAN_MEMBERS #[cfg(feature = "http")] #[inline] - pub fn bans(self, http: impl AsRef) -> Result> {http.as_ref().get_bans(self.0) } + pub fn bans(self, http: impl AsRef) -> Result> { http.as_ref().get_bans(self.0) } /// Gets a list of the guild's audit log entries #[cfg(feature = "http")] #[inline] pub fn audit_logs(self, http: impl AsRef, - action_type: Option, - user_id: Option, - before: Option, - limit: Option) -> Result { + action_type: Option, + user_id: Option, + before: Option, + limit: Option) -> Result { http.as_ref().get_audit_logs(self.0, action_type, user_id.map(|u| u.0), before.map(|a| a.0), limit) } diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 19debc4de8e..8b79df8a821 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -22,40 +22,6 @@ use crate::{cache::CacheRwLock, utils}; #[cfg(all(feature = "http", feature = "cache"))] use crate::http::Http; -/// A trait for allowing both u8 or &str or (u8, &str) to be passed into the `ban` methods in `Guild` and `Member`. -pub trait BanOptions { - fn dmd(&self) -> u8 { 0 } - fn reason(&self) -> &str { "" } -} - -impl BanOptions for u8 { - fn dmd(&self) -> u8 { *self } -} - -impl BanOptions for str { - fn reason(&self) -> &str { self } -} - -impl<'a> BanOptions for &'a str { - fn reason(&self) -> &str { self } -} - -impl BanOptions for String { - fn reason(&self) -> &str { self } -} - -impl<'a> BanOptions for (u8, &'a str) { - fn dmd(&self) -> u8 { self.0 } - - fn reason(&self) -> &str { self.1 } -} - -impl BanOptions for (u8, String) { - fn dmd(&self) -> u8 { self.0 } - - fn reason(&self) -> &str { &self.1 } -} - /// Information about a member of a guild. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Member { @@ -137,8 +103,8 @@ impl Member { } } - /// Ban the member from its guild, deleting the last X number of - /// days' worth of messages. + /// Ban a [`User`] from the guild, deleting a number of + /// days' worth of messages (`dmd`) between the range 0 and 7. /// /// **Note**: Requires the [Ban Members] permission. /// @@ -149,28 +115,19 @@ impl Member { /// /// [`ModelError::GuildNotFound`]: ../error/enum.Error.html#variant.GuildNotFound /// [Ban Members]: ../permissions/struct.Permissions.html#associatedconstant.BAN_MEMBERS - #[cfg(all(feature = "cache", feature = "http"))] + #[cfg(all(feature = "http", feature = "cache"))] #[inline] - pub fn ban(&self, http: impl AsRef, ban_options: &BO) -> Result<()> { - self._ban(&http, ban_options.dmd(), ban_options.reason()) + pub fn ban(&self, http: impl AsRef, dmd: u8) -> Result<()> { + self.ban_with_reason(&http, dmd, "") } - #[cfg(all(feature = "cache", feature = "http"))] - fn _ban(&self, http: impl AsRef, dmd: u8, reason: &str) -> Result<()> { - if dmd > 7 { - return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); - } - - if reason.len() > 512 { - return Err(Error::ExceededLimit(reason.to_string(), 512)); - } - - http.as_ref().ban_user( - self.guild_id.0, - self.user.read().id.0, - dmd, - &*reason, - ) + /// Ban the member from the guild with a reason. Refer to [`ban`] to further documentation. + /// + /// [`ban`]: #method.ban + #[cfg(all(feature = "http", feature = "cache"))] + #[inline] + pub fn ban_with_reason(&self, http: impl AsRef, dmd: u8, reason: impl AsRef) -> Result<()> { + self.guild_id.ban_with_reason(http, self.user.read().id, dmd, reason) } /// Determines the member's colour. diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 161d6bc08ab..75d5c2fea25 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -242,8 +242,8 @@ impl Guild { }) } - /// Ban a [`User`] from the guild. All messages by the - /// user within the last given number of days given will be deleted. + /// Ban a [`User`] from the guild, deleting a number of + /// days' worth of messages (`dmd`) between the range 0 and 7. /// /// Refer to the documentation for [`Guild::ban`] for more information. /// @@ -273,12 +273,25 @@ impl Guild { /// [Ban Members]: ../permissions/struct.Permissions.html#associatedconstant.BAN_MEMBERS #[cfg(feature = "client")] #[inline] - pub fn ban, BO: BanOptions>(&self, cache_http: impl CacheHttp, user: U, options: &BO) -> Result<()> { - self._ban(cache_http, user.into(), options) + pub fn ban(&self, cache_http: impl CacheHttp, user: impl Into, dmd: u8) -> Result<()> { + self._ban_with_reason(cache_http, user.into(), dmd, "") + } + + /// Ban a [`User`] from the guild with a reason. Refer to [`ban`] to further documentation. + /// + /// [`User`]: ../user/struct.User.html + /// [`ban`]: #method.ban + #[cfg(feature = "client")] + #[inline] + pub fn ban_with_reason(&self, cache_http: impl CacheHttp, + user: impl Into, + dmd: u8, + reason: impl AsRef) -> Result<()> { + self._ban_with_reason(cache_http, user.into(), dmd, reason.as_ref()) } #[cfg(feature = "client")] - fn _ban(&self, cache_http: impl CacheHttp, user: UserId, options: &BO) -> Result<()> { + fn _ban_with_reason(&self, cache_http: impl CacheHttp, user: UserId, dmd: u8, reason: &str) -> Result<()> { #[cfg(feature = "cache")] { if let Some(cache) = cache_http.cache() { @@ -292,7 +305,7 @@ impl Guild { } } - self.id.ban(cache_http.http(), user, options) + self.id.ban_with_reason(cache_http.http(), user, dmd, reason) } /// Retrieves a list of [`Ban`]s for the guild. @@ -1869,7 +1882,7 @@ impl<'de> Deserialize<'de> for Guild { Some(v) => Option::::deserialize(v).map_err(DeError::custom)?, None => None, }; - let preferred_locale = map.remove("preferred_locale") + let preferred_locale = map.remove("preferred_locale") .ok_or_else(|| DeError::custom("expected preferred locale")) .and_then(String::deserialize) .map_err(DeError::custom)?; diff --git a/src/model/guild/partial_guild.rs b/src/model/guild/partial_guild.rs index 3d791d83cdb..92f8e4197bc 100644 --- a/src/model/guild/partial_guild.rs +++ b/src/model/guild/partial_guild.rs @@ -52,9 +52,8 @@ pub struct PartialGuild { #[cfg(feature = "model")] impl PartialGuild { - /// Ban a [`User`] from the guild. All messages by the - /// user within the last given number of days given will be deleted. This - /// may be a range between `0` and `7`. + /// Ban a [`User`] from the guild, deleting a number of + /// days' worth of messages (`dmd`) between the range 0 and 7. /// /// **Note**: Requires the [Ban Members] permission. /// @@ -76,14 +75,22 @@ impl PartialGuild { /// [`User`]: ../user/struct.User.html /// [Ban Members]: ../permissions/struct.Permissions.html#associatedconstant.BAN_MEMBERS #[cfg(feature = "http")] - pub fn ban>(&self, http: impl AsRef, user: U, delete_message_days: u8) -> Result<()> { - if delete_message_days > 7 { - return Err(Error::Model( - ModelError::DeleteMessageDaysAmount(delete_message_days), - )); - } + #[inline] + pub fn ban(&self, http: impl AsRef, user: impl Into, dmd: u8) -> Result<()> { + self.ban_with_reason(&http, user, dmd, "") + } - self.id.ban(&http, user, &delete_message_days) + /// Ban a [`User`] from the guild with a reason. Refer to [`ban`] to further documentation. + /// + /// [`User`]: ../user/struct.User.html + /// [`ban`]: #method.ban + #[cfg(feature = "http")] + #[inline] + pub fn ban_with_reason(&self, http: impl AsRef, + user: impl Into, + dmd: u8, + reason: impl AsRef) -> Result<()> { + self.id.ban_with_reason(&http, user, dmd, reason) } /// Gets a list of the guild's bans.