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
//! Structs related to Discord messages in a guild channel.
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::guild::GuildMember;
use crate::snowflake::Snowflake;
use crate::User;

pub use self::embed::*;
pub use self::emoji::*;
pub use self::webhook::Webhook;

mod embed;
mod webhook;
mod emoji;

/// A message sent in a channel on Discord.
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Message {
    /// The message ID of the message.
    pub id: Snowflake,
    /// The ID of the channel that the message was sent in.
    pub channel_id: Snowflake,
    /// The ID of the guild that the message was sent in.
    #[serde(default)]
    pub guild_id: Snowflake,
    /// The author of the message.
    pub author: User,
    /// The contents of this message.
    pub content: String,
    /// The guild member form of the message author.
    #[serde(default)]
    pub member: GuildMember,
    /// The time that this message was sent.
    pub timestamp: String,
    /// When this message was edited, if applicable.
    pub edited_timestamp: Option<String>,
    /// Whether or not this was a TTS message.
    pub tts: bool,
    /// Whether or not this message mentioned everyone.
    pub mention_everyone: bool,
    /// Roles that were mentioned in this message.
    pub mention_roles: Vec<Snowflake>,
    /// The message's attached files, if any.
    pub attachments: Vec<MessageAttachment>,
    /// Any embeds sent with this message.
    pub embeds: Vec<Embed>,
    /// The message's reactions.
    #[serde(default)]
    pub reactions: Vec<MessageReaction>,
    /// A snowflake used to validate that a message was sent.
    #[serde(default)]
    pub nonce: Option<Snowflake>,
    /// Whether or not the message is pinned.
    pub pinned: bool,
    /// The ID of the webhook if the message was sent by a webhook.
    #[serde(default)]
    pub webhook_id: Snowflake,
    /// The type of message sent.
    #[serde(rename = "type")]
    pub kind: MessageType,
    /// Message Activity sent with rich-presence embeds.
    #[serde(default)]
    pub activity: MessageActivity,
    /// Message Application ent with Rich Presence embeds.
    #[serde(default)]
    pub application: MessageApplication,
}

/// Represents a message that is being sent to Discord.
#[derive(Serialize, Clone, Debug, Default)]
pub struct CreateMessage {
    /// The content of this message.
    #[serde(skip_serializing_if = "Option::is_none")]
    content: Option<String>,
    /// The embed that this message has.
    #[serde(skip_serializing_if = "Option::is_none")]
    embed: Option<Embed>,
    /// Whether or not this message is a TTS message.
    #[serde(skip_serializing_if = "Option::is_none")]
    tts: Option<bool>,
}

impl CreateMessage {
    /// Creates a new message with the specified content string.
    pub fn new() -> Self {
        CreateMessage {
            content: None,
            embed: None,
            tts: None
        }
    }

    /// Adds content to the message.
    pub fn with_content(mut self, content: &str) -> Self {
        self.content = Some(content.to_string());

        self
    }

    /// Adds an Embed object to the message.
    pub fn with_embed(mut self, embed: Embed) -> Self {
        self.embed = Some(embed);

        self
    }

    /// Whether or not this message will be a TTS message.
    pub fn tts(mut self, opt: bool) -> Self {
        self.tts = Some(opt);

        self
    }
}

/// Represents a message that is being edited in a Discord channel.
#[derive(Serialize, Clone, Debug, Default)]
pub struct EditMessage {
    #[serde(skip_serializing_if = "Option::is_none")]
    content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    embed: Option<Embed>
}

impl EditMessage {
    pub fn new() -> EditMessage {
        EditMessage {
            content: None,
            embed: None
        }
    }

    /// Adds the content to edit into this message.
    pub fn with_content(mut self, content: &str) -> Self {
        self.content = Some(content.to_string());

        self
    }


    /// Adds an embed to be edited into this message.
    pub fn with_embed(mut self, embed: Embed) -> Self {
        self.embed = Some(embed);

        self
    }
}

/// Options for retrieving messages from a channel.
#[derive(Serialize, Clone, Debug)]
pub struct ChannelMessagesQuery {
    #[serde(skip_serializing_if = "Option::is_none")]
    around: Option<Snowflake>,
    #[serde(skip_serializing_if = "Option::is_none")]
    before: Option<Snowflake>,
    #[serde(skip_serializing_if = "Option::is_none")]
    after: Option<Snowflake>,
    limit: i32
}

impl ChannelMessagesQuery {
    pub fn new() -> ChannelMessagesQuery {
        ChannelMessagesQuery {
            around: None,
            before: None,
            after: None,
            limit: 50
        }
    }


    /// Fetch messages from this channel that are around this message ID.
    pub fn around(mut self, id: u64) -> Self {
        if self.after.is_some() || self.before.is_some() {
            return self;
        };
        self.around = Some(id.into());
        self
    }

    /// Fetch messages from this channel that are before this message ID.
    pub fn before(mut self, id: u64) -> Self {
        if self.around.is_some() || self.after.is_some() {
            return self;
        };
        self.before = Some(id.into());
        self
    }

    /// Fetch messages in this channels that are after this message ID.
    pub fn after(mut self, id: u64) -> Self {
        if self.around.is_some() || self.before.is_some() {
            return self;
        };
        self.after = Some(id.into());
        self
    }
}


/// Represents an attachment sent by a user.
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct MessageAttachment {
    /// The attachment ID.
    pub id: Snowflake,
    /// The name of the file attached.
    pub filename: String,
    /// The size of the file in bytes.
    pub size: i32,
    /// The source URL of the file.
    pub url: String,
    /// A proxied URL of the file.
    pub proxy_url: String,
    /// The height of the file, if it is an image.
    pub height: Option<i32>,
    /// The width of the file, if it is an image.
    pub width: Option<i32>

}
/// A Rich Presence Message activity.
#[derive(Deserialize, Serialize, Clone, Debug, Default)]
pub struct MessageActivity {
    /// The type of message activity.
    #[serde(rename = "type")]
    pub kind: MessageActivityType,
    /// The party ID from a Rich Presence event.
    #[serde(default)]
    pub party_id: String
}

/// A Rich Presence Message Application.
#[derive(Deserialize, Serialize, Clone, Debug, Default)]
pub struct MessageApplication {
    /// The ID of the application.
    pub id: Snowflake,
    /// The ID of the embeds's image.
    pub cover_image: String,
    /// The application description.
    pub description: String,
    /// The ID of the application icon.
    pub icon: String,
    /// The name of the application.
    pub name: String
}

/// A list of Message types.
#[derive(Deserialize_repr, Debug, Clone, Serialize_repr)]
#[repr(u8)]
pub enum MessageType {
    Default,
    RecipientAdd,
    RecipientRemove,
    Call,
    ChannelNameChange,
    ChannelIconChange,
    ChannelPinnedMessage,
    GuildMemberJoin
}

impl Default for MessageType {
    fn default() -> Self {
        MessageType::Default
    }
}

/// A list of Message Activity types.
#[derive(Deserialize, Serialize, Debug, Clone)]

pub enum MessageActivityType {
    Join = 1,
    Spectate,
    Listen = 3,
    JoinRequest = 5
}

impl Default for MessageActivityType {
    fn default() -> Self {
        MessageActivityType::Join
    }
}