Skip to main content

mutiny_rs/builders/
create_message.rs

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