From 10bde98e4bcf6ba16d4cff466fc0b4119ce9daf8 Mon Sep 17 00:00:00 2001 From: TheKodeToad Date: Sun, 5 May 2024 15:32:38 +0100 Subject: [PATCH] Add a context menu button to delete interactions Signed-off-by: TheKodeToad --- src/commands/general/delete_interaction.rs | 22 ++++++++++++++++++ src/commands/general/mod.rs | 1 + src/commands/mod.rs | 1 + src/handlers/event/delete_on_reaction.rs | 26 ---------------------- src/handlers/event/mod.rs | 11 --------- 5 files changed, 24 insertions(+), 37 deletions(-) create mode 100644 src/commands/general/delete_interaction.rs delete mode 100644 src/handlers/event/delete_on_reaction.rs diff --git a/src/commands/general/delete_interaction.rs b/src/commands/general/delete_interaction.rs new file mode 100644 index 0000000..461ecb6 --- /dev/null +++ b/src/commands/general/delete_interaction.rs @@ -0,0 +1,22 @@ +use poise::serenity_prelude::{Message, MessageInteractionMetadata::Command}; + +use crate::{Context, Error}; + +#[poise::command(context_menu_command = "Delete command", ephemeral)] +pub async fn delete_interaction(ctx: Context<'_>, message: Message) -> Result<(), Error> { + let Some(Command(interaction)) = message.interaction_metadata.as_deref() else { + ctx.say("❌ This message does not contain a command") + .await?; + return Ok(()); + }; + + if interaction.user.id != ctx.author().id { + ctx.say("❌ You cannot delete commands run by other users") + .await?; + return Ok(()); + } + + message.delete(ctx).await?; + ctx.say("🗑️ Deleted command!").await?; + Ok(()) +} diff --git a/src/commands/general/mod.rs b/src/commands/general/mod.rs index 7e2d92f..9ecc7ec 100644 --- a/src/commands/general/mod.rs +++ b/src/commands/general/mod.rs @@ -1,3 +1,4 @@ +pub mod delete_interaction; pub mod help; pub mod joke; pub mod members; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 2292afd..210580c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -34,6 +34,7 @@ pub type Command = poise::Command; pub fn all() -> Vec { vec![ + general!(delete_interaction), general!(help), general!(joke), general!(members), diff --git a/src/handlers/event/delete_on_reaction.rs b/src/handlers/event/delete_on_reaction.rs deleted file mode 100644 index 59a9eef..0000000 --- a/src/handlers/event/delete_on_reaction.rs +++ /dev/null @@ -1,26 +0,0 @@ -use eyre::{Context as _, Result}; -use log::trace; -use poise::serenity_prelude::{Context, MessageInteractionMetadata, Reaction}; - -pub async fn handle(ctx: &Context, reaction: &Reaction) -> Result<()> { - let user = reaction - .user(ctx) - .await - .wrap_err("Couldn't fetch user from reaction!")?; - - let message = reaction - .message(ctx) - .await - .wrap_err("Couldn't fetch message from reaction!")?; - - if let Some(MessageInteractionMetadata::Command(metadata)) = - message.interaction_metadata.as_deref() - { - if metadata.user == user && reaction.emoji.unicode_eq("❌") { - trace!("Deleting our own message at the request of {}", user.tag()); - message.delete(ctx).await?; - } - } - - Ok(()) -} diff --git a/src/handlers/event/mod.rs b/src/handlers/event/mod.rs index efaca47..017ccb7 100644 --- a/src/handlers/event/mod.rs +++ b/src/handlers/event/mod.rs @@ -5,7 +5,6 @@ use poise::serenity_prelude::{ActivityData, Context, FullEvent, OnlineStatus}; use poise::FrameworkContext; mod analyze_logs; -mod delete_on_reaction; mod eta; mod expand_link; mod give_role; @@ -66,16 +65,6 @@ pub async fn handle( analyze_logs::handle(ctx, new_message, data).await?; } - FullEvent::ReactionAdd { add_reaction } => { - trace!( - "Received reaction {} on message {} from {}", - add_reaction.emoji, - add_reaction.message_id.to_string(), - add_reaction.user_id.unwrap_or_default().to_string() - ); - delete_on_reaction::handle(ctx, add_reaction).await?; - } - FullEvent::ThreadCreate { thread } => { trace!("Received thread {}", thread.id); support_onboard::handle(ctx, thread).await?;