Rust_Discord_API/utils/
role.rs

1use reqwest::Client;
2use serde_json::Value;
3use std::error::Error;
4
5/// Adds a role to a user in a Discord guild.
6///
7/// # Arguments
8///
9/// * `client` - The HTTP client used to send the request.
10/// * `token` - The bot token for authentication.
11/// * `guild_id` - The ID of the guild.
12/// * `user_id` - The ID of the user to add the role to.
13/// * `role_id` - The ID of the role to add.
14///
15/// # Returns
16///
17/// A result indicating success or failure.
18#[allow(dead_code)]
19pub async fn add_role(client: &Client, token: &str, guild_id: &str, user_id: &str, role_id: &str) -> Result<(), Box<dyn Error>> {
20    let url = format!("https://discord.com/api/v9/guilds/{}/members/{}/roles/{}", guild_id, user_id, role_id);
21    
22    client.put(&url)
23        .bearer_auth(token)
24        .send()
25        .await?
26        .error_for_status()?;
27    
28    Ok(())
29}
30
31/// Removes a role from a user in a Discord guild.
32///
33/// # Arguments
34///
35/// * `client` - The HTTP client used to send the request.
36/// * `token` - The bot token for authentication.
37/// * `guild_id` - The ID of the guild.
38/// * `user_id` - The ID of the user to remove the role from.
39/// * `role_id` - The ID of the role to remove.
40///
41/// # Returns
42///
43/// A result indicating success or failure.
44#[allow(dead_code)]
45pub async fn remove_role(client: &Client, token: &str, guild_id: &str, user_id: &str, role_id: &str) -> Result<(), Box<dyn Error>> {
46    let url = format!("https://discord.com/api/v9/guilds/{}/members/{}/roles/{}", guild_id, user_id, role_id);
47    
48    client.delete(&url)
49        .bearer_auth(token)
50        .send()
51        .await?
52        .error_for_status()?;
53    
54    Ok(())
55}
56
57/// Fetches information about a role in a Discord guild.
58///
59/// # Arguments
60///
61/// * `client` - The HTTP client used to send the request.
62/// * `token` - The bot token for authentication.
63/// * `guild_id` - The ID of the guild.
64/// * `role_id` - The ID of the role to fetch information for.
65///
66/// # Returns
67///
68/// A result containing the role information as a JSON value.
69#[allow(dead_code)]
70pub async fn fetch_role_info(client: &Client, token: &str, guild_id: &str, role_id: &str) -> Result<Value, Box<dyn Error>> {
71    let url = format!("https://discord.com/api/v9/guilds/{}/roles/{}", guild_id, role_id);
72    let response: Value = client.get(&url)
73        .bearer_auth(token)
74        .send()
75        .await?
76        .json()
77        .await?;
78    
79    Ok(response)
80}