keep delete as chat command
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-02-17 15:09:34 +01:00
parent 872464bd31
commit 594a22e743
4 changed files with 34 additions and 86 deletions

View File

@@ -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<dyn Error + Send + Sync + 'static>> {
debug!(
"delete command in guild {:?} in channel {:?} by {:?}",
interaction.guild_id,
interaction.channel,
interaction.author(),
);
let admin = env::var("ADMIN")?.parse::<u64>()?;
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::<u16>()
.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(())

View File

@@ -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<twilight_model::application::command::Command> {
vec![
@@ -36,8 +36,5 @@ pub(crate) fn get_chat_commands() -> Vec<twilight_model::application::command::C
CommandBuilder::new("queue", "Print track queue", CommandType::ChatInput).build(),
CommandBuilder::new("resume", "Resume playing", CommandType::ChatInput).build(),
CommandBuilder::new("stop", "Stop playing", CommandType::ChatInput).build(),
CommandBuilder::new("delete", "Delete messages in chat", CommandType::ChatInput)
.option(IntegerBuilder::new("count", "How many messages to delete").required(true))
.build(),
]
}

View File

@@ -19,7 +19,6 @@ enum InteractionCommand {
Leave(Interaction),
Join(Interaction),
Queue(Interaction),
Delete(Interaction, i64),
NotImplemented,
}
@@ -42,6 +41,15 @@ impl Handler {
Self { state }
}
pub(crate) async fn act(&self, event: Event) {
match &event {
Event::MessageCreate(message) if message.content.starts_with("!") => {
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))
}
_ => {}
};
}

View File

@@ -48,8 +48,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
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