add delete command to clean up the channel
This commit is contained in:
44
src/commands/delete.rs
Normal file
44
src/commands/delete.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::{env, error::Error, num::NonZeroU64, time::Duration};
|
||||
use tokio::time::sleep;
|
||||
use tracing::info;
|
||||
use twilight_model::{channel::Message, id::Id};
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
pub(crate) async fn delete(
|
||||
msg: Message,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
let admin = env::var("ADMIN")?.parse::<u64>()?;
|
||||
if msg.author.id != Id::from(NonZeroU64::new(admin).unwrap()) {
|
||||
return Ok(());
|
||||
}
|
||||
let n = msg
|
||||
.content
|
||||
.split(' ')
|
||||
.last()
|
||||
.unwrap()
|
||||
.parse::<u16>()
|
||||
.unwrap_or(1);
|
||||
if n > 100 {
|
||||
return Ok(());
|
||||
}
|
||||
let messages = state
|
||||
.http
|
||||
.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 {
|
||||
info!("Delete message: {:?}: {:?}", message.author.name, message);
|
||||
state
|
||||
.http
|
||||
.delete_message(msg.channel_id, message.id)
|
||||
.await?;
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -7,8 +7,8 @@ pub(crate) async fn join(
|
||||
msg: Message,
|
||||
state: State,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
let user_id = msg.author.id;
|
||||
let guild_id = msg.guild_id.ok_or("No guild id attached to the message.")?;
|
||||
let user_id = msg.author.id;
|
||||
let channel_id = state
|
||||
.cache
|
||||
.voice_state(user_id, guild_id)
|
||||
@@ -16,16 +16,19 @@ pub(crate) async fn join(
|
||||
.channel_id();
|
||||
let channel_id =
|
||||
NonZeroU64::new(channel_id.into()).ok_or("Joined voice channel must have nonzero ID.")?;
|
||||
state
|
||||
.songbird
|
||||
.join(guild_id, channel_id)
|
||||
.await
|
||||
.map_err(|e| format!("Could not join voice channel: {:?}", e))?;
|
||||
|
||||
// signal that we are not listening
|
||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||
let mut call = call_lock.lock().await;
|
||||
call.deafen(true).await?;
|
||||
}
|
||||
|
||||
// join the voice channel
|
||||
state
|
||||
.songbird
|
||||
.join(guild_id, channel_id)
|
||||
.await
|
||||
.map_err(|e| format!("Could not join voice channel: {:?}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -19,6 +19,9 @@ pub(crate) use resume::resume;
|
||||
mod stop;
|
||||
pub(crate) use stop::stop;
|
||||
|
||||
mod delete;
|
||||
pub(crate) use delete::delete;
|
||||
|
||||
use twilight_model::application::command::CommandType;
|
||||
use twilight_util::builder::command::{CommandBuilder, StringBuilder};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user