From f2cb41b11aa247ae1425d10c9162e7386089583c Mon Sep 17 00:00:00 2001 From: seth Date: Sun, 18 Feb 2024 18:13:32 -0500 Subject: add clear_messages command --- src/commands/mod.rs | 5 +++++ src/commands/moderation/clear.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/commands/moderation/mod.rs | 10 ++++++++++ src/main.rs | 1 + 4 files changed, 55 insertions(+) create mode 100644 src/commands/moderation/clear.rs create mode 100644 src/commands/moderation/mod.rs diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b20258c..db661e6 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -4,6 +4,7 @@ use color_eyre::eyre::Report; use poise::Command; mod general; +mod moderation; mod optional; pub fn global() -> Vec> { @@ -13,3 +14,7 @@ pub fn global() -> Vec> { pub fn optional() -> Vec> { optional::to_commands() } + +pub fn moderation() -> Vec> { + moderation::to_commands() +} 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> { + vec![clear::clear_messages()] +} diff --git a/src/main.rs b/src/main.rs index 1eef5bf..3cf07f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,7 @@ async fn main() -> Result<()> { commands: { let mut commands = commands::global(); commands.append(&mut commands::optional()); + commands.append(&mut commands::moderation()); commands }, on_error: |error| Box::pin(handlers::handle_error(error)), -- cgit v1.2.3