use reqwest::Client;
use serde_json::Value;
use std::error::Error;
#[allow(dead_code)]
pub async fn create_webhook(client: &Client, token: &str, channel_id: &str, webhook_settings: Value) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/channels/{}/webhooks", channel_id);
let response: Value = client.post(&url)
.bearer_auth(token)
.json(&webhook_settings)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_channel_webhooks(client: &Client, token: &str, channel_id: &str) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/channels/{}/webhooks", channel_id);
let response: Value = client.get(&url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_guild_webhooks(client: &Client, token: &str, guild_id: &str) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/guilds/{}/webhooks", guild_id);
let response: Value = client.get(&url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_webhook(client: &Client, token: &str, webhook_id: &str) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}", webhook_id);
let response: Value = client.get(&url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_webhook_with_token(client: &Client, webhook_id: &str, webhook_token: &str) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}", webhook_id, webhook_token);
let response: Value = client.get(&url)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn modify_webhook(client: &Client, token: &str, webhook_id: &str, settings: Value) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}", webhook_id);
let response: Value = client.patch(&url)
.bearer_auth(token)
.json(&settings)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn modify_webhook_with_token(client: &Client, webhook_id: &str, webhook_token: &str, settings: Value) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}", webhook_id, webhook_token);
let response: Value = client.patch(&url)
.json(&settings)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn delete_webhook(client: &Client, token: &str, webhook_id: &str) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}", webhook_id);
client.delete(&url)
.bearer_auth(token)
.send()
.await?
.error_for_status()?;
Ok(())
}
#[allow(dead_code)]
pub async fn delete_webhook_with_token(client: &Client, webhook_id: &str, webhook_token: &str) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}", webhook_id, webhook_token);
client.delete(&url)
.send()
.await?
.error_for_status()?;
Ok(())
}
#[allow(dead_code)]
pub async fn execute_webhook(client: &Client, webhook_id: &str, webhook_token: &str, payload: Value) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}", webhook_id, webhook_token);
client.post(&url)
.json(&payload)
.send()
.await?
.error_for_status()?;
Ok(())
}
#[allow(dead_code)]
pub async fn execute_slack_compatible_webhook(client: &Client, webhook_id: &str, webhook_token: &str, payload: Value) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}/slack", webhook_id, webhook_token);
client.post(&url)
.json(&payload)
.send()
.await?
.error_for_status()?;
Ok(())
}
#[allow(dead_code)]
pub async fn execute_github_compatible_webhook(client: &Client, webhook_id: &str, webhook_token: &str, payload: Value) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}/github", webhook_id, webhook_token);
client.post(&url)
.json(&payload)
.send()
.await?
.error_for_status()?;
Ok(())
}
#[allow(dead_code)]
pub async fn get_webhook_message(client: &Client, webhook_id: &str, webhook_token: &str, message_id: &str) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}/messages/{}", webhook_id, webhook_token, message_id);
let response: Value = client.get(&url)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn edit_webhook_message(client: &Client, webhook_id: &str, webhook_token: &str, message_id: &str, new_content: Value) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}/messages/{}", webhook_id, webhook_token, message_id);
let response: Value = client.patch(&url)
.json(&new_content)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn delete_webhook_message(client: &Client, webhook_id: &str, webhook_token: &str, message_id: &str) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/webhooks/{}/{}/messages/{}", webhook_id, webhook_token, message_id);
client.delete(&url)
.send()
.await?
.error_for_status()?;
Ok(())
}