From 09bea5638cc4c6a2150901c6dfa700b092bdbfc0 Mon Sep 17 00:00:00 2001 From: Sanandan Sashikumar Date: Fri, 14 Feb 2025 21:48:12 +0100 Subject: [PATCH] Replace Delete-Messages-Days with Delete-Messages-Seconds (#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. --- src/http/client.rs | 10 ++++------ src/model/error.rs | 40 ------------------------------------- src/model/guild/guild_id.rs | 29 ++++++++++++++------------- src/model/guild/member.rs | 11 +++++----- 4 files changed, 24 insertions(+), 66 deletions(-) diff --git a/src/http/client.rs b/src/http/client.rs index f2c1b35597c..06ae1db471d 100644 --- a/src/http/client.rs +++ b/src/http/client.rs @@ -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, diff --git a/src/model/error.rs b/src/model/error.rs index f97f4dad0b5..c3afa896e12 100644 --- a/src/model/error.rs +++ b/src/model/error.rs @@ -12,7 +12,6 @@ pub enum Maximum { StickerCount, WebhookName, AuditLogReason, - DeleteMessageDays, BulkDeleteAmount, } @@ -38,7 +37,6 @@ impl Maximum { Self::StickerCount => crate::constants::STICKER_MAX_COUNT, Self::WebhookName | Self::BulkDeleteAmount => 100, Self::AuditLogReason => 512, - Self::DeleteMessageDays => 7, } } } @@ -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"), } } @@ -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> { -/// # 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] diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index c663d5e4749..701fe2fbc8a 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -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. /// @@ -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> { /// # 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 diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 0cedb1afdfa..dfaa67225ac 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -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.