Rust_Discord_API/utils/
poll.rs

1use reqwest::Client;
2use serde_json::Value;
3use std::error::Error;
4
5#[allow(dead_code)]
6/// Fetches the voters for a specific answer in a poll.
7///
8/// # Arguments
9///
10/// * `client` - The HTTP client used to send the request.
11/// * `token` - The bot token for authentication.
12/// * `channel_id` - The ID of the channel.
13/// * `message_id` - The ID of the poll message.
14/// * `answer_id` - The ID of the answer to fetch voters for.
15/// * `after` - (Optional) Fetch users after this user ID.
16/// * `limit` - (Optional) Max number of users to return (1-100).
17///
18/// # Returns
19///
20/// A result containing the list of voters as a JSON value.
21pub async fn get_answer_voters(client: &Client, token: &str, channel_id: &str, message_id: &str, answer_id: &str, after: Option<&str>, limit: Option<u32>) -> Result<Value, Box<dyn Error>> {
22    let mut url = format!("https://discord.com/api/v9/channels/{}/polls/{}/answers/{}/voters", channel_id, message_id, answer_id);
23    
24    if after.is_some() || limit.is_some() {
25        url.push('?');
26        if let Some(after) = after {
27            url.push_str(&format!("after={}&", after));
28        }
29        if let Some(limit) = limit {
30            url.push_str(&format!("limit={}&", limit));
31        }
32        url.pop(); // Remove trailing '&' or '?'.
33    }
34    
35    let response: Value = client.get(&url)
36        .bearer_auth(token)
37        .send()
38        .await?
39        .json()
40        .await?;
41    
42    Ok(response)
43}
44
45#[allow(dead_code)]
46/// Ends a poll.
47///
48/// # Arguments
49///
50/// * `client` - The HTTP client used to send the request.
51/// * `token` - The bot token for authentication.
52/// * `channel_id` - The ID of the channel.
53/// * `message_id` - The ID of the poll message.
54///
55/// # Returns
56///
57/// A result containing the updated message information as a JSON value.
58pub async fn end_poll(client: &Client, token: &str, channel_id: &str, message_id: &str) -> Result<Value, Box<dyn Error>> {
59    let url = format!("https://discord.com/api/v9/channels/{}/polls/{}/expire", channel_id, message_id);
60    let response: Value = client.post(&url)
61        .bearer_auth(token)
62        .send()
63        .await?
64        .json()
65        .await?;
66    
67    Ok(response)
68}