discord_webhook2/message/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use crate::id::DiscordID;
4use crate::message::embed::Embed;
5use crate::message::flags::MessageFlags;
6use crate::message::poll::create::PollCreate;
7use crate::message::poll::media::PollMedia;
8
9pub mod embed;
10pub mod emoji;
11pub mod flags;
12pub mod poll;
13
14#[derive(Serialize, Deserialize, Debug, Clone)]
15pub struct Message {
16    pub id: Option<DiscordID>,
17    /// The message contents (up to 2000 characters)
18    pub content: Option<String>,
19    /// Override the default username of the webhook
20    pub username: Option<String>,
21    /// Override the default avatar of the webhook
22    pub avatar_url: Option<String>,
23    /// Enables Text-To-Speech
24    pub tts: Option<bool>,
25
26    /// Vector of up to 10 Embed objects
27    pub embeds: Option<Vec<Embed>>,
28
29    /// Well, a pool
30    pub poll: Option<PollCreate>,
31
32    /// Message flags combined as a bitfield
33    /// (only `suppress_embeds` and `suppress_notifications` can be set can be set by webhooks)
34    pub flags: Option<MessageFlags>,
35}
36
37impl Message {
38    pub fn new<Func>(function: Func) -> Self
39    where
40        Func: FnOnce(Message) -> Message,
41    {
42        function(Self {
43            id: None,
44            content: None,
45            username: None,
46            avatar_url: None,
47            tts: None,
48            embeds: None,
49            poll: None,
50            flags: None,
51        })
52    }
53
54    pub fn content(mut self, content: impl Into<String>) -> Self {
55        self.content = Some(content.into());
56        self
57    }
58
59    pub fn username(mut self, username: impl Into<String>) -> Self {
60        self.username = Some(username.into());
61        self
62    }
63
64    pub fn avatar_url(mut self, avatar_url: impl Into<String>) -> Self {
65        self.avatar_url = Some(avatar_url.into());
66        self
67    }
68
69    pub fn tts(mut self, tts: bool) -> Self {
70        self.tts = Some(tts);
71        self
72    }
73
74    pub fn embed<Func>(mut self, function: Func) -> Self
75    where
76        Func: FnOnce(Embed) -> Embed,
77    {
78        let embed = function(Embed::new());
79
80        match &mut self.embeds {
81            None => self.embeds = Some(vec![embed]),
82            Some(ref mut vec) => vec.push(embed),
83        };
84        self
85    }
86
87    pub fn poll<Func1, Func2>(mut self, title: Func1, content: Func2) -> Self
88    where
89        Func1: FnOnce(PollMedia) -> PollMedia,
90        Func2: FnOnce(PollCreate) -> PollCreate,
91    {
92        self.poll = Some(content(PollCreate::new(title(PollMedia::new()))));
93        self
94    }
95}