Rust_Discord_API/utils/
user.rs

1use reqwest::Client;
2use serde_json::Value;
3use std::error::Error;
4
5#[allow(dead_code)]
6/// Fetches the current user's information.
7///
8/// # Arguments
9///
10/// * `client` - The HTTP client used to send the request.
11/// * `token` - The bot token for authentication.
12///
13/// # Returns
14///
15/// A result containing the current user's information as a JSON value.
16pub async fn get_current_user(client: &Client, token: &str) -> Result<Value, Box<dyn Error>> {
17    let url = "https://discord.com/api/v9/users/@me";
18    let response: Value = client.get(url)
19        .bearer_auth(token)
20        .send()
21        .await?
22        .json()
23        .await?;
24    
25    Ok(response)
26}
27
28#[allow(dead_code)]
29/// Fetches a user's information.
30///
31/// # Arguments
32///
33/// * `client` - The HTTP client used to send the request.
34/// * `token` - The bot token for authentication.
35/// * `user_id` - The ID of the user to fetch.
36///
37/// # Returns
38///
39/// A result containing the user's information as a JSON value.
40pub async fn get_user(client: &Client, token: &str, user_id: &str) -> Result<Value, Box<dyn Error>> {
41    let url = format!("https://discord.com/api/v9/users/{}", user_id);
42    let response: Value = client.get(&url)
43        .bearer_auth(token)
44        .send()
45        .await?
46        .json()
47        .await?;
48    
49    Ok(response)
50}
51
52#[allow(dead_code)]
53/// Modifies the current user's information.
54///
55/// # Arguments
56///
57/// * `client` - The HTTP client used to send the request.
58/// * `token` - The bot token for authentication.
59/// * `settings` - The JSON value of the settings to update.
60///
61/// # Returns
62///
63/// A result indicating success or failure.
64pub async fn modify_current_user(client: &Client, token: &str, settings: Value) -> Result<Value, Box<dyn Error>> {
65    let url = "https://discord.com/api/v9/users/@me";
66    let response: Value = client.patch(url)
67        .bearer_auth(token)
68        .json(&settings)
69        .send()
70        .await?
71        .json()
72        .await?;
73    
74    Ok(response)
75}
76
77#[allow(dead_code)]
78/// Fetches the current user's guilds.
79///
80/// # Arguments
81///
82/// * `client` - The HTTP client used to send the request.
83/// * `token` - The bot token for authentication.
84///
85/// # Returns
86///
87/// A result containing the current user's guilds as a JSON value.
88pub async fn get_current_user_guilds(client: &Client, token: &str) -> Result<Value, Box<dyn Error>> {
89    let url = "https://discord.com/api/v9/users/@me/guilds";
90    let response: Value = client.get(url)
91        .bearer_auth(token)
92        .send()
93        .await?
94        .json()
95        .await?;
96    
97    Ok(response)
98}
99
100#[allow(dead_code)]
101/// Fetches the current user's guild member information for a specific guild.
102///
103/// # Arguments
104///
105/// * `client` - The HTTP client used to send the request.
106/// * `token` - The bot token for authentication.
107/// * `guild_id` - The ID of the guild to fetch member information for.
108///
109/// # Returns
110///
111/// A result containing the guild member information as a JSON value.
112pub async fn get_current_user_guild_member(client: &Client, token: &str, guild_id: &str) -> Result<Value, Box<dyn Error>> {
113    let url = format!("https://discord.com/api/v9/users/@me/guilds/{}/member", guild_id);
114    let response: Value = client.get(&url)
115        .bearer_auth(token)
116        .send()
117        .await?
118        .json()
119        .await?;
120    
121    Ok(response)
122}
123
124#[allow(dead_code)]
125/// Leaves a guild.
126///
127/// # Arguments
128///
129/// * `client` - The HTTP client used to send the request.
130/// * `token` - The bot token for authentication.
131/// * `guild_id` - The ID of the guild to leave.
132///
133/// # Returns
134///
135/// A result indicating success or failure.
136pub async fn leave_guild(client: &Client, token: &str, guild_id: &str) -> Result<(), Box<dyn Error>> {
137    let url = format!("https://discord.com/api/v9/users/@me/guilds/{}", guild_id);
138    
139    client.delete(&url)
140        .bearer_auth(token)
141        .send()
142        .await?
143        .error_for_status()?;
144    
145    Ok(())
146}
147
148#[allow(dead_code)]
149/// Creates a DM channel with a user.
150///
151/// # Arguments
152///
153/// * `client` - The HTTP client used to send the request.
154/// * `token` - The bot token for authentication.
155/// * `recipient_id` - The ID of the user to create a DM with.
156///
157/// # Returns
158///
159/// A result containing the created DM channel information as a JSON value.
160pub async fn create_dm(client: &Client, token: &str, recipient_id: &str) -> Result<Value, Box<dyn Error>> {
161    let url = "https://discord.com/api/v9/users/@me/channels";
162    let body = serde_json::json!({ "recipient_id": recipient_id });
163    let response: Value = client.post(url)
164        .bearer_auth(token)
165        .json(&body)
166        .send()
167        .await?
168        .json()
169        .await?;
170    
171    Ok(response)
172}
173
174#[allow(dead_code)]
175/// Creates a group DM channel.
176///
177/// # Arguments
178///
179/// * `client` - The HTTP client used to send the request.
180/// * `token` - The bot token for authentication.
181/// * `access_tokens` - The OAuth2 access tokens of the users to add.
182/// * `nicks` - The nicknames of the users to add.
183///
184/// # Returns
185///
186/// A result containing the created group DM channel information as a JSON value.
187pub async fn create_group_dm(client: &Client, token: &str, access_tokens: Vec<&str>, nicks: Value) -> Result<Value, Box<dyn Error>> {
188    let url = "https://discord.com/api/v9/users/@me/channels";
189    let body = serde_json::json!({ "access_tokens": access_tokens, "nicks": nicks });
190    let response: Value = client.post(url)
191        .bearer_auth(token)
192        .json(&body)
193        .send()
194        .await?
195        .json()
196        .await?;
197    
198    Ok(response)
199}
200
201#[allow(dead_code)]
202/// Fetches the current user's connections.
203///
204/// # Arguments
205///
206/// * `client` - The HTTP client used to send the request.
207/// * `token` - The bot token for authentication.
208///
209/// # Returns
210///
211/// A result containing the current user's connections as a JSON value.
212pub async fn get_current_user_connections(client: &Client, token: &str) -> Result<Value, Box<dyn Error>> {
213    let url = "https://discord.com/api/v9/users/@me/connections";
214    let response: Value = client.get(url)
215        .bearer_auth(token)
216        .send()
217        .await?
218        .json()
219        .await?;
220    
221    Ok(response)
222}
223
224#[allow(dead_code)]
225/// Fetches the current user's application role connection.
226///
227/// # Arguments
228///
229/// * `client` - The HTTP client used to send the request.
230/// * `token` - The bot token for authentication.
231/// * `application_id` - The ID of the application to fetch role connection for.
232///
233/// # Returns
234///
235/// A result containing the role connection information as a JSON value.
236pub async fn get_current_user_application_role_connection(client: &Client, token: &str, application_id: &str) -> Result<Value, Box<dyn Error>> {
237    let url = format!("https://discord.com/api/v9/users/@me/applications/{}/role-connection", application_id);
238    let response: Value = client.get(&url)
239        .bearer_auth(token)
240        .send()
241        .await?
242        .json()
243        .await?;
244    
245    Ok(response)
246}
247
248#[allow(dead_code)]
249/// Updates the current user's application role connection.
250///
251/// # Arguments
252///
253/// * `client` - The HTTP client used to send the request.
254/// * `token` - The bot token for authentication.
255/// * `application_id` - The ID of the application to update role connection for.
256/// * `role_connection` - The JSON value of the role connection settings.
257///
258/// # Returns
259///
260/// A result indicating success or failure.
261pub async fn update_current_user_application_role_connection(client: &Client, token: &str, application_id: &str, role_connection: Value) -> Result<Value, Box<dyn Error>> {
262    let url = format!("https://discord.com/api/v9/users/@me/applications/{}/role-connection", application_id);
263    let response: Value = client.put(&url)
264        .bearer_auth(token)
265        .json(&role_connection)
266        .send()
267        .await?
268        .json()
269        .await?;
270    
271    Ok(response)
272}
273
274#[allow(dead_code)]
275/// Kicks a user from a guild.
276///
277/// # Arguments
278///
279/// * `client` - The HTTP client used to send the request.
280/// * `token` - The bot token for authentication.
281/// * `guild_id` - The ID of the guild.
282/// * `user_id` - The ID of the user to kick.
283///
284/// # Returns
285///
286/// A result indicating success or failure.
287pub async fn kick_user(client: &Client, token: &str, guild_id: &str, user_id: &str) -> Result<(), Box<dyn Error>> {
288    let url = format!("https://discord.com/api/v9/guilds/{}/members/{}", guild_id, user_id);
289    
290    client.delete(&url)
291        .bearer_auth(token)
292        .send()
293        .await?
294        .error_for_status()?;
295    
296    Ok(())
297}
298
299#[allow(dead_code)]
300/// Bans a user from a guild.
301///
302/// # Arguments
303///
304/// * `client` - The HTTP client used to send the request.
305/// * `token` - The bot token for authentication.
306/// * `guild_id` - The ID of the guild.
307/// * `user_id` - The ID of the user to ban.
308/// * `delete_message_days` - The number of days to delete messages for (0-7).
309/// * `reason` - The reason for the ban.
310///
311/// # Returns
312///
313/// A result indicating success or failure.
314pub async fn ban_user(client: &Client, token: &str, guild_id: &str, user_id: &str, delete_message_days: u8, reason: &str) -> Result<(), Box<dyn Error>> {
315    let url = format!("https://discord.com/api/v9/guilds/{}/bans/{}", guild_id, user_id);
316    let body = serde_json::json!({
317        "delete_message_days": delete_message_days,
318        "reason": reason
319    });
320    
321    client.put(&url)
322        .bearer_auth(token)
323        .json(&body)
324        .send()
325        .await?
326        .error_for_status()?;
327    
328    Ok(())
329}