From 5a5e3081052cf269e341863d83754812366c04d4 Mon Sep 17 00:00:00 2001 From: DevAndromeda <46562212+DevAndromeda@users.noreply.github.com> Date: Sat, 7 Aug 2021 21:19:31 +0545 Subject: [PATCH] feat(Player): add connectionTimeout option --- src/Player.ts | 3 ++- src/Structures/Queue.ts | 3 ++- src/VoiceInterface/StreamDispatcher.ts | 6 +++--- src/types/types.ts | 2 ++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Player.ts b/src/Player.ts index 392fb80..f67826e 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -24,7 +24,8 @@ class Player extends EventEmitter { autoRegisterExtractor: true, ytdlOptions: { highWaterMark: 1 << 25 - } + }, + connectionTimeout: 20000 }; public readonly queues = new Collection(); public readonly voiceUtils = new VoiceUtils(); diff --git a/src/Structures/Queue.ts b/src/Structures/Queue.ts index 620ba34..03c672c 100644 --- a/src/Structures/Queue.ts +++ b/src/Structures/Queue.ts @@ -146,7 +146,8 @@ class Queue { if (!["GUILD_STAGE_VOICE", "GUILD_VOICE"].includes(_channel?.type)) throw new PlayerError(`Channel type must be GUILD_VOICE or GUILD_STAGE_VOICE, got ${_channel?.type}!`, ErrorStatusCode.INVALID_ARG_TYPE); const connection = await this.player.voiceUtils.connect(_channel, { - deaf: this.options.autoSelfDeaf + deaf: this.options.autoSelfDeaf, + maxTime: this.player.options.connectionTimeout || 20000 }); this.connection = connection; diff --git a/src/VoiceInterface/StreamDispatcher.ts b/src/VoiceInterface/StreamDispatcher.ts index ecf431c..6f2f702 100644 --- a/src/VoiceInterface/StreamDispatcher.ts +++ b/src/VoiceInterface/StreamDispatcher.ts @@ -41,7 +41,7 @@ class StreamDispatcher extends EventEmitter { * @param {VoiceChannel|StageChannel} channel The connected channel * @private */ - constructor(connection: VoiceConnection, channel: VoiceChannel | StageChannel) { + constructor(connection: VoiceConnection, channel: VoiceChannel | StageChannel, public readonly connectionTimeout: number = 20000) { super(); /** @@ -72,7 +72,7 @@ class StreamDispatcher extends EventEmitter { if (newState.status === VoiceConnectionStatus.Disconnected) { if (newState.reason === VoiceConnectionDisconnectReason.WebSocketClose && newState.closeCode === 4014) { try { - await entersState(this.voiceConnection, VoiceConnectionStatus.Connecting, 5000); + await entersState(this.voiceConnection, VoiceConnectionStatus.Connecting, this.connectionTimeout); } catch { this.voiceConnection.destroy(); } @@ -87,7 +87,7 @@ class StreamDispatcher extends EventEmitter { } else if (!this.readyLock && (newState.status === VoiceConnectionStatus.Connecting || newState.status === VoiceConnectionStatus.Signalling)) { this.readyLock = true; try { - await entersState(this.voiceConnection, VoiceConnectionStatus.Ready, 20000); + await entersState(this.voiceConnection, VoiceConnectionStatus.Ready, this.connectionTimeout); } catch { if (this.voiceConnection.state.status !== VoiceConnectionStatus.Destroyed) this.voiceConnection.destroy(); } finally { diff --git a/src/types/types.ts b/src/types/types.ts index bba4d8b..b40c101 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -454,8 +454,10 @@ export interface PlaylistJSON { * @typedef {object} PlayerInitOptions * @property {boolean} [autoRegisterExtractor=true] If it should automatically register `@discord-player/extractor` * @property {YTDLDownloadOptions} [ytdlOptions={}] The options passed to `ytdl-core` + * @property {number} [connectionTimeout=20000] The voice connection timeout */ export interface PlayerInitOptions { autoRegisterExtractor?: boolean; ytdlOptions?: downloadOptions; + connectionTimeout?: number; }