From 91b4522fd969b9819ba9382fe533e3e40abd3688 Mon Sep 17 00:00:00 2001 From: obvMellow Date: Sat, 7 Sep 2024 22:24:46 +0300 Subject: [PATCH] Added delete command --- libdistore/src/commands.rs | 54 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 18 +++++++++++++ 2 files changed, 72 insertions(+) diff --git a/libdistore/src/commands.rs b/libdistore/src/commands.rs index f0b738b..d239921 100644 --- a/libdistore/src/commands.rs +++ b/libdistore/src/commands.rs @@ -516,6 +516,60 @@ pub async fn check_update() -> Result<()> { } } +pub async fn delete( + message_id: u64, + token: Option, + channel: Option, + dir: Option, +) -> Result<()> { + colog::default_builder() + .filter(Some("serenity"), log::LevelFilter::Off) + .init(); + let mut path = dir + .unwrap_or(dirs::config_dir().ok_or(ConfigError::NoConfigDir)?) + .join("distore"); + fs::create_dir_all(&path).context("Failed to create config directory")?; + path.push("distore.ini"); + + let token = token.unwrap_or_else(|| { + crate::config::ConfigValue::get_current_config(&path) + .context("Failed to get the config file") + .unwrap() + .0 + .inner() + .to_string() + }); + let channel = channel.unwrap_or_else(|| { + crate::config::ConfigValue::get_current_config(&path) + .unwrap() + .1 + .inner() + .parse() + .unwrap() + }); + + let http = Http::new(&token); + + let msg = http.get_message(channel.into(), message_id.into()).await?; + + let mut entry = FileEntry::from_str(&msg.content)?; + + let len = entry.len.ok_or(anyhow!("Invalid Message"))?; + info!("Deleting {} message(s)...", (len + 9) / 10); + + msg.delete(&http).await?; + + while entry.next.is_some() { + let msg = http + .get_message(channel.into(), entry.next.unwrap().into()) + .await?; + entry = FileEntry::from_str(&msg.content)?; + msg.delete(&http).await?; + } + + Ok(()) +} + async fn _get_messages( channel_id: ChannelId, http: &Http, diff --git a/src/main.rs b/src/main.rs index 03371a9..af895bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,6 +94,19 @@ enum Commands { }, /// Checks for updates Update, + /// Deletes a file from Discord + Delete { + /// Message ID for the file + message_id: u64, + + /// Optionally use a token for this one time + #[arg(short, long, require_equals = true)] + token: Option, + + /// Optionally use a channel for this one time + #[arg(short, long, require_equals = true)] + channel: Option, + }, } // Convenience macro to read user input @@ -166,6 +179,11 @@ async fn main() -> anyhow::Result<()> { commands::list(token, channel, args.config_directory).await? } Commands::Update => commands::check_update().await?, + Commands::Delete { + message_id, + token, + channel, + } => commands::delete(message_id, token, channel, args.config_directory).await?, } Ok(())