use reqwest::Client;
use serde_json::Value;
use std::error::Error;
#[allow(dead_code)]
pub async fn get_current_user(client: &Client, token: &str) -> Result<Value, Box<dyn Error>> {
let url = "https://discord.com/api/v9/users/@me";
let response: Value = client.get(url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_user(client: &Client, token: &str, user_id: &str) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/users/{}", user_id);
let response: Value = client.get(&url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn modify_current_user(client: &Client, token: &str, settings: Value) -> Result<Value, Box<dyn Error>> {
let url = "https://discord.com/api/v9/users/@me";
let response: Value = client.patch(url)
.bearer_auth(token)
.json(&settings)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_current_user_guilds(client: &Client, token: &str) -> Result<Value, Box<dyn Error>> {
let url = "https://discord.com/api/v9/users/@me/guilds";
let response: Value = client.get(url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_current_user_guild_member(client: &Client, token: &str, guild_id: &str) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/users/@me/guilds/{}/member", guild_id);
let response: Value = client.get(&url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn leave_guild(client: &Client, token: &str, guild_id: &str) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/users/@me/guilds/{}", guild_id);
client.delete(&url)
.bearer_auth(token)
.send()
.await?
.error_for_status()?;
Ok(())
}
#[allow(dead_code)]
pub async fn create_dm(client: &Client, token: &str, recipient_id: &str) -> Result<Value, Box<dyn Error>> {
let url = "https://discord.com/api/v9/users/@me/channels";
let body = serde_json::json!({ "recipient_id": recipient_id });
let response: Value = client.post(url)
.bearer_auth(token)
.json(&body)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn create_group_dm(client: &Client, token: &str, access_tokens: Vec<&str>, nicks: Value) -> Result<Value, Box<dyn Error>> {
let url = "https://discord.com/api/v9/users/@me/channels";
let body = serde_json::json!({ "access_tokens": access_tokens, "nicks": nicks });
let response: Value = client.post(url)
.bearer_auth(token)
.json(&body)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_current_user_connections(client: &Client, token: &str) -> Result<Value, Box<dyn Error>> {
let url = "https://discord.com/api/v9/users/@me/connections";
let response: Value = client.get(url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn get_current_user_application_role_connection(client: &Client, token: &str, application_id: &str) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/users/@me/applications/{}/role-connection", application_id);
let response: Value = client.get(&url)
.bearer_auth(token)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn update_current_user_application_role_connection(client: &Client, token: &str, application_id: &str, role_connection: Value) -> Result<Value, Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/users/@me/applications/{}/role-connection", application_id);
let response: Value = client.put(&url)
.bearer_auth(token)
.json(&role_connection)
.send()
.await?
.json()
.await?;
Ok(response)
}
#[allow(dead_code)]
pub async fn kick_user(client: &Client, token: &str, guild_id: &str, user_id: &str) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/guilds/{}/members/{}", guild_id, user_id);
client.delete(&url)
.bearer_auth(token)
.send()
.await?
.error_for_status()?;
Ok(())
}
#[allow(dead_code)]
pub async fn ban_user(client: &Client, token: &str, guild_id: &str, user_id: &str, delete_message_days: u8, reason: &str) -> Result<(), Box<dyn Error>> {
let url = format!("https://discord.com/api/v9/guilds/{}/bans/{}", guild_id, user_id);
let body = serde_json::json!({
"delete_message_days": delete_message_days,
"reason": reason
});
client.put(&url)
.bearer_auth(token)
.json(&body)
.send()
.await?
.error_for_status()?;
Ok(())
}