Skip to content

Commit 09bea56

Browse files
cheesycodarqunis
authored andcommitted
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.
1 parent 025eacb commit 09bea56

File tree

4 files changed

+24
-66
lines changed

4 files changed

+24
-66
lines changed

src/http/client.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,17 @@ impl Http {
334334
}
335335

336336
/// Bans a [`User`] from a [`Guild`], removing their messages sent in the last X number of
337-
/// days.
337+
/// seconds.
338338
///
339-
/// Passing a `delete_message_days` of `0` is equivalent to not removing any messages. Up to
340-
/// `7` days' worth of messages may be deleted.
339+
/// Passing a `delete_message_seconds` of `0` is equivalent to not removing any messages. Up to
340+
/// `604800` seconds (or 7 days) worth of messages may be deleted.
341341
pub async fn ban_user(
342342
&self,
343343
guild_id: GuildId,
344344
user_id: UserId,
345-
delete_message_days: u8,
345+
delete_message_seconds: u32,
346346
reason: Option<&str>,
347347
) -> Result<()> {
348-
let delete_message_seconds = u32::from(delete_message_days) * 86400;
349-
350348
self.wind(Request {
351349
body: None,
352350
multipart: None,

src/model/error.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub enum Maximum {
1212
StickerCount,
1313
WebhookName,
1414
AuditLogReason,
15-
DeleteMessageDays,
1615
BulkDeleteAmount,
1716
}
1817

@@ -38,7 +37,6 @@ impl Maximum {
3837
Self::StickerCount => crate::constants::STICKER_MAX_COUNT,
3938
Self::WebhookName | Self::BulkDeleteAmount => 100,
4039
Self::AuditLogReason => 512,
41-
Self::DeleteMessageDays => 7,
4240
}
4341
}
4442
}
@@ -52,7 +50,6 @@ impl fmt::Display for Maximum {
5250
Self::StickerCount => f.write_str("Sticker count"),
5351
Self::WebhookName => f.write_str("Webhook name"),
5452
Self::AuditLogReason => f.write_str("Audit log reason"),
55-
Self::DeleteMessageDays => f.write_str("Delete message days"),
5653
Self::BulkDeleteAmount => f.write_str("Message bulk delete count"),
5754
}
5855
}
@@ -100,44 +97,7 @@ impl fmt::Display for Minimum {
10097
///
10198
/// This is always wrapped within the library's [`Error::Model`] variant.
10299
///
103-
/// # Examples
104-
///
105-
/// Matching an [`Error`] with this variant would look something like the following for the
106-
/// [`GuildId::ban`] method, which in this example is used to re-ban all members.
107-
///
108-
/// ```rust,no_run
109-
/// use serenity::model::prelude::*;
110-
/// use serenity::model::ModelError;
111-
/// use serenity::prelude::*;
112-
/// use serenity::Error;
113-
///
114-
/// # #[cfg(feature = "http")]
115-
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
116-
/// # let http: serenity::http::Http = unimplemented!();
117-
/// # let guild_id: GuildId = unimplemented!();
118-
/// # let user: User = unimplemented!();
119-
///
120-
/// match guild_id.ban(&http, user.id, 8, Some("No unbanning people!")).await {
121-
/// Ok(()) => {
122-
/// // Ban successful.
123-
/// },
124-
/// Err(Error::Model(ModelError::TooLarge {
125-
/// value, ..
126-
/// })) => {
127-
/// println!("Failed deleting {value} days' worth of messages");
128-
/// },
129-
/// Err(why) => {
130-
/// println!("Unexpected error: {why:?}");
131-
/// },
132-
/// }
133-
///
134-
/// # Ok(())
135-
/// # }
136-
/// ```
137-
///
138-
/// [`Error`]: crate::Error
139100
/// [`Error::Model`]: crate::Error::Model
140-
/// [`GuildId::ban`]: super::id::GuildId::ban
141101
/// [`model`]: crate::model
142102
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
143103
#[non_exhaustive]

src/model/guild/guild_id.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ impl GuildId {
161161
builder.execute(http, self, user_id).await
162162
}
163163

164-
/// Ban a [`User`] from the guild, deleting a number of days' worth of messages (`dmd`) between
165-
/// the range 0 and 7.
164+
/// Ban a [`User`] from the guild, deleting `dms` seconds worth of messages from them.
165+
/// `dms` should be between 0 and 604800 (subject to change). To ban someone for X days,
166+
/// multiply X by 86400.
166167
///
167168
/// **Note**: Requires the [Ban Members] permission.
168169
///
@@ -173,31 +174,31 @@ impl GuildId {
173174
/// ```rust,no_run
174175
/// use serenity::model::id::{GuildId, UserId};
175176
///
177+
/// const FOUR_DAYS_IN_SECONDS: u32 = 4 * 24 * 60 * 60;
178+
///
176179
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
177180
/// # use serenity::http::Http;
178181
/// # let http: Http = unimplemented!();
179182
/// # let user = UserId::new(1);
180183
/// // assuming a `user` has already been bound
181-
/// let _ = GuildId::new(81384788765712384).ban(&http, user, 4, None).await;
184+
/// let _ = GuildId::new(81384788765712384).ban(&http, user, FOUR_DAYS_IN_SECONDS, None).await;
182185
/// # Ok(())
183186
/// # }
184187
/// ```
185188
///
186189
/// # Errors
187190
///
188-
/// Returns a [`ModelError::TooLarge`] if the number of days' worth of messages
189-
/// to delete is over the maximum.
190-
///
191-
/// Also can return [`Error::Http`] if the current user lacks permission.
191+
/// Can return [`Error::Http`] if the current user lacks permission.
192192
///
193193
/// [Ban Members]: Permissions::BAN_MEMBERS
194-
pub async fn ban(self, http: &Http, user: UserId, dmd: u8, reason: Option<&str>) -> Result<()> {
195-
Maximum::DeleteMessageDays.check_overflow(dmd.into())?;
196-
if let Some(reason) = reason {
197-
Maximum::AuditLogReason.check_overflow(reason.len())?;
198-
}
199-
200-
http.ban_user(self, user, dmd, reason).await
194+
pub async fn ban(
195+
self,
196+
http: &Http,
197+
user: UserId,
198+
dms: u32,
199+
reason: Option<&str>,
200+
) -> Result<()> {
201+
http.ban_user(self, user, dms, reason).await
201202
}
202203

203204
/// Bans multiple users from the guild, returning the users that were and weren't banned, and

src/model/guild/member.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,18 @@ impl Member {
133133
Ok(())
134134
}
135135

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

151150
/// Determines the member's colour.

0 commit comments

Comments
 (0)