Skip to content

Commit

Permalink
Add methods to disconnect a member from a voice channel (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
mendess authored May 22, 2020
1 parent 9f75c09 commit 4e8e40b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/builder/edit_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,15 @@ impl EditMember {
let num = Value::Number(Number::from(channel_id.0));
self.0.insert("channel_id", num);
}

/// Disconnects the user from their voice channel if any
///
/// Requires the [Move Members] permission.
///
/// [Move Members]: ../model/permissions/struct.Permissions.html#associatedconstant.MOVE_MEMBERS
pub fn disconnect_member(&mut self) -> &mut Self {
self.0.insert("channel_id", Value::Null);

self
}
}
24 changes: 24 additions & 0 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,30 @@ impl GuildId {
Some(guild.name.to_string())
}

/// Disconnects a member from a voice channel in the guild.
///
/// Requires the [Move Members] permission.
///
/// [Move Members]: ../permissions/struct.Permissions.html#associatedconstant.MOVE_MEMBERS
#[inline]
pub fn disconnect_member<U>(self, http: impl AsRef<Http>, user_id: U) -> Result<()>
where U: Into<UserId> {
self._disconnect_member(&http, user_id.into())
}

fn _disconnect_member(
self,
http: impl AsRef<Http>,
user_id: UserId,
) -> Result<()> {
let mut map = Map::new();
map.insert(
"channel_id".to_string(),
Value::Null
);
http.as_ref().edit_member(self.0, user_id.0, &map)
}

/// Gets the number of [`Member`]s that would be pruned with the given
/// number of days.
///
Expand Down
29 changes: 29 additions & 0 deletions src/model/guild/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,35 @@ impl Member {
self.guild_id.kick_with_reason(cache_http.http(), self.user.read().id, reason)
}

/// Moves the member to a voice channel.
///
/// Requires the [Move Members] permission.
///
/// [Move Members]: ../permissions/struct.Permissions.html#associatedconstant.MOVE_MEMBERS
pub fn move_to_voice_channel<C, H>(&self, http: H, c: C) -> Result<()>
where
C: Into<ChannelId>,
H: AsRef<Http>,
{
self._move_to_voice_channel(http, c.into())
}

fn _move_to_voice_channel<H>(&self, http: H, c: ChannelId) -> Result<()>
where
H: AsRef<Http>
{
self.guild_id.move_member(http, self.user.read().id, c)
}

/// Disconnects the member from their voice channel if any.
///
/// Requires the [Move Members] permission.
///
/// [Move Members]: ../permissions/struct.Permissions.html#associatedconstant.MOVE_MEMBERS
pub fn disconnect_from_voice(&self, http: impl AsRef<Http>) -> Result<()> {
self.guild_id.disconnect_member(http, self.user.read().id)
}


/// Returns the guild-level permissions for the member.
///
Expand Down

0 comments on commit 4e8e40b

Please sign in to comment.