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
/// Type used for IDs
pub type Snowflake = String;

#[derive(Debug)]
/// Known events that may be received
pub enum Event {
    /// Connection established
    Ready(ReadyData),
    /// Message received
    MessageCreate(ReceivedMessage),
}

#[derive(Debug)]
/// Object contained in [`Event::Ready`]
pub struct ReadyData {
    /// Authenticated user info
    pub user: Myself,
}

#[derive(Debug, Deserialize)]
/// Message received from a channel
pub struct ReceivedMessage {
    /// Message ID
    pub id: Snowflake,
    /// ID of the origin channel
    pub channel_id: Snowflake,
    /// Text content of the message
    pub content: String,
    /// Whether this is a TTS message
    pub tts: bool,
    /// Author of the message
    pub author: User,
    timestamp: String,
}

#[derive(Debug, Deserialize)]
/// Data about the current user. ([relevant Discord docs](https://discordapp.com/developers/docs/resources/user#user-object))
pub struct Myself {
    /// User ID
    pub id: Snowflake,
    /// Username, not unique
    pub username: String,
    /// 4-digit Discord tag
    pub discriminator: String,
    /// User's [avatar hash](https://discordapp.com/developers/docs/reference#image-formatting)
    pub avatar: Option<String>,
    /// Whether this is a bot account
    #[serde(default)]
    pub bot: bool,
    /// Whether the account has MFA enabled
    pub mfa_enabled: bool,
    /// Whether the email address on this account has been verified
    pub verified: bool,
    /// Email address
    pub email: Option<String>,
}

#[derive(Debug, Deserialize)]
/// Data about a Discord User. ([relevant Discord docs](https://discordapp.com/developers/docs/resources/user#user-object))
pub struct User {
    /// User ID
    pub id: Snowflake,
    /// Username, not unique
    pub username: String,
    /// 4-digit Discord tag
    pub discriminator: String,
    /// User's [avatar hash](https://discordapp.com/developers/docs/reference#image-formatting)
    pub avatar: Option<String>,
}

impl Into<User> for Myself {
    fn into(self) -> User {
        User {
            id: self.id,
            username: self.username,
            discriminator: self.discriminator,
            avatar: self.avatar,
        }
    }
}