keep delete as chat command
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,84 +1,43 @@
|
|||||||
use std::{env, error::Error, num::NonZeroU64, time::Duration};
|
use std::{env, error::Error, num::NonZeroU64, time::Duration};
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
use tracing::{debug, info};
|
use tracing::info;
|
||||||
use twilight_model::{
|
use twilight_model::{channel::Message, id::Id};
|
||||||
application::interaction::Interaction,
|
|
||||||
channel::message::MessageFlags,
|
|
||||||
http::interaction::{InteractionResponse, InteractionResponseType},
|
|
||||||
id::Id,
|
|
||||||
};
|
|
||||||
use twilight_util::builder::InteractionResponseDataBuilder;
|
|
||||||
|
|
||||||
use crate::state::State;
|
use crate::state::State;
|
||||||
|
|
||||||
pub(crate) async fn delete(
|
pub(crate) async fn delete(
|
||||||
interaction: Interaction,
|
msg: Message,
|
||||||
state: State,
|
state: State,
|
||||||
count: i64,
|
|
||||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
) -> 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>()?;
|
let admin = env::var("ADMIN")?.parse::<u64>()?;
|
||||||
if interaction.author_id() != Some(Id::from(NonZeroU64::new(admin).unwrap())) {
|
if msg.author.id != 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?;
|
|
||||||
|
|
||||||
return Ok(());
|
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(());
|
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
|
let messages = state
|
||||||
.http
|
.http
|
||||||
.channel_messages(channel.id)
|
.channel_messages(msg.channel_id)
|
||||||
.before(message_id.cast())
|
.before(msg.id)
|
||||||
.limit(count)?
|
.limit(n)?
|
||||||
.await?
|
.await?
|
||||||
.model()
|
.model()
|
||||||
.await?;
|
.await?;
|
||||||
|
state.http.delete_message(msg.channel_id, msg.id).await?;
|
||||||
for message in messages {
|
for message in messages {
|
||||||
debug!("Delete message: {:?}: {:?}", message.author.name, message);
|
info!("Delete message: {:?}: {:?}", message.author.name, message);
|
||||||
state.http.delete_message(channel.id, message.id).await?;
|
state
|
||||||
|
.http
|
||||||
|
.delete_message(msg.channel_id, message.id)
|
||||||
|
.await?;
|
||||||
sleep(Duration::from_secs(5)).await;
|
sleep(Duration::from_secs(5)).await;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ mod delete;
|
|||||||
pub(crate) use delete::delete;
|
pub(crate) use delete::delete;
|
||||||
|
|
||||||
use twilight_model::application::command::CommandType;
|
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> {
|
pub(crate) fn get_chat_commands() -> Vec<twilight_model::application::command::Command> {
|
||||||
vec![
|
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("queue", "Print track queue", CommandType::ChatInput).build(),
|
||||||
CommandBuilder::new("resume", "Resume playing", CommandType::ChatInput).build(),
|
CommandBuilder::new("resume", "Resume playing", CommandType::ChatInput).build(),
|
||||||
CommandBuilder::new("stop", "Stop 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(),
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ enum InteractionCommand {
|
|||||||
Leave(Interaction),
|
Leave(Interaction),
|
||||||
Join(Interaction),
|
Join(Interaction),
|
||||||
Queue(Interaction),
|
Queue(Interaction),
|
||||||
Delete(Interaction, i64),
|
|
||||||
NotImplemented,
|
NotImplemented,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,6 +41,15 @@ impl Handler {
|
|||||||
Self { state }
|
Self { state }
|
||||||
}
|
}
|
||||||
pub(crate) async fn act(&self, event: Event) {
|
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 {
|
let interaction_command = match event {
|
||||||
Event::InteractionCreate(interaction) => {
|
Event::InteractionCreate(interaction) => {
|
||||||
debug!("interaction: {:?}", &interaction);
|
debug!("interaction: {:?}", &interaction);
|
||||||
@@ -71,19 +79,6 @@ impl Handler {
|
|||||||
"leave" => InteractionCommand::Leave(interaction.0.clone()),
|
"leave" => InteractionCommand::Leave(interaction.0.clone()),
|
||||||
"join" => InteractionCommand::Join(interaction.0.clone()),
|
"join" => InteractionCommand::Join(interaction.0.clone()),
|
||||||
"queue" => InteractionCommand::Queue(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,
|
_ => InteractionCommand::NotImplemented,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,9 +110,6 @@ impl Handler {
|
|||||||
InteractionCommand::Queue(interaction) => {
|
InteractionCommand::Queue(interaction) => {
|
||||||
spawn(queue(interaction, Arc::clone(&self.state)))
|
spawn(queue(interaction, Arc::clone(&self.state)))
|
||||||
}
|
}
|
||||||
InteractionCommand::Delete(interaction, count) => {
|
|
||||||
spawn(delete(interaction, Arc::clone(&self.state), count))
|
|
||||||
}
|
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
|||||||
interaction_client
|
interaction_client
|
||||||
.set_global_commands(&get_chat_commands())
|
.set_global_commands(&get_chat_commands())
|
||||||
.await?;
|
.await?;
|
||||||
let commands = interaction_client.global_commands().await?.models().await?;
|
// let commands = interaction_client.global_commands().await?.models().await?;
|
||||||
debug!("Global commands: {:?}", commands);
|
// debug!("Global commands: {:?}", commands);
|
||||||
|
|
||||||
let intents = Intents::GUILDS
|
let intents = Intents::GUILDS
|
||||||
| Intents::GUILD_MESSAGES
|
| Intents::GUILD_MESSAGES
|
||||||
|
|||||||
Reference in New Issue
Block a user