dsc_commands_remover/
lib.rs

1use reqwest::{header::AUTHORIZATION, Client};
2
3/// Just an empty serde_json array
4pub const EMPTY_JSON_ARRAY: &serde_json::Value = &serde_json::json!([]);
5
6/// Pushes `[]` as commands to Discord's API route
7pub async fn remove_commands(
8    application_id: &str,
9    bot_token: &str,
10    guild_id: &Option<String>,
11) -> Result<(), reqwest::Error> {
12    let url = match guild_id {
13        Some(guild_id) => {
14            // Only remove persistent commands for this guild
15            format!(
16                "https://discord.com/api/v10/applications/{}/guilds/{}/commands",
17                application_id, guild_id,
18            )
19        }
20        None => {
21            // Only remove global commands
22            format!(
23                "https://discord.com/api/v10/applications/{}/commands",
24                application_id
25            )
26        }
27    };
28
29    call_command_api(&url, bot_token).await
30}
31
32pub async fn call_command_api(url: &String, bot_token: &str) -> Result<(), reqwest::Error> {
33    let client = Client::new();
34
35    let res = client
36        .put(url)
37        .header(AUTHORIZATION, format!("Bot {}", bot_token))
38        .json(EMPTY_JSON_ARRAY)
39        .send()
40        .await?;
41
42    res.error_for_status()?;
43    Ok(())
44}