diff options
| author | seth <[email protected]> | 2024-02-18 18:13:32 -0500 |
|---|---|---|
| committer | seth <[email protected]> | 2024-02-18 18:38:30 -0500 |
| commit | f2cb41b11aa247ae1425d10c9162e7386089583c (patch) | |
| tree | e43f3bec0a56d47d8b048f646aece2e1c8eccea7 /src/commands/moderation | |
| parent | 0bf172a14076a929b095a3c02ec14ed9ab62b8a4 (diff) | |
add clear_messages command
Diffstat (limited to 'src/commands/moderation')
| -rw-r--r-- | src/commands/moderation/clear.rs | 39 | ||||
| -rw-r--r-- | src/commands/moderation/mod.rs | 10 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/commands/moderation/clear.rs b/src/commands/moderation/clear.rs new file mode 100644 index 0000000..677dc14 --- /dev/null +++ b/src/commands/moderation/clear.rs @@ -0,0 +1,39 @@ +use crate::Context; + +use color_eyre::eyre::{Context as _, Result}; +use log::debug; +use poise::serenity_prelude::futures::{StreamExt, TryStreamExt}; + +#[poise::command( + slash_command, + ephemeral, + required_permissions = "MANAGE_MESSAGES", + default_member_permissions = "MANAGE_MESSAGES" +)] +pub async fn clear_messages( + ctx: Context<'_>, + #[description = "How many messages to delete"] num_messages: usize, +) -> Result<()> { + ctx.defer_ephemeral().await?; + + let channel = ctx.channel_id(); + let messages = channel + .messages_iter(ctx) + .take(num_messages) + .try_fold(Vec::new(), |mut acc, msg| async move { + acc.push(msg); + Ok(acc) + }) + .await + .wrap_err_with(|| { + format!("Couldn't collect {num_messages} messages from channel {channel}") + })?; + + debug!("Clearing {num_messages} messages from channel {channel}!"); + channel.delete_messages(ctx, messages).await?; + + ctx.reply(format!("Deleted {num_messages} message(s)")) + .await?; + + Ok(()) +} diff --git a/src/commands/moderation/mod.rs b/src/commands/moderation/mod.rs new file mode 100644 index 0000000..218ad16 --- /dev/null +++ b/src/commands/moderation/mod.rs @@ -0,0 +1,10 @@ +use crate::Data; + +use color_eyre::eyre::Report; +use poise::Command; + +mod clear; + +pub fn to_commands() -> Vec<Command<Data, Report>> { + vec![clear::clear_messages()] +} |
