From c1931161005c9f204edc8e600cb2eeadb503f5c3 Mon Sep 17 00:00:00 2001 From: Elysia <71698422+aiko-chan-ai@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:50:46 +0700 Subject: [PATCH] fix: voice connection --- src/client/voice/ClientVoiceManager.js | 28 ++++++++++++++------------ src/client/voice/VoiceConnection.js | 4 ++-- src/client/voice/util/Secretbox.js | 12 ++++++++++- typings/index.d.ts | 4 ++-- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/client/voice/ClientVoiceManager.js b/src/client/voice/ClientVoiceManager.js index ab1cc7e..51664a4 100644 --- a/src/client/voice/ClientVoiceManager.js +++ b/src/client/voice/ClientVoiceManager.js @@ -1,6 +1,5 @@ 'use strict'; -const { Collection } = require('@discordjs/collection'); const VoiceConnection = require('./VoiceConnection'); const { Error } = require('../../errors'); const { Events } = require('../../util/Constants'); @@ -20,10 +19,10 @@ class ClientVoiceManager { Object.defineProperty(this, 'client', { value: client }); /** - * A collection mapping connection IDs to the Connection objects - * @type {Collection} + * A current connection objects + * @type {?VoiceConnection} */ - this.connections = new Collection(); + this.connection = null; /** * Maps guild ids to voice adapters created for use with @discordjs/voice. @@ -49,7 +48,7 @@ class ClientVoiceManager { channel_id || guild_id } token: ${token} endpoint: ${endpoint}`, ); - const connection = this.connections.get(guild_id || channel_id); // DMs Call + const connection = this.connection; if (connection) connection.setTokenAndEndpoint(token, endpoint); // Djs / voice if (payload.guild_id) { @@ -61,12 +60,12 @@ class ClientVoiceManager { onVoiceStateUpdate(payload) { const { guild_id, session_id, channel_id } = payload; - const connection = this.connections.get(guild_id || channel_id); // DMs Call + const connection = this.connection; this.client.emit('debug', `[VOICE] connection? ${!!connection}, ${guild_id} ${session_id} ${channel_id}`); if (!connection) return; if (!channel_id) { connection._disconnect(); - this.connections.delete(guild_id || channel_id); + this.connection = null; return; } const channel = this.client.channels.cache.get(channel_id); @@ -95,21 +94,22 @@ class ClientVoiceManager { /** * Sets up a request to join a voice channel. - * @param {VoiceChannel | StageChannel | DMChannel | GroupDMChannel} channel The voice channel to join + * @param {VoiceChannel | StageChannel | DMChannel | GroupDMChannel | Snowflake} channel The voice channel to join * @param {JoinChannelConfig} config Config to join voice channel * @returns {Promise} */ joinChannel(channel, config = {}) { return new Promise((resolve, reject) => { + channel = this.client.channels.resolve(channel); if (!['DM', 'GROUP_DM'].includes(channel.type) && !channel.joinable) { throw new Error('VOICE_JOIN_CHANNEL', channel.full); } - let connection = this.connections.get(channel.guild?.id || channel.id); + let connection = this.connection; if (connection) { if (connection.channel.id !== channel.id) { - this.connections.get(channel.guild?.id || channel.id).updateChannel(channel); + this.connection.updateChannel(channel); } resolve(connection); return; @@ -124,11 +124,11 @@ class ClientVoiceManager { self_deaf: Boolean(config.selfDeaf), self_video: Boolean(config.selfVideo), }); - this.connections.set(channel.guild?.id || channel.id, connection); + this.connection = connection; } connection.once('failed', reason => { - this.connections.delete(channel.guild?.id || channel.id); + this.connection = null; reject(reason); }); @@ -139,7 +139,9 @@ class ClientVoiceManager { resolve(connection); connection.removeListener('error', reject); }); - connection.once('disconnect', () => this.connections.delete(channel.guild?.id || channel.id)); + connection.once('disconnect', () => { + this.connection = null; + }); }); }); } diff --git a/src/client/voice/VoiceConnection.js b/src/client/voice/VoiceConnection.js index fb041b0..33a5dbf 100644 --- a/src/client/voice/VoiceConnection.js +++ b/src/client/voice/VoiceConnection.js @@ -440,8 +440,8 @@ class VoiceConnection extends EventEmitter { this.emit('closing'); this.emit('debug', 'disconnect() triggered'); clearTimeout(this.connectTimeout); - const conn = this.voiceManager.connections.get(this.channel.guild?.id || this.channel.id); - if (conn === this) this.voiceManager.connections.delete(this.channel.guild?.id || this.channel.id); + const conn = this.voiceManager.connection; + if (conn === this) this.voiceManager.connection = null; this.sendVoiceStateUpdate({ channel_id: null, }); diff --git a/src/client/voice/util/Secretbox.js b/src/client/voice/util/Secretbox.js index c16a435..c679758 100644 --- a/src/client/voice/util/Secretbox.js +++ b/src/client/voice/util/Secretbox.js @@ -18,7 +18,17 @@ const libs = { }), }; -exports.methods = {}; +function NoLib() { + throw new Error( + 'Cannot play audio as no valid encryption package is installed.\n- Install sodium, libsodium-wrappers, or tweetnacl.', + ); +} + +exports.methods = { + open: NoLib, + close: NoLib, + random: NoLib, +}; (async () => { for (const libName of Object.keys(libs)) { diff --git a/typings/index.d.ts b/typings/index.d.ts index 6ea5108..bfb181c 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -882,10 +882,10 @@ export class ClientVoiceManager { private constructor(client: Client); public readonly client: Client; public adapters: Map; - public connections: Collection; + public connection: VoiceConnection | null; public joinChannel( - channel: VoiceChannel | StageChannel | DMChannel | GroupDMChannel, + channel: VoiceBasedChannel | DMChannel | GroupDMChannel | Snowflake, config?: JoinChannelConfig, ): Promise; }