Skip to content

Commit

Permalink
Adjust the ban methods to be consistent with the kick methods (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis authored May 6, 2020
1 parent 5edd11d commit 8628b2a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/model/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// },
Expand Down
36 changes: 22 additions & 14 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
///
Expand Down Expand Up @@ -50,15 +48,25 @@ impl GuildId {
/// [Ban Members]: ../permissions/struct.Permissions.html#associatedconstant.BAN_MEMBERS
#[cfg(feature = "http")]
#[inline]
pub fn ban<U, BO>(self, http: impl AsRef<Http>, user: U, ban_options: &BO) -> Result<()>
where U: Into<UserId>, BO: BanOptions {
self._ban(&http, user.into(), (ban_options.dmd(), ban_options.reason()))
pub fn ban(self, http: impl AsRef<Http>, user: impl Into<UserId>, 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<Http>, user: UserId, ban_options: (u8, &str)) -> Result<()> {
let (dmd, reason) = ban_options;
#[inline]
pub fn ban_with_reason(self, http: impl AsRef<Http>,
user: impl Into<UserId>,
dmd: u8,
reason: impl AsRef<str>) -> Result<()> {
self._ban_with_reason(http, user.into(), dmd, reason.as_ref())
}

#[cfg(feature = "http")]
fn _ban_with_reason(self, http: impl AsRef<Http>, user: UserId, dmd: u8, reason: &str) -> Result<()> {
if dmd > 7 {
return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd)));
}
Expand All @@ -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<Http>) -> Result<Vec<Ban>> {http.as_ref().get_bans(self.0) }
pub fn bans(self, http: impl AsRef<Http>) -> Result<Vec<Ban>> { 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<Http>,
action_type: Option<u8>,
user_id: Option<UserId>,
before: Option<AuditLogEntryId>,
limit: Option<u8>) -> Result<AuditLogs> {
action_type: Option<u8>,
user_id: Option<UserId>,
before: Option<AuditLogEntryId>,
limit: Option<u8>) -> Result<AuditLogs> {
http.as_ref().get_audit_logs(self.0, action_type, user_id.map(|u| u.0), before.map(|a| a.0), limit)
}

Expand Down
67 changes: 12 additions & 55 deletions src/model/guild/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
///
Expand All @@ -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<BO: BanOptions>(&self, http: impl AsRef<Http>, ban_options: &BO) -> Result<()> {
self._ban(&http, ban_options.dmd(), ban_options.reason())
pub fn ban(&self, http: impl AsRef<Http>, dmd: u8) -> Result<()> {
self.ban_with_reason(&http, dmd, "")
}

#[cfg(all(feature = "cache", feature = "http"))]
fn _ban(&self, http: impl AsRef<Http>, 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<Http>, dmd: u8, reason: impl AsRef<str>) -> Result<()> {
self.guild_id.ban_with_reason(http, self.user.read().id, dmd, reason)
}

/// Determines the member's colour.
Expand Down
27 changes: 20 additions & 7 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -273,12 +273,25 @@ impl Guild {
/// [Ban Members]: ../permissions/struct.Permissions.html#associatedconstant.BAN_MEMBERS
#[cfg(feature = "client")]
#[inline]
pub fn ban<U: Into<UserId>, 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<UserId>, 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<UserId>,
dmd: u8,
reason: impl AsRef<str>) -> Result<()> {
self._ban_with_reason(cache_http, user.into(), dmd, reason.as_ref())
}

#[cfg(feature = "client")]
fn _ban<BO: BanOptions>(&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() {
Expand All @@ -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.
Expand Down Expand Up @@ -1869,7 +1882,7 @@ impl<'de> Deserialize<'de> for Guild {
Some(v) => Option::<String>::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)?;
Expand Down
27 changes: 17 additions & 10 deletions src/model/guild/partial_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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<U: Into<UserId>>(&self, http: impl AsRef<Http>, 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<Http>, user: impl Into<UserId>, 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<Http>,
user: impl Into<UserId>,
dmd: u8,
reason: impl AsRef<str>) -> Result<()> {
self.id.ban_with_reason(&http, user, dmd, reason)
}

/// Gets a list of the guild's bans.
Expand Down

0 comments on commit 8628b2a

Please sign in to comment.