mutiny_rs/builders/
create_message.rs

1use crate::builders::CreateEmbed;
2use crate::http::{Http, HttpError};
3use crate::model::channel::ChannelId;
4use crate::model::message::Message;
5use crate::model::message::Replies;
6use serde::Serialize;
7
8#[derive(Debug, Default, Serialize)]
9pub struct CreateMessage {
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub content: Option<String>,
12
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub nonce: Option<String>,
15
16    #[serde(skip_serializing_if = "Vec::is_empty")]
17    pub attachments: Vec<String>,
18
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub replies: Option<Vec<Replies>>,
21
22    #[serde(skip_serializing_if = "Vec::is_empty")]
23    pub embeds: Vec<CreateEmbed>,
24}
25impl CreateMessage {
26    pub fn new() -> Self {
27        Default::default()
28    }
29    pub fn content(mut self, content: impl Into<String>) -> Self {
30        self.content = Some(content.into());
31        self
32    }
33    pub fn nonce(mut self, nonce: Option<String>) -> Self {
34        self.nonce = nonce;
35        self
36    }
37    pub fn attachments(mut self, attachments: Vec<String>) -> Self {
38        self.attachments = attachments;
39        self
40    }
41    /// Add replies using message ID's
42    ///
43    /// **Note**: Having more than 5 will cause API error, and you can't use the same ID twice
44    pub fn replies(mut self, replies: Replies) -> Self {
45        self.replies = Some(vec![replies]);
46        self
47    }
48    /// Add multiple embeds for the message.
49    ///
50    /// **Note**: This will keep all existing embeds. Use [`Self::embeds()`] to replace existing
51    /// embeds.
52    pub fn add_embeds(mut self, embeds: Vec<CreateEmbed>) -> Self {
53        self.embeds.extend(embeds);
54        self
55    }
56
57    /// Set an embed for the message.
58    ///
59    /// **Note**: This will replace all existing embeds. Use [`Self::add_embed()`] to keep existing
60    /// embeds.
61    pub fn embed(self, embed: CreateEmbed) -> Self {
62        self.embeds(vec![embed])
63    }
64
65    /// Set multiple embeds for the message.
66    ///
67    /// **Note**: This will replace all existing embeds. Use [`Self::add_embeds()`] to keep existing
68    /// embeds.
69    pub fn embeds(mut self, embeds: Vec<CreateEmbed>) -> Self {
70        self.embeds = embeds;
71        self
72    }
73    /// Sends the message
74    pub(crate) async fn execute(self, http: &Http, channel_id: &ChannelId) -> Result<Message, HttpError> {
75        let route = format!("/channels/{channel_id}/messages");
76        let response = http.post(&route, &self).await?;
77        Ok(response)
78    }
79}