serenity/utils/
custom_message.rs

1use crate::model::prelude::*;
2
3/// A builder for constructing a personal [`Message`] instance.
4///
5/// This can be useful for emitting a manual [`dispatch`] to the framework, but you don't have a
6/// message in hand, or just have a fragment of its data.
7///
8/// [`dispatch`]: crate::framework::Framework::dispatch
9#[derive(Clone, Default, Debug)]
10pub struct CustomMessage {
11    msg: Message,
12}
13
14impl CustomMessage {
15    /// Constructs a new instance of this builder, alongside a message with dummy data. Use the
16    /// methods to replace the individual bits of this message with valid data.
17    #[inline]
18    #[must_use]
19    pub fn new() -> Self {
20        Self::default()
21    }
22
23    /// Assign the dummy message a proper ID for identification.
24    ///
25    /// If not used, the default value is `MessageId::new(1)`.
26    #[inline]
27    #[must_use]
28    pub fn id(&mut self, id: MessageId) -> &mut Self {
29        self.msg.id = id;
30
31        self
32    }
33
34    /// Assign the dummy message files attached to it.
35    ///
36    /// If not used, the default value is an empty vector (`Vec::default()`).
37    #[inline]
38    pub fn attachments(&mut self, attachments: impl IntoIterator<Item = Attachment>) -> &mut Self {
39        self.msg.attachments = attachments.into_iter().collect();
40
41        self
42    }
43
44    /// Assign the dummy message its author.
45    ///
46    /// If not used, the default value is a dummy [`User`].
47    #[inline]
48    pub fn author(&mut self, user: User) -> &mut Self {
49        self.msg.author = user;
50
51        self
52    }
53
54    /// Assign the dummy message its origin channel's ID.
55    ///
56    /// If not used, the default value is `ChannelId::new(1)`.
57    #[inline]
58    pub fn channel_id(&mut self, channel_id: ChannelId) -> &mut Self {
59        self.msg.channel_id = channel_id;
60
61        self
62    }
63
64    /// Assign the dummy message its contents.
65    ///
66    /// If not used, the default value is an empty string (`String::default()`).
67    #[inline]
68    pub fn content(&mut self, s: impl Into<String>) -> &mut Self {
69        self.msg.content = s.into();
70
71        self
72    }
73
74    /// Assign the dummy message the timestamp it was edited.
75    ///
76    /// If not used, the default value is [`None`] (not all messages are edited).
77    #[inline]
78    pub fn edited_timestamp<T: Into<Timestamp>>(&mut self, timestamp: T) -> &mut Self {
79        self.msg.edited_timestamp = Some(timestamp.into());
80
81        self
82    }
83
84    /// Assign the dummy message embeds.
85    ///
86    /// If not used, the default value is an empty vector (`Vec::default()`).
87    #[inline]
88    pub fn embeds(&mut self, embeds: impl IntoIterator<Item = Embed>) -> &mut Self {
89        self.msg.embeds = embeds.into_iter().collect();
90
91        self
92    }
93
94    /// Assign the dummy message its origin guild's ID.
95    ///
96    /// If not used, the default value is [`None`] (not all messages are sent in guilds).
97    #[inline]
98    pub fn guild_id(&mut self, guild_id: GuildId) -> &mut Self {
99        self.msg.guild_id = Some(guild_id);
100
101        self
102    }
103
104    /// Assign the dummy message its type.
105    ///
106    /// If not used, the default value is [`MessageType::Regular`].
107    #[inline]
108    pub fn kind(&mut self, kind: MessageType) -> &mut Self {
109        self.msg.kind = kind;
110
111        self
112    }
113
114    /// Assign the dummy message member data pertaining to its [author].
115    ///
116    /// If not used, the default value is [`None`] (not all messages are sent in guilds).
117    ///
118    /// [author]: Self::author
119    #[inline]
120    pub fn member(&mut self, member: PartialMember) -> &mut Self {
121        self.msg.member = Some(Box::new(member));
122
123        self
124    }
125
126    /// Assign the dummy message a flag whether it mentions everyone (`@everyone`).
127    ///
128    /// If not used, the default value is `false`.
129    #[inline]
130    pub fn mention_everyone(&mut self, mentions: bool) -> &mut Self {
131        self.msg.mention_everyone = mentions;
132
133        self
134    }
135
136    /// Assign the dummy message a list of roles it mentions.
137    ///
138    /// If not used, the default value is an empty vector (`Vec::default()`).
139    #[inline]
140    pub fn mention_roles(&mut self, roles: impl IntoIterator<Item = RoleId>) -> &mut Self {
141        self.msg.mention_roles = roles.into_iter().collect();
142
143        self
144    }
145
146    /// Assign the dummy message a list of mentions.
147    ///
148    /// If not used, the default value is an empty vector (`Vec::default()`).
149    #[inline]
150    pub fn mentions(&mut self, mentions: impl IntoIterator<Item = User>) -> &mut Self {
151        self.msg.mentions = mentions.into_iter().collect();
152
153        self
154    }
155
156    /// Assign the dummy message a flag whether it's been pinned.
157    ///
158    /// If not used, the default value is `false`.
159    #[inline]
160    pub fn pinned(&mut self, pinned: bool) -> &mut Self {
161        self.msg.pinned = pinned;
162
163        self
164    }
165
166    /// Assign the dummy message a list of emojis it was reacted with.
167    ///
168    /// If not used, the default value is an empty vector (`Vec::default()`).
169    #[inline]
170    pub fn reactions(&mut self, reactions: impl IntoIterator<Item = MessageReaction>) -> &mut Self {
171        self.msg.reactions = reactions.into_iter().collect();
172
173        self
174    }
175
176    /// Assign the dummy message the timestamp it was created at.
177    ///
178    /// If not used, the default value is the current local time.
179    #[inline]
180    pub fn timestamp<T: Into<Timestamp>>(&mut self, timestamp: T) -> &mut Self {
181        self.msg.timestamp = timestamp.into();
182
183        self
184    }
185
186    /// Assign the dummy message a flag whether it'll be read by a Text-To-Speech program.
187    ///
188    /// If not used, the default value is `false`.
189    #[inline]
190    pub fn tts(&mut self, tts: bool) -> &mut Self {
191        self.msg.tts = tts;
192
193        self
194    }
195
196    /// Assign the dummy message the webhook author's ID.
197    ///
198    /// If not used, the default value is [`None`] (not all messages are sent by webhooks).
199    #[inline]
200    pub fn webhook_id(&mut self, id: WebhookId) -> &mut Self {
201        self.msg.webhook_id = Some(id);
202
203        self
204    }
205
206    /// Consume this builder and return the constructed message.
207    #[inline]
208    #[must_use]
209    pub fn build(self) -> Message {
210        self.msg
211    }
212}