mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
feat(VoiceState): add edit method (#7569)
Co-authored-by: muchnameless <12682826+muchnameless@users.noreply.github.com>
This commit is contained in:
@@ -205,29 +205,64 @@ class VoiceState extends Base {
|
|||||||
return this.guild.members.edit(this.id, { channel }, reason);
|
return this.guild.members.edit(this.id, { channel }, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data to edit the logged in user's own voice state with, when in a stage channel
|
||||||
|
* @typedef {Object} VoiceStateEditData
|
||||||
|
* @property {boolean} [requestToSpeak] Whether or not the client is requesting to become a speaker.
|
||||||
|
* <info>Only available to the logged in user's own voice state.</info>
|
||||||
|
* @property {boolean} [suppressed] Whether or not the user should be suppressed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edits this voice state. Currently only available when in a stage channel
|
||||||
|
* @param {VoiceStateEditData} data The data to edit the voice state with
|
||||||
|
* @returns {Promise<VoiceState>}
|
||||||
|
*/
|
||||||
|
async edit(data) {
|
||||||
|
if (this.channel?.type !== ChannelType.GuildStageVoice) throw new Error('VOICE_NOT_STAGE_CHANNEL');
|
||||||
|
|
||||||
|
const target = this.client.user.id === this.id ? '@me' : this.id;
|
||||||
|
|
||||||
|
if (target !== '@me' && typeof data.requestToSpeak !== 'undefined') {
|
||||||
|
throw new Error('VOICE_STATE_NOT_OWN');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!['boolean', 'undefined'].includes(typeof data.requestToSpeak)) {
|
||||||
|
throw new TypeError('VOICE_STATE_INVALID_TYPE', 'requestToSpeak');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!['boolean', 'undefined'].includes(typeof data.suppressed)) {
|
||||||
|
throw new TypeError('VOICE_STATE_INVALID_TYPE', 'suppressed');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.client.rest.patch(Routes.guildVoiceState(this.guild.id, target), {
|
||||||
|
body: {
|
||||||
|
channel_id: this.channelId,
|
||||||
|
request_to_speak_timestamp: data.requestToSpeak
|
||||||
|
? new Date().toISOString()
|
||||||
|
: data.requestToSpeak === false
|
||||||
|
? null
|
||||||
|
: undefined,
|
||||||
|
suppress: data.suppressed,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Toggles the request to speak in the channel.
|
* Toggles the request to speak in the channel.
|
||||||
* Only applicable for stage channels and for the client's own voice state.
|
* Only applicable for stage channels and for the client's own voice state.
|
||||||
* @param {boolean} [request=true] Whether or not the client is requesting to become a speaker.
|
* @param {boolean} [requestToSpeak=true] Whether or not the client is requesting to become a speaker.
|
||||||
* @example
|
* @example
|
||||||
* // Making the client request to speak in a stage channel (raise its hand)
|
* // Making the client request to speak in a stage channel (raise its hand)
|
||||||
* guild.me.voice.setRequestToSpeak(true);
|
* guild.me.voice.setRequestToSpeak(true);
|
||||||
* @example
|
* @example
|
||||||
* // Making the client cancel a request to speak
|
* // Making the client cancel a request to speak
|
||||||
* guild.me.voice.setRequestToSpeak(false);
|
* guild.me.voice.setRequestToSpeak(false);
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<VoiceState>}
|
||||||
*/
|
*/
|
||||||
async setRequestToSpeak(request = true) {
|
setRequestToSpeak(requestToSpeak = true) {
|
||||||
if (this.channel?.type !== ChannelType.GuildStageVoice) throw new Error('VOICE_NOT_STAGE_CHANNEL');
|
return this.edit({ requestToSpeak });
|
||||||
|
|
||||||
if (this.client.user.id !== this.id) throw new Error('VOICE_STATE_NOT_OWN');
|
|
||||||
|
|
||||||
await this.client.rest.patch(Routes.guildVoiceState(this.guild.id), {
|
|
||||||
body: {
|
|
||||||
channel_id: this.channelId,
|
|
||||||
request_to_speak_timestamp: request ? new Date().toISOString() : null,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -245,21 +280,10 @@ class VoiceState extends Base {
|
|||||||
* @example
|
* @example
|
||||||
* // Moving another user to the audience, or cancelling their invite to speak
|
* // Moving another user to the audience, or cancelling their invite to speak
|
||||||
* voiceState.setSuppressed(true);
|
* voiceState.setSuppressed(true);
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<VoiceState>}
|
||||||
*/
|
*/
|
||||||
async setSuppressed(suppressed = true) {
|
setSuppressed(suppressed = true) {
|
||||||
if (typeof suppressed !== 'boolean') throw new TypeError('VOICE_STATE_INVALID_TYPE', 'suppressed');
|
return this.edit({ suppressed });
|
||||||
|
|
||||||
if (this.channel?.type !== ChannelType.GuildStageVoice) throw new Error('VOICE_NOT_STAGE_CHANNEL');
|
|
||||||
|
|
||||||
const target = this.client.user.id === this.id ? '@me' : this.id;
|
|
||||||
|
|
||||||
await this.client.rest.patch(Routes.guildVoiceState(this.guild.id, target), {
|
|
||||||
body: {
|
|
||||||
channel_id: this.channelId,
|
|
||||||
suppress: suppressed,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
|||||||
10
packages/discord.js/typings/index.d.ts
vendored
10
packages/discord.js/typings/index.d.ts
vendored
@@ -2517,8 +2517,9 @@ export class VoiceState extends Base {
|
|||||||
public setMute(mute?: boolean, reason?: string): Promise<GuildMember>;
|
public setMute(mute?: boolean, reason?: string): Promise<GuildMember>;
|
||||||
public disconnect(reason?: string): Promise<GuildMember>;
|
public disconnect(reason?: string): Promise<GuildMember>;
|
||||||
public setChannel(channel: GuildVoiceChannelResolvable | null, reason?: string): Promise<GuildMember>;
|
public setChannel(channel: GuildVoiceChannelResolvable | null, reason?: string): Promise<GuildMember>;
|
||||||
public setRequestToSpeak(request?: boolean): Promise<void>;
|
public setRequestToSpeak(request?: boolean): Promise<this>;
|
||||||
public setSuppressed(suppressed?: boolean): Promise<void>;
|
public setSuppressed(suppressed?: boolean): Promise<this>;
|
||||||
|
public edit(data: VoiceStateEditData): Promise<this>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Webhook extends WebhookMixin() {
|
export class Webhook extends WebhookMixin() {
|
||||||
@@ -5129,6 +5130,11 @@ export type VoiceBasedChannelTypes = VoiceBasedChannel['type'];
|
|||||||
|
|
||||||
export type VoiceChannelResolvable = Snowflake | VoiceChannel;
|
export type VoiceChannelResolvable = Snowflake | VoiceChannel;
|
||||||
|
|
||||||
|
export interface VoiceStateEditData {
|
||||||
|
requestToSpeak?: boolean;
|
||||||
|
suppressed?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export type WebhookClientData = WebhookClientDataIdWithToken | WebhookClientDataURL;
|
export type WebhookClientData = WebhookClientDataIdWithToken | WebhookClientDataURL;
|
||||||
|
|
||||||
export interface WebhookClientDataIdWithToken {
|
export interface WebhookClientDataIdWithToken {
|
||||||
|
|||||||
Reference in New Issue
Block a user