aboutsummaryrefslogtreecommitdiff
path: root/src/builder/edit_profile.rs
blob: df428b43d697ee7f879b6966ee2c0d08c4287249 (plain) (blame)
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
use internal::prelude::*;
use utils::VecMap;

/// A builder to edit the current user's settings, to be used in conjunction
/// with [`CurrentUser::edit`].
///
/// [`CurrentUser::edit`]: ../model/user/struct.CurrentUser.html#method.edit
#[derive(Clone, Debug, Default)]
pub struct EditProfile(pub VecMap<&'static str, Value>);

impl EditProfile {
    /// Sets the avatar of the current user. `None` can be passed to remove an
    /// avatar.
    ///
    /// A base64-encoded string is accepted as the avatar content.
    ///
    /// # Examples
    ///
    /// A utility method - [`utils::read_image`] - is provided to read an
    /// image from a file and return its contents in base64-encoded form:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # use serenity::model::prelude::*;
    /// #
    /// # struct Handler;
    ///
    /// # impl EventHandler for Handler {
    ///    # fn message(&self, context: Context, _: Message) {
    ///         use serenity::utils;
    ///
    ///         // assuming a `context` has been bound
    ///
    ///         let base64 = utils::read_image("./my_image.jpg")
    ///         .expect("Failed to read image");
    ///
    ///         let _ = context.edit_profile(|profile| {
    ///             profile.avatar(Some(&base64))
    ///         });
    ///    # }
    /// }
    /// #
    /// # let mut client = Client::new("token", Handler).unwrap();
    /// #
    /// # client.start().unwrap();
    /// ```
    ///
    /// [`utils::read_image`]: ../fn.read_image.html
    pub fn avatar(mut self, avatar: Option<&str>) -> Self {
        let avatar = avatar.map_or(Value::Null, |x| Value::String(x.to_string()));
        self.0.insert("avatar", avatar);

        self
    }

    /// Modifies the current user's email address.
    ///
    /// Note that when modifying the email address, the current password must
    /// also be [provided].
    ///
    /// No validation is performed on this by the library.
    ///
    /// **Note**: This can only be used by user accounts.
    ///
    /// [provided]: #method.password
    pub fn email(mut self, email: &str) -> Self {
        self.0.insert("email", Value::String(email.to_string()));

        self
    }

    /// Modifies the current user's password.
    ///
    /// Note that when modifying the password, the current password must also be
    /// [provided].
    ///
    /// [provided]: #method.password
    pub fn new_password(mut self, new_password: &str) -> Self {
        self.0.insert("new_password", Value::String(new_password.to_string()));

        self
    }

    /// Used for providing the current password as verification when
    /// [modifying the password] or [modifying the associated email address].
    ///
    /// [modifying the password]: #method.new_password
    /// [modifying the associated email address]: #method.email
    pub fn password(mut self, password: &str) -> Self {
        self.0.insert("password", Value::String(password.to_string()));

        self
    }

    /// Modifies the current user's username.
    ///
    /// When modifying the username, if another user has the same _new_ username
    /// and current discriminator, a new unique discriminator will be assigned.
    /// If there are no available discriminators with the requested username,
    /// an error will occur.
    pub fn username(mut self, username: &str) -> Self {
        self.0.insert("username", Value::String(username.to_string()));

        self
    }
}