Skip to main content

zaun_bot/
body.rs

1//! [`Reply`]: a message body builder.
2
3use crate::types::{Component, Embed, Id, MessageBody, MessageFlags};
4use crate::{ActionRow, Container, EmbedBuilder};
5
6/// Builds a [`MessageBody`]. `Reply::from("text")` is the common case.
7#[derive(Debug, Clone)]
8pub struct Reply(pub(crate) MessageBody);
9
10impl Default for Reply {
11    fn default() -> Self {
12        Self(MessageBody {
13            content: None,
14            embeds: Vec::new(),
15            components: Vec::new(),
16            attachments: Vec::new(),
17            mentions: None,
18            flags: MessageFlags::empty(),
19        })
20    }
21}
22
23impl Reply {
24    /// An empty body.
25    #[must_use]
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    /// Sets the text (≤ 4 000 scalars).
31    #[must_use]
32    pub fn content(mut self, text: impl Into<String>) -> Self {
33        self.0.content = Some(text.into());
34        self
35    }
36
37    /// Adds an embed (≤ 10).
38    #[must_use]
39    pub fn embed(mut self, embed: impl Into<Embed>) -> Self {
40        self.0.embeds.push(embed.into());
41        self
42    }
43
44    /// Adds a row of buttons or one select.
45    #[must_use]
46    pub fn row(mut self, row: ActionRow) -> Self {
47        self.0.components.push(Component::ActionRow(row.build()));
48        self
49    }
50
51    /// Adds any top-level component. Anything but an action row needs [`v2`](Self::v2).
52    #[must_use]
53    pub fn component(mut self, component: Component) -> Self {
54        self.0.components.push(component);
55        self
56    }
57
58    /// Adds a Components V2 container and switches the message to V2 layout.
59    #[must_use]
60    pub fn container(self, container: Container) -> Self {
61        self.v2().component(Component::Container(container.build()))
62    }
63
64    /// Uses Components V2 layout: no `content` or embeds, components carry everything.
65    #[must_use]
66    pub fn v2(mut self) -> Self {
67        self.0.flags |= MessageFlags::COMPONENTS_V2;
68        self
69    }
70
71    /// Only the invoker sees it (interaction answers only).
72    #[must_use]
73    pub fn ephemeral(mut self) -> Self {
74        self.0.flags |= MessageFlags::EPHEMERAL;
75        self
76    }
77
78    /// Delivers without notifying anyone.
79    #[must_use]
80    pub fn silent(mut self) -> Self {
81        self.0.flags |= MessageFlags::SUPPRESS_NOTIFICATIONS;
82        self
83    }
84
85    /// Attaches a file uploaded with [`crate::messages::upload`].
86    #[must_use]
87    pub fn attachment(mut self, id: impl Into<Id>) -> Self {
88        self.0.attachments.push(id.into());
89        self
90    }
91
92    /// The finished body.
93    #[must_use]
94    pub fn build(self) -> MessageBody {
95        self.0
96    }
97}
98
99impl From<&str> for Reply {
100    fn from(text: &str) -> Self {
101        Self::new().content(text)
102    }
103}
104
105impl From<String> for Reply {
106    fn from(text: String) -> Self {
107        Self::new().content(text)
108    }
109}
110
111impl From<EmbedBuilder> for Reply {
112    fn from(embed: EmbedBuilder) -> Self {
113        Self::new().embed(embed)
114    }
115}