Skip to content

Commit

Permalink
Handle 200 Response when modifying Members on API v8 (#1122)
Browse files Browse the repository at this point in the history
* fix: Expect 200 for modify member

* feat: Use returned member object on modify member

* fix: Insert guild_id into modify member response
  • Loading branch information
drklee3 committed Dec 16, 2020
1 parent 0ca5813 commit 8471f5b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
12 changes: 9 additions & 3 deletions src/http/client.rs
Expand Up @@ -609,14 +609,20 @@ impl Http {
}

/// Does specific actions to a member.
pub async fn edit_member(&self, guild_id: u64, user_id: u64, map: &JsonMap) -> Result<()> {
pub async fn edit_member(&self, guild_id: u64, user_id: u64, map: &JsonMap) -> Result<Member> {
let body = serde_json::to_vec(map)?;

self.wind(204, Request {
let mut value = self.request(Request {
body: Some(&body),
headers: None,
route: RouteInfo::EditMember { guild_id, user_id },
}).await
}).await?.json::<Value>().await?;

if let Some(map) = value.as_object_mut() {
map.insert("guild_id".to_string(), Value::Number(Number::from(guild_id)));
}

serde_json::from_value::<Member>(value).map_err(From::from)
}

/// Edits a message by Id.
Expand Down
6 changes: 3 additions & 3 deletions src/model/guild/guild_id.rs
Expand Up @@ -317,7 +317,7 @@ impl GuildId {
/// guild.edit_member(&context, user_id, |m| m.mute(true).roles(&vec![role_id]));
/// ```
#[inline]
pub async fn edit_member<F>(self, http: impl AsRef<Http>, user_id: impl Into<UserId>, f: F) -> Result<()>
pub async fn edit_member<F>(self, http: impl AsRef<Http>, user_id: impl Into<UserId>, f: F) -> Result<Member>
where F: FnOnce(&mut EditMember) -> &mut EditMember {
let mut edit_member = EditMember::default();
f(&mut edit_member);
Expand Down Expand Up @@ -527,7 +527,7 @@ impl GuildId {
http: impl AsRef<Http>,
user_id: impl Into<UserId>,
channel_id: impl Into<ChannelId>
) -> Result<()> {
) -> Result<Member> {
let mut map = Map::new();
map.insert(
"channel_id".to_string(),
Expand All @@ -550,7 +550,7 @@ impl GuildId {
///
/// [Move Members]: Permissions::MOVE_MEMBERS
#[inline]
pub async fn disconnect_member(self, http: impl AsRef<Http>, user_id: impl Into<UserId>) -> Result<()> {
pub async fn disconnect_member(self, http: impl AsRef<Http>, user_id: impl Into<UserId>) -> Result<Member> {
let mut map = Map::new();
map.insert(
"channel_id".to_string(),
Expand Down
17 changes: 9 additions & 8 deletions src/model/guild/member.rs
Expand Up @@ -78,15 +78,15 @@ impl Member {
/// **Note**: Requires the [Manage Roles] permission.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
pub async fn add_roles(&mut self, http: impl AsRef<Http>, role_ids: &[RoleId]) -> Result<()> {
pub async fn add_roles(&mut self, http: impl AsRef<Http>, role_ids: &[RoleId]) -> Result<Vec<RoleId>> {
self.roles.extend_from_slice(role_ids);

let mut builder = EditMember::default();
builder.roles(&self.roles);
let map = utils::hashmap_to_json_map(builder.0);

match http.as_ref().edit_member(self.guild_id.0, self.user.id.0, &map).await {
Ok(()) => Ok(()),
Ok(member) => Ok(member.roles),
Err(why) => {
self.roles.retain(|r| !role_ids.contains(r));

Expand Down Expand Up @@ -182,7 +182,7 @@ impl Member {
/// methods, as well as usage of this.
///
/// [`EditMember`]: crate::builder::EditMember
pub async fn edit<F>(&self, http: impl AsRef<Http>, f: F) -> Result<()>
pub async fn edit<F>(&self, http: impl AsRef<Http>, f: F) -> Result<Member>
where F: FnOnce(&mut EditMember) -> &mut EditMember
{
let mut edit_member = EditMember::default();
Expand Down Expand Up @@ -319,7 +319,7 @@ impl Member {
&self,
http: impl AsRef<Http>,
channel: impl Into<ChannelId>
) -> Result<()> {
) -> Result<Member> {
self.guild_id.move_member(http, self.user.id, channel).await
}

Expand All @@ -328,7 +328,7 @@ impl Member {
/// Requires the [Move Members] permission.
///
/// [Move Members]: Permissions::MOVE_MEMBERS
pub async fn disconnect_from_voice(&self, http: impl AsRef<Http>) -> Result<()> {
pub async fn disconnect_from_voice(&self, http: impl AsRef<Http>) -> Result<Member> {
self.guild_id.disconnect_member(http, self.user.id).await
}

Expand Down Expand Up @@ -383,20 +383,21 @@ impl Member {
}
}

/// Removes one or multiple [`Role`]s from the member.
/// Removes one or multiple [`Role`]s from the member. Returns the member's
/// new roles.
///
/// **Note**: Requires the [Manage Roles] permission.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
pub async fn remove_roles(&mut self, http: impl AsRef<Http>, role_ids: &[RoleId]) -> Result<()> {
pub async fn remove_roles(&mut self, http: impl AsRef<Http>, role_ids: &[RoleId]) -> Result<Vec<RoleId>> {
self.roles.retain(|r| !role_ids.contains(r));

let mut builder = EditMember::default();
builder.roles(&self.roles);
let map = utils::hashmap_to_json_map(builder.0);

match http.as_ref().edit_member(self.guild_id.0, self.user.id.0, &map).await {
Ok(()) => Ok(()),
Ok(member) => Ok(member.roles),
Err(why) => {
self.roles.extend_from_slice(role_ids);

Expand Down
6 changes: 3 additions & 3 deletions src/model/guild/mod.rs
Expand Up @@ -622,7 +622,7 @@ impl Guild {
}

/// Edits the properties of member of the guild, such as muting or
/// nicknaming them.
/// nicknaming them. Returns the new member.
///
/// Refer to `EditMember`'s documentation for a full list of methods and
/// permission restrictions.
Expand All @@ -635,7 +635,7 @@ impl Guild {
/// guild.edit_member(user_id, |m| m.mute(true).roles(&vec![role_id]));
/// ```
#[inline]
pub async fn edit_member<F>(&self, http: impl AsRef<Http>, user_id: impl Into<UserId>, f: F) -> Result<()>
pub async fn edit_member<F>(&self, http: impl AsRef<Http>, user_id: impl Into<UserId>, f: F) -> Result<Member>
where F: FnOnce(&mut EditMember) -> &mut EditMember
{
self.id.edit_member(&http, user_id, f).await
Expand Down Expand Up @@ -1267,7 +1267,7 @@ impl Guild {
///
/// [Move Members]: Permissions::MOVE_MEMBERS
#[inline]
pub async fn move_member(&self, http: impl AsRef<Http>, user_id: impl Into<UserId>, channel_id: impl Into<ChannelId>) -> Result<()> {
pub async fn move_member(&self, http: impl AsRef<Http>, user_id: impl Into<UserId>, channel_id: impl Into<ChannelId>) -> Result<Member> {
self.id.move_member(&http, user_id, channel_id).await
}

Expand Down
4 changes: 2 additions & 2 deletions src/model/guild/partial_guild.rs
Expand Up @@ -286,7 +286,7 @@ impl PartialGuild {
/// GuildId(7).edit_member(user_id, |m| m.mute(true).roles(&vec![role_id])).await;
/// ```
#[inline]
pub async fn edit_member<F>(&self, http: impl AsRef<Http>, user_id: impl Into<UserId>, f: F) -> Result<()>
pub async fn edit_member<F>(&self, http: impl AsRef<Http>, user_id: impl Into<UserId>, f: F) -> Result<Member>
where F: FnOnce(&mut EditMember) -> &mut EditMember
{
self.id.edit_member(&http, user_id, f).await
Expand Down Expand Up @@ -413,7 +413,7 @@ impl PartialGuild {
http: impl AsRef<Http>,
user_id: impl Into<UserId>,
channel_id: impl Into<ChannelId>
) -> Result<()> {
) -> Result<Member> {
self.id.move_member(&http, user_id, channel_id).await
}

Expand Down

0 comments on commit 8471f5b

Please sign in to comment.