Add a context menu button to delete interactions

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad 2024-05-05 15:32:38 +01:00
parent 04faac4de3
commit 10bde98e4b
No known key found for this signature in database
GPG key ID: 5E39D70B4C93C38E
5 changed files with 24 additions and 37 deletions

View file

@ -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(())
}

View file

@ -1,3 +1,4 @@
pub mod delete_interaction;
pub mod help;
pub mod joke;
pub mod members;

View file

@ -34,6 +34,7 @@ pub type Command = poise::Command<Data, Error>;
pub fn all() -> Vec<Command> {
vec![
general!(delete_interaction),
general!(help),
general!(joke),
general!(members),

View file

@ -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(())
}

View file

@ -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?;