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
use chrono::{DateTime, FixedOffset};

use crate::{
    Snowflake,
    user::User,
};

/// A User that is part of a guild.
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct GuildMember {
    /// The guild ID of this guild member. (Guild Member Add)
    #[serde(default)]
    pub guild_id: Option<Snowflake>,
    /// The Discord user of this guild member.
    pub user: Option<User>,
    /// The member's nickname, if applicable.
    #[serde(default, rename = "nick")]
    pub nickname: Option<String>,
    /// The date that they joined the server.
    pub joined_at: Option<DateTime<FixedOffset>>,
    /// Whether or not the member is muted.
    #[serde(default)]
    pub mute: bool,
    /// Whether or not the member has been deafened.
    #[serde(default)]
    pub deaf: bool,
    /// A collection of roles that this member has.
    pub roles: Vec<String>,
}

/// Options for adding a member to a guild.
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct AddMemberOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    access_token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    nick: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    roles: Option<Vec<Snowflake>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    mute: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    deaf: Option<bool>,
}

impl AddMemberOptions {
    /// Sets the access token of the user which you want to add.
    pub fn access_token(mut self, token: String) -> Self {
        self.access_token = Some(token);
        self
    }

    /// Sets the nickname for the newly created user.
    pub fn nickname(mut self, name: &str) -> Self {
        self.nick = Some(name.to_string());
        self
    }

    /// Sets the roles that the user should have upon joining the guild.
    pub fn roles(mut self, rls: Vec<Snowflake>) -> Self {
        self.roles = Some(rls);
        self
    }

    /// Sets the user's muted status when joining the guild.
    pub fn muted(mut self, mute: bool) -> Self {
        self.mute = Some(mute);
        self
    }

    /// Sets the deaf status of the user when joining the guild.
    pub fn deaf(mut self, opt: bool) -> Self {
        self.deaf = Some(opt);
        self
    }
}

/// Options for modifying a guild member.
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct ModifyMemberOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    nick: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    roles: Option<Vec<Snowflake>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    mute: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    deaf: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    channel_id: Option<Snowflake>,
}

impl ModifyMemberOptions {
    /// Sets the new nickname of this user.
    pub fn nick(mut self, name: &str) -> Self {
        self.nick = Some(name.to_string());
        self
    }

    /// Sets the roles that should be assigned to the user.
    pub fn roles(mut self, rls: Vec<Snowflake>) -> Self {
        self.roles = Some(rls);
        self
    }

    /// Sets the muted status of this guild member.
    pub fn muted(mut self, opt: bool) -> Self {
        self.mute = Some(opt);
        self
    }

    /// Sets the deafened status of this guild member.
    pub fn deaf(mut self, opt: bool) -> Self {
        self.deaf = Some(opt);
        self
    }

    /// Sets the voice channel that this member should be moved to.
    pub fn channel_id(mut self, id: Snowflake) -> Self {
        self.channel_id = Some(id);
        self
    }
}

/// Options for requesting a list of guild members from the API.
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct ListMembersOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    limit: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    after: Option<Snowflake>,
}

impl ListMembersOptions {
    /// Sets the maximum amount of members for this request.
    pub fn limit(mut self, num: i32) -> Self {
        self.limit = Some(num);
        self
    }

    /// Sets the the highest user id in the previous page.
    pub fn after(mut self, id: Snowflake) -> Self {
        self.after = Some(id);
        self
    }
}

/// A payload sent by the gateway when a guild member is removed from a guild.
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct GuildMemberRemove {
    /// The guild ID of the guild that this member belongs to.
    pub guild_id: Snowflake,
    /// The Discord User of this guild member.
    pub user: User,
}

/// A payload sent by the gateway upon a RequestGuildMembers packet.
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct GuildMembersChunk {
    /// The guild ID of the guild that the members belong to.
    pub guild_id: Snowflake,
    /// The array of guild members.
    pub members: Vec<GuildMember>
}