diff options
| author | Austin Hellyer <[email protected]> | 2016-11-14 22:01:23 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-14 22:01:23 -0800 |
| commit | 0102f4d5338741797e7d3745b516e9bb781f8e38 (patch) | |
| tree | 763089e5650463256b12d1892f884881a7f27ac0 | |
| parent | Add missing permission shorthands (diff) | |
| download | serenity-0102f4d5338741797e7d3745b516e9bb781f8e38.tar.xz serenity-0102f4d5338741797e7d3745b516e9bb781f8e38.zip | |
Allow current user to nickname themselves
Add support for the `PATCH /guilds/:guild_id/members/@me/nick` endpoint,
which allows the current user to edit their own nickname.
A user can only nickname themselves if they have the `Change Nickname`
permission.
This adds 4 methods:
- `serenity::client::http::edit_nickname`;
- `serenity::client::Context::edit_nickname`;
- `serenity::model::Guild::edit_nickname`;
- `serenity::model::LiveGuild::edit_nickname`.
`LiveGuild`'s implementation checks for whether the current user has
permission to change their own nickname.
| -rw-r--r-- | src/client/context.rs | 14 | ||||
| -rw-r--r-- | src/client/http/mod.rs | 19 | ||||
| -rw-r--r-- | src/client/http/ratelimiting.rs | 1 | ||||
| -rw-r--r-- | src/model/guild.rs | 35 |
4 files changed, 69 insertions, 0 deletions
diff --git a/src/client/context.rs b/src/client/context.rs index 1bee390..f4ddd5e 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -514,6 +514,20 @@ impl Context { http::edit_member(guild_id.into().0, user_id.into().0, map) } + /// Edits the current user's nickname for the provided [`Guild`] via its Id. + /// + /// Pass `None` to reset the nickname. + /// + /// **Note**: Requires the [Change Nickname] permission. + /// + /// [`Guild`]: ../../model/struct.Guild.html + /// [Change Nickname]: permissions/constant.CHANGE_NICKNAME.html + #[inline] + pub fn edit_nickname<G>(&self, guild_id: G, new_nickname: Option<&str>) + -> Result<()> where G: Into<GuildId> { + http::edit_nickname(guild_id.into().0, new_nickname) + } + pub fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(&mut self, f: F) -> Result<CurrentUser> { let user = try!(http::get_current_user()); diff --git a/src/client/http/mod.rs b/src/client/http/mod.rs index 9fba750..ca21c44 100644 --- a/src/client/http/mod.rs +++ b/src/client/http/mod.rs @@ -31,6 +31,7 @@ use hyper::status::StatusCode; use hyper::{Error as HyperError, Result as HyperResult, Url, header}; use multipart::client::Multipart; use self::ratelimiting::Route; +use serde_json::builder::ObjectBuilder; use serde_json; use std::default::Default; use std::io::{ErrorKind as IoErrorKind, Read}; @@ -451,6 +452,23 @@ pub fn edit_message(channel_id: u64, Message::decode(try!(serde_json::from_reader(response))) } +/// Edits the current user's nickname for the provided [`Guild`] via its Id. +/// +/// Pass `None` to reset the nickname. +/// +/// [`Guild`]: ../../model/struct.Guild.html +pub fn edit_nickname(guild_id: u64, new_nickname: Option<&str>) + -> Result<()> { + let map = ObjectBuilder::new().insert("nick", new_nickname).build(); + let body = try!(serde_json::to_string(&map)); + let response = request!(Route::GuildsIdMembersMeNick(guild_id), + patch(body), + "/guilds/{}/members/@me/nick", + guild_id); + + verify(200, response) +} + pub fn edit_note(user_id: u64, map: Value) -> Result<()> { let body = try!(serde_json::to_string(&map)); @@ -1130,6 +1148,7 @@ fn verify(expected_status_code: u16, mut response: HyperResponse) -> Result<()> { let expected_status = match expected_status_code { + 200 => StatusCode::Ok, 204 => StatusCode::NoContent, 401 => StatusCode::Unauthorized, _ => { diff --git a/src/client/http/ratelimiting.rs b/src/client/http/ratelimiting.rs index 6a5ef17..f1a1713 100644 --- a/src/client/http/ratelimiting.rs +++ b/src/client/http/ratelimiting.rs @@ -84,6 +84,7 @@ pub enum Route { GuildsIdIntegrationsIdSync(u64), GuildsIdInvites(u64), GuildsIdMembersId(u64), + GuildsIdMembersMeNick(u64), GuildsIdPrune(u64), GuildsIdRegions(u64), GuildsIdRoles(u64), diff --git a/src/model/guild.rs b/src/model/guild.rs index 0506115..866cd19 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -120,6 +120,18 @@ impl Guild { self.roles.get(&role_id.into()) } + /// Edits the current user's nickname for the guild. + /// + /// Pass `None` to reset the nickname. + /// + /// **Note**: Requires the [Change Nickname] permission. + /// + /// [Change Nickname]: permissions/constant.CHANGE_NICKNAME.html + #[inline] + pub fn edit_nickname(&self, new_nickname: Option<&str>) -> Result<()> { + http::edit_nickname(self.id.0, new_nickname) + } + /// Returns a formatted URL of the guild's icon, if the guild has an icon. pub fn icon_url(&self) -> Option<String> { self.icon.as_ref().map(|icon| @@ -396,6 +408,29 @@ impl LiveGuild { } } + /// Edits the current user's nickname for the guild. + /// + /// Pass `None` to reset the nickname. + /// + /// **Note**: Requires the [Change Nickname] permission. + /// + /// # Errors + /// + /// Returns a [`ClientError::InvalidPermissions`] if the current user does + /// not have permission to change their own nickname. + /// + /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions + /// [Change Nickname]: permissions/constant.CHANGE_NICKNAME.html + pub fn edit_nickname(&self, new_nickname: Option<&str>) -> Result<()> { + let req = permissions::CHANGE_NICKNAME; + + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + + http::edit_nickname(self.id.0, new_nickname) + } + /// Attempts to retrieve a [`PublicChannel`] with the given Id. /// /// [`PublicChannel`]: struct.PublicChannel.html |