aboutsummaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-12-03 08:10:09 -0800
committerAustin Hellyer <[email protected]>2016-12-03 08:10:09 -0800
commit16d1b3cad3982accd805f64ef93e51d921b3da55 (patch)
treed5910849d3fb62caf1b743405bcee5ffbaefa6cd /src/model
parentFix rest::get_guilds doctest (diff)
downloadserenity-16d1b3cad3982accd805f64ef93e51d921b3da55.tar.xz
serenity-16d1b3cad3982accd805f64ef93e51d921b3da55.zip
Add CurrentUser::edit
Diffstat (limited to 'src/model')
-rw-r--r--src/model/user.rs47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/model/user.rs b/src/model/user.rs
index 88a6f9d..0cb0c3a 100644
--- a/src/model/user.rs
+++ b/src/model/user.rs
@@ -1,4 +1,4 @@
-use std::fmt;
+use std::{fmt, mem};
use super::utils::{into_map, into_string, remove};
use super::{
CurrentUser,
@@ -13,6 +13,7 @@ use super::{
};
use ::client::rest::GuildPagination;
use ::internal::prelude::*;
+use ::utils::builder::EditProfile;
use ::utils::decode_array;
#[cfg(feature = "methods")]
@@ -34,6 +35,50 @@ impl CurrentUser {
format!(cdn!("/avatars/{}/{}.jpg"), self.id, av))
}
+ /// Edits the current user's profile settings.
+ ///
+ /// This mutates the current user in-place.
+ ///
+ /// Refer to `EditProfile`'s documentation for its methods.
+ ///
+ /// # Examples
+ ///
+ /// Change the avatar:
+ ///
+ /// ```rust,no_run
+ /// use serenity::client::CACHE;
+ ///
+ /// let avatar = serenity::utils::read_image("./avatar.png").unwrap();
+ ///
+ /// CACHE.write()
+ /// .unwrap()
+ /// .user
+ /// .edit(|p| p
+ /// .avatar(Some(&avatar)));
+ /// ```
+ #[cfg(feature = "methods")]
+ pub fn edit<F>(&mut self, f: F) -> Result<()>
+ where F: FnOnce(EditProfile) -> EditProfile {
+ let mut map = ObjectBuilder::new()
+ .insert("avatar", Some(&self.avatar))
+ .insert("username", &self.name);
+
+ if let Some(email) = self.email.as_ref() {
+ map = map.insert("email", email)
+ }
+
+ let edited = f(EditProfile(map)).0.build();
+
+ match rest::edit_profile(edited) {
+ Ok(new) => {
+ mem::replace(self, new);
+
+ Ok(())
+ },
+ Err(why) => Err(why),
+ }
+ }
+
/// Gets a list of guilds that the current user is in.
pub fn guilds(&self) -> Result<Vec<GuildInfo>> {
rest::get_guilds(GuildPagination::After(GuildId(0)), 100)