Rust_Discord_API/utils/message.rs
1use reqwest::Client;
2use serde_json::json;
3use std::error::Error;
4
5/// Sends a message to a specified Discord channel.
6///
7/// # Arguments
8///
9/// * `client` - The HTTP client used to send the request.
10/// * `token` - The bot token for authentication.
11/// * `channel_id` - The ID of the channel to send the message to.
12/// * `content` - The content of the message.
13///
14/// # Returns
15///
16/// A result indicating success or failure.
17#[allow(dead_code)]
18pub async fn send_message(client: &Client, token: &str, channel_id: &str, content: &str) -> Result<(), Box<dyn Error>> {
19 let url = format!("https://discord.com/api/v9/channels/{}/messages", channel_id);
20 let body = json!({ "content": content });
21
22 client.post(&url)
23 .bearer_auth(token)
24 .json(&body)
25 .send()
26 .await?
27 .error_for_status()?;
28
29 Ok(())
30}
31
32/// Edits a message in a specified Discord channel.
33///
34/// # Arguments
35///
36/// * `client` - The HTTP client used to send the request.
37/// * `token` - The bot token for authentication.
38/// * `channel_id` - The ID of the channel where the message is located.
39/// * `message_id` - The ID of the message to edit.
40/// * `new_content` - The new content of the message.
41///
42/// # Returns
43///
44/// A result indicating success or failure.
45#[allow(dead_code)]
46pub async fn edit_message(client: &Client, token: &str, channel_id: &str, message_id: &str, new_content: &str) -> Result<(), Box<dyn Error>> {
47 let url = format!("https://discord.com/api/v9/channels/{}/messages/{}", channel_id, message_id);
48 let body = json!({ "content": new_content });
49
50 client.patch(&url)
51 .bearer_auth(token)
52 .json(&body)
53 .send()
54 .await?
55 .error_for_status()?;
56
57 Ok(())
58}
59
60/// Deletes a message in a specified Discord channel.
61///
62/// # Arguments
63///
64/// * `client` - The HTTP client used to send the request.
65/// * `token` - The bot token for authentication.
66/// * `channel_id` - The ID of the channel where the message is located.
67/// * `message_id` - The ID of the message to delete.
68///
69/// # Returns
70///
71/// A result indicating success or failure.
72#[allow(dead_code)]
73pub async fn delete_message(client: &Client, token: &str, channel_id: &str, message_id: &str) -> Result<(), Box<dyn Error>> {
74 let url = format!("https://discord.com/api/v9/channels/{}/messages/{}", channel_id, message_id);
75
76 client.delete(&url)
77 .bearer_auth(token)
78 .send()
79 .await?
80 .error_for_status()?;
81
82 Ok(())
83}
84
85/// Pins a message in a specified Discord channel.
86///
87/// # Arguments
88///
89/// * `client` - The HTTP client used to send the request.
90/// * `token` - The bot token for authentication.
91/// * `channel_id` - The ID of the channel where the message is located.
92/// * `message_id` - The ID of the message to pin.
93///
94/// # Returns
95///
96/// A result indicating success or failure.
97#[allow(dead_code)]
98pub async fn pin_message(client: &Client, token: &str, channel_id: &str, message_id: &str) -> Result<(), Box<dyn Error>> {
99 let url = format!("https://discord.com/api/v9/channels/{}/pins/{}", channel_id, message_id);
100
101 client.put(&url)
102 .bearer_auth(token)
103 .send()
104 .await?
105 .error_for_status()?;
106
107 Ok(())
108}
109
110/// Unpins a message in a specified Discord channel.
111///
112/// # Arguments
113///
114/// * `client` - The HTTP client used to send the request.
115/// * `token` - The bot token for authentication.
116/// * `channel_id` - The ID of the channel where the message is located.
117/// * `message_id` - The ID of the message to unpin.
118///
119/// # Returns
120///
121/// A result indicating success or failure.
122#[allow(dead_code)]
123pub async fn unpin_message(client: &Client, token: &str, channel_id: &str, message_id: &str) -> Result<(), Box<dyn Error>> {
124 let url = format!("https://discord.com/api/v9/channels/{}/pins/{}", channel_id, message_id);
125
126 client.delete(&url)
127 .bearer_auth(token)
128 .send()
129 .await?
130 .error_for_status()?;
131
132 Ok(())
133}