Add ability to kick members from VoiceChannels and remove duplicated methods (#3242)

* feat(voice): kick members from voice channels

* fix(VoiceState): improve stability in checking for client user

* feat(VoiceState): add setChannel for moving/kicking members

* update typings

* remove duplicated methods across GuildMember and VoiceState

member.setDeaf => member.voice.setDeaf
member.setMute => member.voice.setMute
member.setVoiceChannel => member.voice.setChannel
This commit is contained in:
Amish Shah
2019-05-04 16:46:42 +01:00
committed by GitHub
parent d7f8fd1ae0
commit e64773e21b
3 changed files with 21 additions and 36 deletions

View File

@@ -254,7 +254,8 @@ class GuildMember extends Base {
* @property {Collection<Snowflake, Role>|RoleResolvable[]} [roles] The roles or role IDs to apply
* @property {boolean} [mute] Whether or not the member should be muted
* @property {boolean} [deaf] Whether or not the member should be deafened
* @property {ChannelResolvable} [channel] Channel to move member to (if they are connected to voice)
* @property {ChannelResolvable|null} [channel] Channel to move member to (if they are connected to voice), or `null`
* if you want to kick them from voice
*/
/**
@@ -270,8 +271,10 @@ class GuildMember extends Base {
throw new Error('GUILD_VOICE_CHANNEL_RESOLVE');
}
data.channel_id = data.channel.id;
data.channel = null;
} else if (data.channel === null) {
data.channel_id = null;
}
data.channel = undefined;
if (data.roles) data.roles = data.roles.map(role => role instanceof Role ? role.id : role);
let endpoint = this.client.api.guilds(this.guild.id);
if (this.user.id === this.client.user.id) {
@@ -289,35 +292,6 @@ class GuildMember extends Base {
return clone;
}
/**
* Mutes/unmutes this member.
* @param {boolean} mute Whether or not the member should be muted
* @param {string} [reason] Reason for muting or unmuting
* @returns {Promise<GuildMember>}
*/
setMute(mute, reason) {
return this.edit({ mute }, reason);
}
/**
* Deafens/undeafens this member.
* @param {boolean} deaf Whether or not the member should be deafened
* @param {string} [reason] Reason for deafening or undeafening
* @returns {Promise<GuildMember>}
*/
setDeaf(deaf, reason) {
return this.edit({ deaf }, reason);
}
/**
* Moves this member to the given channel.
* @param {ChannelResolvable} channel The channel to move the member to
* @returns {Promise<GuildMember>}
*/
setVoiceChannel(channel) {
return this.edit({ channel });
}
/**
* Sets the nickname for this member.
* @param {string} nick The nickname for the guild member

View File

@@ -85,7 +85,7 @@ class VoiceState extends Base {
* @readonly
*/
get connection() {
if (browser || this.id !== this.guild.me.id) return null;
if (browser || this.id !== this.client.user.id) return null;
return this.client.voice.connections.get(this.guild.id) || null;
}
@@ -139,6 +139,19 @@ class VoiceState extends Base {
return this.member ? this.member.edit({ deaf }, reason) : Promise.reject(new Error('VOICE_STATE_UNCACHED_MEMBER'));
}
/**
* Moves the member to a different channel, or kick them from the one they're in.
* @param {ChannelResolvable|null} [channel] Channel to move the member to, or `null` if you want to kick them from
* voice
* @param {string} [reason] Reason for moving member to another channel or kicking
* @returns {Promise<GuildMember>}
*/
setChannel(channel, reason) {
return this.member ?
this.member.edit({ channel }, reason) :
Promise.reject(new Error('VOICE_STATE_UNCACHED_MEMBER'));
}
/**
* Self-mutes/unmutes the bot for this voice state.
* @param {boolean} mute Whether or not the bot should be self-muted

6
typings/index.d.ts vendored
View File

@@ -591,10 +591,7 @@ declare module 'discord.js' {
public hasPermission(permission: PermissionResolvable, options?: { checkAdmin?: boolean; checkOwner?: boolean }): boolean;
public kick(reason?: string): Promise<GuildMember>;
public permissionsIn(channel: ChannelResolvable): Readonly<Permissions>;
public setDeaf(deaf: boolean, reason?: string): Promise<GuildMember>;
public setMute(mute: boolean, reason?: string): Promise<GuildMember>;
public setNickname(nickname: string, reason?: string): Promise<GuildMember>;
public setVoiceChannel(voiceChannel: ChannelResolvable): Promise<GuildMember>;
public toJSON(): object;
public toString(): string;
}
@@ -1287,6 +1284,7 @@ declare module 'discord.js' {
public setDeaf(deaf: boolean, reason?: string): Promise<GuildMember>;
public setMute(mute: boolean, reason?: string): Promise<GuildMember>;
public setChannel(channel: ChannelResolvable | null, reason?: string): Promise<GuildMember>;
public setSelfDeaf(deaf: boolean): Promise<boolean>;
public setSelfMute(mute: boolean): Promise<boolean>;
}
@@ -1927,7 +1925,7 @@ declare module 'discord.js' {
roles?: Collection<Snowflake, Role> | RoleResolvable[];
mute?: boolean;
deaf?: boolean;
channel?: ChannelResolvable;
channel?: ChannelResolvable | null;
}
type GuildMemberResolvable = GuildMember | UserResolvable;