discord_webhook2/message/
mod.rs1use 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 pub content: Option<String>,
19 pub username: Option<String>,
21 pub avatar_url: Option<String>,
23 pub tts: Option<bool>,
25
26 pub embeds: Option<Vec<Embed>>,
28
29 pub poll: Option<PollCreate>,
31
32 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}