diff --git a/src/commands/delete.rs b/src/commands/delete.rs index 39392c3..ad119c1 100644 --- a/src/commands/delete.rs +++ b/src/commands/delete.rs @@ -1,84 +1,43 @@ use std::{env, error::Error, num::NonZeroU64, time::Duration}; use tokio::time::sleep; -use tracing::{debug, info}; -use twilight_model::{ - application::interaction::Interaction, - channel::message::MessageFlags, - http::interaction::{InteractionResponse, InteractionResponseType}, - id::Id, -}; -use twilight_util::builder::InteractionResponseDataBuilder; +use tracing::info; +use twilight_model::{channel::Message, id::Id}; use crate::state::State; pub(crate) async fn delete( - interaction: Interaction, + msg: Message, state: State, - count: i64, ) -> Result<(), Box> { - debug!( - "delete command in guild {:?} in channel {:?} by {:?}", - interaction.guild_id, - interaction.channel, - interaction.author(), - ); - let admin = env::var("ADMIN")?.parse::()?; - if interaction.author_id() != Some(Id::from(NonZeroU64::new(admin).unwrap())) { - let interaction_response_data = InteractionResponseDataBuilder::new() - .content("You do not have permissions to delete messages.") - .flags(MessageFlags::EPHEMERAL) - .build(); - - let response = InteractionResponse { - kind: InteractionResponseType::ChannelMessageWithSource, - data: Some(interaction_response_data), - }; - - state - .http - .interaction(interaction.application_id) - .create_response(interaction.id, &interaction.token, &response) - .await?; - + if msg.author.id != Id::from(NonZeroU64::new(admin).unwrap()) { return Ok(()); } - let Some(channel) = interaction.channel else { + let n = msg + .content + .split(' ') + .last() + .unwrap() + .parse::() + .unwrap_or(1); + if n > 100 { return Ok(()); - }; - let Some(message_id) = channel.last_message_id else { - return Ok(()); - }; - - let count = count.max(1).min(100) as u16; - - let interaction_response_data = InteractionResponseDataBuilder::new() - .content(format!("Deleting {count} messages.")) - .flags(MessageFlags::EPHEMERAL) - .build(); - - let response = InteractionResponse { - kind: InteractionResponseType::ChannelMessageWithSource, - data: Some(interaction_response_data), - }; - - state - .http - .interaction(interaction.application_id) - .create_response(interaction.id, &interaction.token, &response) - .await?; - + } let messages = state .http - .channel_messages(channel.id) - .before(message_id.cast()) - .limit(count)? + .channel_messages(msg.channel_id) + .before(msg.id) + .limit(n)? .await? .model() .await?; + state.http.delete_message(msg.channel_id, msg.id).await?; for message in messages { - debug!("Delete message: {:?}: {:?}", message.author.name, message); - state.http.delete_message(channel.id, message.id).await?; + info!("Delete message: {:?}: {:?}", message.author.name, message); + state + .http + .delete_message(msg.channel_id, message.id) + .await?; sleep(Duration::from_secs(5)).await; } Ok(()) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d29ab67..cb82b6f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -23,7 +23,7 @@ mod delete; pub(crate) use delete::delete; use twilight_model::application::command::CommandType; -use twilight_util::builder::command::{CommandBuilder, IntegerBuilder, StringBuilder}; +use twilight_util::builder::command::{CommandBuilder, StringBuilder}; pub(crate) fn get_chat_commands() -> Vec { vec![ @@ -36,8 +36,5 @@ pub(crate) fn get_chat_commands() -> Vec { + if message.content.contains("!delete") { + spawn(delete(message.0.clone(), Arc::clone(&self.state))); + } + } + _ => {} + } + let interaction_command = match event { Event::InteractionCreate(interaction) => { debug!("interaction: {:?}", &interaction); @@ -71,19 +79,6 @@ impl Handler { "leave" => InteractionCommand::Leave(interaction.0.clone()), "join" => InteractionCommand::Join(interaction.0.clone()), "queue" => InteractionCommand::Queue(interaction.0.clone()), - "delete" => { - if let Some(count_option) = - command.options.iter().find(|opt| opt.name == "count") - { - if let CommandOptionValue::Integer(count) = count_option.value { - InteractionCommand::Delete(interaction.0.clone(), count) - } else { - InteractionCommand::NotImplemented - } - } else { - InteractionCommand::NotImplemented - } - } _ => InteractionCommand::NotImplemented, } } @@ -115,9 +110,6 @@ impl Handler { InteractionCommand::Queue(interaction) => { spawn(queue(interaction, Arc::clone(&self.state))) } - InteractionCommand::Delete(interaction, count) => { - spawn(delete(interaction, Arc::clone(&self.state), count)) - } _ => {} }; } diff --git a/src/main.rs b/src/main.rs index 2f1dfbb..92e9fde 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,8 +48,8 @@ async fn main() -> Result<(), Box> { interaction_client .set_global_commands(&get_chat_commands()) .await?; - let commands = interaction_client.global_commands().await?.models().await?; - debug!("Global commands: {:?}", commands); + // let commands = interaction_client.global_commands().await?.models().await?; + // debug!("Global commands: {:?}", commands); let intents = Intents::GUILDS | Intents::GUILD_MESSAGES