Skip to content

Commit

Permalink
Added delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
obvMellow committed Sep 7, 2024
1 parent b243f18 commit 91b4522
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
54 changes: 54 additions & 0 deletions libdistore/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,60 @@ pub async fn check_update() -> Result<()> {
}
}

pub async fn delete(
message_id: u64,
token: Option<String>,
channel: Option<u64>,
dir: Option<PathBuf>,
) -> 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,
Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// Optionally use a channel for this one time
#[arg(short, long, require_equals = true)]
channel: Option<u64>,
},
}

// Convenience macro to read user input
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit 91b4522

Please sign in to comment.