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 /src/model | |
| 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.
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/guild.rs | 35 |
1 files changed, 35 insertions, 0 deletions
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 |