Skip to content

Commit

Permalink
Replace Delete-Messages-Days with Delete-Messages-Seconds (serenity-r…
Browse files Browse the repository at this point in the history
…s#3088)

This replaces the time unit that determines how many messages in a
period of X time should be deleted when a user is banned. Discord
expresses this time in seconds, whereas Serenity exposed it in days. The
limit is still 7 days, but with this, users of Serenity can be more
precise with which messages they want to delete.
  • Loading branch information
cheesycod authored and arqunis committed Feb 14, 2025
1 parent 025eacb commit 09bea56
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 66 deletions.
10 changes: 4 additions & 6 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,17 @@ impl Http {
}

/// Bans a [`User`] from a [`Guild`], removing their messages sent in the last X number of
/// days.
/// seconds.
///
/// Passing a `delete_message_days` of `0` is equivalent to not removing any messages. Up to
/// `7` days' worth of messages may be deleted.
/// Passing a `delete_message_seconds` of `0` is equivalent to not removing any messages. Up to
/// `604800` seconds (or 7 days) worth of messages may be deleted.
pub async fn ban_user(
&self,
guild_id: GuildId,
user_id: UserId,
delete_message_days: u8,
delete_message_seconds: u32,
reason: Option<&str>,
) -> Result<()> {
let delete_message_seconds = u32::from(delete_message_days) * 86400;

self.wind(Request {
body: None,
multipart: None,
Expand Down
40 changes: 0 additions & 40 deletions src/model/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub enum Maximum {
StickerCount,
WebhookName,
AuditLogReason,
DeleteMessageDays,
BulkDeleteAmount,
}

Expand All @@ -38,7 +37,6 @@ impl Maximum {
Self::StickerCount => crate::constants::STICKER_MAX_COUNT,
Self::WebhookName | Self::BulkDeleteAmount => 100,
Self::AuditLogReason => 512,
Self::DeleteMessageDays => 7,
}
}
}
Expand All @@ -52,7 +50,6 @@ impl fmt::Display for Maximum {
Self::StickerCount => f.write_str("Sticker count"),
Self::WebhookName => f.write_str("Webhook name"),
Self::AuditLogReason => f.write_str("Audit log reason"),
Self::DeleteMessageDays => f.write_str("Delete message days"),
Self::BulkDeleteAmount => f.write_str("Message bulk delete count"),
}
}
Expand Down Expand Up @@ -100,44 +97,7 @@ impl fmt::Display for Minimum {
///
/// This is always wrapped within the library's [`Error::Model`] variant.
///
/// # Examples
///
/// Matching an [`Error`] with this variant would look something like the following for the
/// [`GuildId::ban`] method, which in this example is used to re-ban all members.
///
/// ```rust,no_run
/// use serenity::model::prelude::*;
/// use serenity::model::ModelError;
/// use serenity::prelude::*;
/// use serenity::Error;
///
/// # #[cfg(feature = "http")]
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: serenity::http::Http = unimplemented!();
/// # let guild_id: GuildId = unimplemented!();
/// # let user: User = unimplemented!();
///
/// match guild_id.ban(&http, user.id, 8, Some("No unbanning people!")).await {
/// Ok(()) => {
/// // Ban successful.
/// },
/// Err(Error::Model(ModelError::TooLarge {
/// value, ..
/// })) => {
/// println!("Failed deleting {value} days' worth of messages");
/// },
/// Err(why) => {
/// println!("Unexpected error: {why:?}");
/// },
/// }
///
/// # Ok(())
/// # }
/// ```
///
/// [`Error`]: crate::Error
/// [`Error::Model`]: crate::Error::Model
/// [`GuildId::ban`]: super::id::GuildId::ban
/// [`model`]: crate::model
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
Expand Down
29 changes: 15 additions & 14 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ impl GuildId {
builder.execute(http, self, user_id).await
}

/// Ban a [`User`] from the guild, deleting a number of days' worth of messages (`dmd`) between
/// the range 0 and 7.
/// Ban a [`User`] from the guild, deleting `dms` seconds worth of messages from them.
/// `dms` should be between 0 and 604800 (subject to change). To ban someone for X days,
/// multiply X by 86400.
///
/// **Note**: Requires the [Ban Members] permission.
///
Expand All @@ -173,31 +174,31 @@ impl GuildId {
/// ```rust,no_run
/// use serenity::model::id::{GuildId, UserId};
///
/// const FOUR_DAYS_IN_SECONDS: u32 = 4 * 24 * 60 * 60;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # use serenity::http::Http;
/// # let http: Http = unimplemented!();
/// # let user = UserId::new(1);
/// // assuming a `user` has already been bound
/// let _ = GuildId::new(81384788765712384).ban(&http, user, 4, None).await;
/// let _ = GuildId::new(81384788765712384).ban(&http, user, FOUR_DAYS_IN_SECONDS, None).await;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns a [`ModelError::TooLarge`] if the number of days' worth of messages
/// to delete is over the maximum.
///
/// Also can return [`Error::Http`] if the current user lacks permission.
/// Can return [`Error::Http`] if the current user lacks permission.
///
/// [Ban Members]: Permissions::BAN_MEMBERS
pub async fn ban(self, http: &Http, user: UserId, dmd: u8, reason: Option<&str>) -> Result<()> {
Maximum::DeleteMessageDays.check_overflow(dmd.into())?;
if let Some(reason) = reason {
Maximum::AuditLogReason.check_overflow(reason.len())?;
}

http.ban_user(self, user, dmd, reason).await
pub async fn ban(
self,
http: &Http,
user: UserId,
dms: u32,
reason: Option<&str>,
) -> Result<()> {
http.ban_user(self, user, dms, reason).await
}

/// Bans multiple users from the guild, returning the users that were and weren't banned, and
Expand Down
11 changes: 5 additions & 6 deletions src/model/guild/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,18 @@ impl Member {
Ok(())
}

/// Ban a [`User`] from the guild, deleting a number of days' worth of messages (`dmd`) between
/// the range 0 and 7.
/// Ban a [`User`] from the guild, deleting `dms` seconds worth of messages from them.
/// `dms` should be between 0 and 604800. To ban someone for X days, multiply X by 86400.
///
/// **Note**: Requires the [Ban Members] permission.
///
/// # Errors
///
/// Returns a [`ModelError::TooLarge`] if the `dmd` is greater than 7. Can also
/// return [`Error::Http`] if the current user lacks permission to ban this member.
/// Can return [`Error::Http`] if the current user lacks permission to ban this member.
///
/// [Ban Members]: Permissions::BAN_MEMBERS
pub async fn ban(&self, http: &Http, dmd: u8, audit_log_reason: Option<&str>) -> Result<()> {
self.guild_id.ban(http, self.user.id, dmd, audit_log_reason).await
pub async fn ban(&self, http: &Http, dms: u32, audit_log_reason: Option<&str>) -> Result<()> {
self.guild_id.ban(http, self.user.id, dms, audit_log_reason).await
}

/// Determines the member's colour.
Expand Down

0 comments on commit 09bea56

Please sign in to comment.