Rust_Discord_API/utils/guild_scheduled_event.rs
1use reqwest::Client;
2use serde_json::Value;
3use std::error::Error;
4
5#[allow(dead_code)]
6/// Lists scheduled events for a Discord guild.
7///
8/// # Arguments
9///
10/// * `client` - The HTTP client used to send the request.
11/// * `token` - The bot token for authentication.
12/// * `guild_id` - The ID of the guild to list scheduled events for.
13///
14/// # Returns
15///
16/// A result containing the list of scheduled events as a JSON value.
17pub async fn list_scheduled_events(client: &Client, token: &str, guild_id: &str, event_id: &str) -> Result<Value, Box<dyn Error>> {
18 let url = format!("https://discord.com/api/v9/guilds/{}/scheduled-events/{}", guild_id, event_id);
19 let response: Value = client.get(&url)
20 .bearer_auth(token)
21 .send()
22 .await?
23 .json()
24 .await?;
25
26 Ok(response)
27}
28
29#[allow(dead_code)]
30/// Creates a scheduled event in a Discord guild.
31///
32/// # Arguments
33///
34/// * `client` - The HTTP client used to send the request.
35/// * `token` - The bot token for authentication.
36/// * `guild_id` - The ID of the guild to create the scheduled event in.
37/// * `event_settings` - The JSON value of the event settings.
38///
39/// # Returns
40///
41/// A result containing the created scheduled event information as a JSON value.
42pub async fn create_scheduled_event(client: &Client, token: &str, guild_id: &str, event_settings: Value) -> Result<Value, Box<dyn Error>> {
43 let url = format!("https://discord.com/api/v9/guilds/{}/scheduled-events", guild_id);
44 let response: Value = client.post(&url)
45 .bearer_auth(token)
46 .json(&event_settings)
47 .send()
48 .await?
49 .json()
50 .await?;
51
52 Ok(response)
53}
54
55#[allow(dead_code)]
56/// Fetches a scheduled event from a Discord guild.
57///
58/// # Arguments
59///
60/// * `client` - The HTTP client used to send the request.
61/// * `token` - The bot token for authentication.
62/// * `guild_id` - The ID of the guild.
63/// * `event_id` - The ID of the scheduled event to fetch.
64///
65/// # Returns
66///
67/// A result containing the scheduled event information as a JSON value.
68pub async fn get_scheduled_event(client: &Client, token: &str, guild_id: &str, event_id: &str) -> Result<Value, Box<dyn Error>> {
69 let url = format!("https://discord.com/api/v9/guilds/{}/scheduled-events/{}", guild_id, event_id);
70 let response: Value = client.get(&url)
71 .bearer_auth(token)
72 .send()
73 .await?
74 .json()
75 .await?;
76
77 Ok(response)
78}
79
80#[allow(dead_code)]
81/// Modifies a scheduled event in a Discord guild.
82///
83/// # Arguments
84///
85/// * `client` - The HTTP client used to send the request.
86/// * `token` - The bot token for authentication.
87/// * `guild_id` - The ID of the guild.
88/// * `event_id` - The ID of the scheduled event to modify.
89/// * `event_settings` - The JSON value of the event settings.
90///
91/// # Returns
92///
93/// A result containing the modified scheduled event information as a JSON value.
94pub async fn modify_scheduled_event(client: &Client, token: &str, guild_id: &str, event_id: &str, event_settings: Value) -> Result<Value, Box<dyn Error>> {
95 let url = format!("https://discord.com/api/v9/guilds/{}/scheduled-events/{}", guild_id, event_id);
96 let response: Value = client.patch(&url)
97 .bearer_auth(token)
98 .json(&event_settings)
99 .send()
100 .await?
101 .json()
102 .await?;
103
104 Ok(response)
105}
106
107#[allow(dead_code)]
108/// Deletes a scheduled event from a Discord guild.
109///
110/// # Arguments
111///
112/// * `client` - The HTTP client used to send the request.
113/// * `token` - The bot token for authentication.
114/// * `guild_id` - The ID of the guild.
115/// * `event_id` - The ID of the scheduled event to delete.
116///
117/// # Returns
118///
119/// A result indicating success or failure.
120pub async fn delete_scheduled_event(client: &Client, token: &str, guild_id: &str, event_id: &str) -> Result<(), Box<dyn Error>> {
121 let url = format!("https://discord.com/api/v9/guilds/{}/scheduled-events/{}", guild_id, event_id);
122
123 client.delete(&url)
124 .bearer_auth(token)
125 .send()
126 .await?
127 .error_for_status()?;
128
129 Ok(())
130}
131
132#[allow(dead_code)]
133/// Fetches users of a scheduled event in a Discord guild.
134///
135/// # Arguments
136///
137/// * `client` - The HTTP client used to send the request.
138/// * `token` - The bot token for authentication.
139/// * `guild_id` - The ID of the guild.
140/// * `event_id` - The ID of the scheduled event to fetch users for.
141///
142/// # Returns
143///
144/// A result containing the list of users as a JSON value.
145pub async fn get_scheduled_event_users(client: &Client, token: &str, guild_id: &str, event_id: &str) -> Result<Value, Box<dyn Error>> {
146 let url = format!("https://discord.com/api/v9/guilds/{}/scheduled-events/{}/users", guild_id, event_id);
147 let response: Value = client.get(&url)
148 .bearer_auth(token)
149 .send()
150 .await?
151 .json()
152 .await?;
153
154 Ok(response)
155}
156
157#[allow(dead_code)]
158/// Updates the status of a scheduled event in a Discord guild.
159///
160/// # Arguments
161///
162/// * `client` - The HTTP client used to send the request.
163/// * `token` - The bot token for authentication.
164/// * `guild_id` - The ID of the guild.
165/// * `event_id` - The ID of the scheduled event to update the status for.
166/// * `status` - The new status of the scheduled event.
167///
168/// # Returns
169///
170/// A result containing the updated scheduled event information as a JSON value.
171pub async fn update_scheduled_event_status(client: &Client, token: &str, guild_id: &str, event_id: &str, status: &str) -> Result<Value, Box<dyn Error>> {
172 let url = format!("https://discord.com/api/v9/guilds/{}/scheduled-events/{}", guild_id, event_id);
173 let body = serde_json::json!({ "status": status });
174 let response: Value = client.patch(&url)
175 .bearer_auth(token)
176 .json(&body)
177 .send()
178 .await?
179 .json()
180 .await?;
181
182 Ok(response)
183}
184
185#[allow(dead_code)]
186/// Fetches the permissions required for guild scheduled events.
187///
188/// # Arguments
189///
190/// * `client` - The HTTP client used to send the request.
191/// * `token` - The bot token for authentication.
192///
193/// # Returns
194///
195/// A result containing the permissions requirements as a JSON value.
196pub async fn get_scheduled_event_permissions(client: &Client, token: &str) -> Result<Value, Box<dyn Error>> {
197 let url = "https://discord.com/api/v9/scheduled-events/permissions";
198 let response: Value = client.get(url)
199 .bearer_auth(token)
200 .send()
201 .await?
202 .json()
203 .await?;
204
205 Ok(response)
206}