mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
feat(Voice): implement support for @discordjs/voice (#5402)
This commit is contained in:
@@ -66,30 +66,6 @@ class BaseGuildVoiceChannel extends GuildChannel {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to join this voice-based channel.
|
||||
* @returns {Promise<VoiceConnection>}
|
||||
* @example
|
||||
* // Join a voice-based channel
|
||||
* channel.join()
|
||||
* .then(connection => console.log('Connected!'))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
join() {
|
||||
return this.client.voice.joinChannel(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Leaves this voice-based channel.
|
||||
* @example
|
||||
* // Leave a voice-based channel
|
||||
* channel.leave();
|
||||
*/
|
||||
leave() {
|
||||
const connection = this.client.voice.connections.get(this.guild.id);
|
||||
if (connection?.channel.id === this.id) connection.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the RTC region of the channel.
|
||||
* @param {?string} region The new region of the channel. Set to `null` to remove a specific region for the channel
|
||||
|
||||
@@ -25,6 +25,7 @@ const {
|
||||
VerificationLevels,
|
||||
ExplicitContentFilterLevels,
|
||||
NSFWLevels,
|
||||
Status,
|
||||
} = require('../util/Constants');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const SystemChannelFlags = require('../util/SystemChannelFlags');
|
||||
@@ -1328,6 +1329,35 @@ class Guild extends BaseGuild {
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* The voice state adapter for this guild that can be used with @discordjs/voice to play audio in voice
|
||||
* and stage channels.
|
||||
* @type {Function}
|
||||
* @readonly
|
||||
* @example
|
||||
* const { joinVoiceChannel } = require('@discordjs/voice');
|
||||
* const voiceConnection = joinVoiceChannel({
|
||||
* channelId: channel.id,
|
||||
* guildId: channel.guild.id,
|
||||
* adapterCreator: channel.guild.voiceAdapterCreator,
|
||||
* });
|
||||
*/
|
||||
get voiceAdapterCreator() {
|
||||
return methods => {
|
||||
this.client.voice.adapters.set(this.id, methods);
|
||||
return {
|
||||
sendPayload: data => {
|
||||
if (this.shard.status !== Status.READY) return false;
|
||||
this.shard.send(data);
|
||||
return true;
|
||||
},
|
||||
destroy: () => {
|
||||
this.client.voice.adapters.delete(this.id);
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a collection of this guild's roles, sorted by their position and IDs.
|
||||
* @returns {Collection<Snowflake, Role>}
|
||||
|
||||
@@ -100,16 +100,6 @@ class VoiceState extends Base {
|
||||
return this.guild.channels.cache.get(this.channelID) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* If this is a voice state of the client user, then this will refer to the active VoiceConnection for this guild
|
||||
* @type {?VoiceConnection}
|
||||
* @readonly
|
||||
*/
|
||||
get connection() {
|
||||
if (this.id !== this.client.user.id) return null;
|
||||
return this.client.voice.connections.get(this.guild.id) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this member is either self-deafened or server-deafened
|
||||
* @type {?boolean}
|
||||
@@ -128,16 +118,6 @@ class VoiceState extends Base {
|
||||
return this.serverMute || this.selfMute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this member is currently speaking. A boolean if the information is available (aka
|
||||
* the bot is connected to any voice channel or stage channel in the guild), otherwise this is `null`
|
||||
* @type {?boolean}
|
||||
* @readonly
|
||||
*/
|
||||
get speaking() {
|
||||
return this.channel && this.channel.connection ? Boolean(this.channel.connection._speaking.get(this.id)) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mutes/unmutes the member of this voice state.
|
||||
* @param {boolean} mute Whether or not the member should be muted
|
||||
@@ -180,34 +160,6 @@ class VoiceState extends Base {
|
||||
: 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
|
||||
* @returns {Promise<boolean>} true if the voice state was successfully updated, otherwise false
|
||||
*/
|
||||
async setSelfMute(mute) {
|
||||
if (this.id !== this.client.user.id) throw new Error('VOICE_STATE_NOT_OWN');
|
||||
if (typeof mute !== 'boolean') throw new TypeError('VOICE_STATE_INVALID_TYPE', 'mute');
|
||||
if (!this.connection) return false;
|
||||
this.selfMute = mute;
|
||||
await this.connection.sendVoiceStateUpdate();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Self-deafens/undeafens the bot for this voice state.
|
||||
* @param {boolean} deaf Whether or not the bot should be self-deafened
|
||||
* @returns {Promise<boolean>} true if the voice state was successfully updated, otherwise false
|
||||
*/
|
||||
async setSelfDeaf(deaf) {
|
||||
if (this.id !== this.client.user.id) return new Error('VOICE_STATE_NOT_OWN');
|
||||
if (typeof deaf !== 'boolean') return new TypeError('VOICE_STATE_INVALID_TYPE', 'deaf');
|
||||
if (!this.connection) return false;
|
||||
this.selfDeaf = deaf;
|
||||
await this.connection.sendVoiceStateUpdate();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles the request to speak in the channel.
|
||||
* Only applicable for stage channels and for the client's own voice state.
|
||||
|
||||
Reference in New Issue
Block a user