From 54c62f240af92e4ac1f2868d119d5c4d2eb0c53e Mon Sep 17 00:00:00 2001 From: Manuel <30572287+manuel-rw@users.noreply.github.com> Date: Thu, 2 Mar 2023 20:40:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20Fix=20voice=20connection=20being?= =?UTF-8?q?=20stuck=20in=20signaling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clients/discord/discord.voice.service.ts | 28 ++++++++++++++++++++ src/models/shared/GenericPlaylist.ts | 2 +- src/utils/constants.ts | 6 ++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/clients/discord/discord.voice.service.ts b/src/clients/discord/discord.voice.service.ts index 619d408..48a6b67 100644 --- a/src/clients/discord/discord.voice.service.ts +++ b/src/clients/discord/discord.voice.service.ts @@ -9,6 +9,7 @@ import { joinVoiceChannel, NoSubscriberBehavior, VoiceConnection, + VoiceConnectionStatus, } from '@discordjs/voice'; import { Injectable } from '@nestjs/common'; @@ -216,6 +217,33 @@ export class DiscordVoiceService { } private attachEventListenersToAudioPlayer() { + this.voiceConnection.on('debug', (message) => { + if (process.env.DEBUG?.toLowerCase() !== 'true') { + return; + } + this.logger.debug(message); + }); + this.voiceConnection.on('error', (err) => { + this.logger.error(`Voice connection error: ${err}`); + }); + + // Tempoary keep alive fix for servers, see https://github.com/discordjs/discord.js/issues/9185 + this.voiceConnection.on('stateChange', (oldState, newState) => { + const oldNetworking = Reflect.get(oldState, 'networking'); + const newNetworking = Reflect.get(newState, 'networking'); + + const networkStateChangeHandler = ( + oldNetworkState: any, + newNetworkState: any, + ) => { + const newUdp = Reflect.get(newNetworkState, 'udp'); + clearInterval(newUdp?.keepAliveInterval); + }; + + oldNetworking?.off('stateChange', networkStateChangeHandler); + newNetworking?.on('stateChange', networkStateChangeHandler); + }); + this.audioPlayer.on('debug', (message) => { this.logger.debug(message); }); diff --git a/src/models/shared/GenericPlaylist.ts b/src/models/shared/GenericPlaylist.ts index 74444e2..01c547f 100644 --- a/src/models/shared/GenericPlaylist.ts +++ b/src/models/shared/GenericPlaylist.ts @@ -90,7 +90,7 @@ export class GenericPlaylist { * @returns if there is a track next in the playlist */ hasNextTrackInPlaylist() { - return this.activeTrackIndex < this.tracks.length; + return this.activeTrackIndex + 1 < this.tracks.length; } /** diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 5fffa28..0883ea0 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -33,11 +33,11 @@ export const Constants = { InvisibleSpace: '\u1CBC', Icons: { JellyfinLogo: - 'https://github.com/manuel-rw/jellyfin-discord-music-bot/blob/nestjs-migration/images/icons/jellyfin-icon-squared.png?raw=true', + 'https://raw.githubusercontent.com/manuel-rw/jellyfin-discord-music-bot/master/images/icons/jellyfin-icon-squared.png', SuccessIcon: - 'https://github.com/manuel-rw/jellyfin-discord-music-bot/blob/nestjs-migration/images/icons/circle-check.png?raw=true', + 'https://raw.githubusercontent.com/manuel-rw/jellyfin-discord-music-bot/master/images/icons/circle-check.png', ErrorIcon: - 'https://github.com/manuel-rw/jellyfin-discord-music-bot/blob/nestjs-migration/images/icons/alert-circle.png?raw=true', + 'https://raw.githubusercontent.com/manuel-rw/jellyfin-discord-music-bot/master/images/icons/alert-circle.png', }, }, };