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 | |
| parent | 0bf172a14076a929b095a3c02ec14ed9ab62b8a4 (diff) | |
add clear_messages command
| -rw-r--r-- | src/commands/mod.rs | 5 | ||||
| -rw-r--r-- | src/commands/moderation/clear.rs | 39 | ||||
| -rw-r--r-- | src/commands/moderation/mod.rs | 10 | ||||
| -rw-r--r-- | src/main.rs | 1 |
4 files changed, 55 insertions, 0 deletions
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<Command<Data, Report>> { @@ -13,3 +14,7 @@ pub fn global() -> Vec<Command<Data, Report>> { pub fn optional() -> Vec<Command<Data, Report>> { optional::to_commands() } + +pub fn moderation() -> Vec<Command<Data, Report>> { + 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<Command<Data, Report>> { + 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)), |
