1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use chrono::{DateTime, Utc};
use serde_json::Value;

use crate::model::prelude::*;

/// A builder for constructing a personal [`Message`] instance.
/// This can be useful for emitting a manual [`dispatch`] to the framework,
/// but you don't have a message in hand, or just have a fragment of its data.
///
/// [`dispatch`]: crate::framework::Framework::dispatch
#[derive(Debug, Clone)]
pub struct CustomMessage {
    msg: Message,
}

impl CustomMessage {
    /// Constructs a new instance of this builder, alongside a message
    /// with dummy data. Use the methods to replace the individual bits
    /// of this message with valid data.
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Assign the dummy message a proper ID for identification.
    ///
    /// If not used, the default value is `MessageId(0)`.
    #[inline]
    pub fn id(&mut self, id: MessageId) -> &mut Self {
        self.msg.id = id;

        self
    }

    /// Assign the dummy message files attached to it.
    ///
    /// If not used, the default value is an empty vector (`Vec::default()`).
    #[inline]
    pub fn attachments<It>(&mut self, attachments: It) -> &mut Self
    where
        It: IntoIterator<Item = Attachment>,
    {
        self.msg.attachments = attachments.into_iter().collect();

        self
    }

    /// Assign the dummy message its author.
    ///
    /// If not used, the default value is a dummy [`User`].
    #[inline]
    pub fn author(&mut self, user: User) -> &mut Self {
        self.msg.author = user;

        self
    }

    /// Assign the dummy message its origin channel's ID.
    ///
    /// If not used, the default value is `ChannelId(0)`.
    #[inline]
    pub fn channel_id(&mut self, channel_id: ChannelId) -> &mut Self {
        self.msg.channel_id = channel_id;

        self
    }

    /// Assign the dummy message its contents.
    ///
    /// If not used, the default value is an empty string (`String::default()`).
    #[inline]
    pub fn content<T: ToString>(&mut self, s: T) -> &mut Self {
        self.msg.content = s.to_string();

        self
    }

    /// Assign the dummy message the timestamp it was edited.
    ///
    /// If not used, the default value is [`None`] (not all messages are edited).
    #[inline]
    pub fn edited_timestamp(&mut self, timestamp: DateTime<Utc>) -> &mut Self {
        self.msg.edited_timestamp = Some(timestamp);

        self
    }

    /// Assign the dummy message embeds.
    ///
    /// If not used, the default value is an empty vector (`Vec::default()`).
    #[inline]
    pub fn embeds<It>(&mut self, embeds: It) -> &mut Self
    where
        It: IntoIterator<Item = Embed>,
    {
        self.msg.embeds = embeds.into_iter().collect();

        self
    }

    /// Assign the dummy message its origin guild's ID.
    ///
    /// If not used, the default value is [`None`] (not all messages are sent in guilds).
    #[inline]
    pub fn guild_id(&mut self, guild_id: GuildId) -> &mut Self {
        self.msg.guild_id = Some(guild_id);

        self
    }

    /// Assign the dummy message its type.
    ///
    /// If not used, the default value is [`MessageType::Regular`].
    #[inline]
    pub fn kind(&mut self, kind: MessageType) -> &mut Self {
        self.msg.kind = kind;

        self
    }

    /// Assign the dummy message member data pertaining to its [author].
    ///
    /// If not used, the default value is [`None`] (not all messages are sent in guilds).
    ///
    /// [author]: Self::author
    #[inline]
    pub fn member(&mut self, member: PartialMember) -> &mut Self {
        self.msg.member = Some(member);

        self
    }

    /// Assign the dummy message a flag whether it mentions everyone (`@everyone`).
    ///
    /// If not used, the default value is `false`.
    #[inline]
    pub fn mention_everyone(&mut self, mentions: bool) -> &mut Self {
        self.msg.mention_everyone = mentions;

        self
    }

    /// Assign the dummy message a list of roles it mentions.
    ///
    /// If not used, the default value is an empty vector (`Vec::default()`).
    #[inline]
    pub fn mention_roles<It>(&mut self, roles: It) -> &mut Self
    where
        It: IntoIterator<Item = RoleId>,
    {
        self.msg.mention_roles = roles.into_iter().collect();

        self
    }

    /// Assign the dummy message a list of mentions.
    ///
    /// If not used, the default value is an empty vector (`Vec::default()`).
    #[inline]
    pub fn mentions<It>(&mut self, mentions: It) -> &mut Self
    where
        It: IntoIterator<Item = User>,
    {
        self.msg.mentions = mentions.into_iter().collect();

        self
    }

    /// Assign the dummy message a flag whether it's been pinned.
    ///
    /// If not used, the default value is `false`.
    #[inline]
    pub fn pinned(&mut self, pinned: bool) -> &mut Self {
        self.msg.pinned = pinned;

        self
    }

    /// Assign the dummy message a list of emojis it was reacted with.
    ///
    /// If not used, the default value is an empty vector (`Vec::default()`).
    #[inline]
    pub fn reactions<It>(&mut self, reactions: It) -> &mut Self
    where
        It: IntoIterator<Item = MessageReaction>,
    {
        self.msg.reactions = reactions.into_iter().collect();

        self
    }

    /// Assign the dummy message the timestamp it was created at.
    ///
    /// If not used, the default value is the current local time.
    #[inline]
    pub fn timestamp(&mut self, timestamp: DateTime<Utc>) -> &mut Self {
        self.msg.timestamp = timestamp;

        self
    }

    /// Assign the dummy message a flag whether it'll be read by a Text-To-Speech program.
    ///
    /// If not used, the default value is `false`.
    #[inline]
    pub fn tts(&mut self, tts: bool) -> &mut Self {
        self.msg.tts = tts;

        self
    }

    /// Assign the dummy message the webhook author's ID.
    ///
    /// If not used, the default value is [`None`] (not all messages are sent by webhooks).
    #[inline]
    pub fn webhook_id(&mut self, id: WebhookId) -> &mut Self {
        self.msg.webhook_id = Some(id);

        self
    }

    /// Consume this builder and return the constructed message.
    #[inline]
    pub fn build(self) -> Message {
        self.msg
    }
}

impl Default for CustomMessage {
    #[inline]
    fn default() -> Self {
        CustomMessage {
            msg: dummy_message(),
        }
    }
}

#[inline]
fn dummy_message() -> Message {
    Message {
        id: MessageId::default(),
        attachments: Vec::new(),
        author: User {
            id: UserId::default(),
            avatar: None,
            bot: false,
            discriminator: 0x0000,
            name: String::new(),
            public_flags: None,
            banner: None,
            accent_colour: None,
        },
        channel_id: ChannelId::default(),
        content: String::new(),
        edited_timestamp: None,
        embeds: Vec::new(),
        guild_id: None,
        kind: MessageType::Regular,
        member: None,
        mention_everyone: false,
        mention_roles: Vec::new(),
        mention_channels: Vec::new(),
        mentions: Vec::new(),
        nonce: Value::Null,
        pinned: false,
        reactions: Vec::new(),
        tts: false,
        webhook_id: None,
        timestamp: Utc::now(),
        activity: None,
        application: None,
        message_reference: None,
        flags: None,
        stickers: Vec::new(),
        referenced_message: None,
        #[cfg(feature = "unstable_discord_api")]
        interaction: None,
        #[cfg(feature = "unstable_discord_api")]
        components: vec![],
    }
}