Rust_Discord_API/utils/
permissions.rs

1use reqwest::Client;
2use serde_json::Value;
3use std::error::Error;
4
5/// Checks if a user has a specific permission 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.
13/// * `permission` - The permission to check for.
14///
15/// # Returns
16///
17/// A result indicating whether the user has the specified permission.
18#[allow(dead_code)]
19pub async fn check_permission(client: &Client, token: &str, guild_id: &str, user_id: &str, permission: &str) -> Result<bool, Box<dyn Error>> {
20    let url = format!("https://discord.com/api/v9/guilds/{}/members/{}", guild_id, user_id);
21    let response: Value = client.get(&url)
22        .bearer_auth(token)
23        .send()
24        .await?
25        .json()
26        .await?;
27    
28    if let Some(permissions) = response["permissions"].as_str() {
29        // This is a simplified check. Adjust based on your specific permission needs.
30        Ok(permissions.contains(permission))
31    } else {
32        Ok(false)
33    }
34}