From f826c9c75e976a8dca3fb4c88c130a1e12147a80 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Tue, 5 Feb 2019 10:26:16 +0000 Subject: [PATCH] voice: workaround for receiving audio (#2929 and discordapp/discord-api-docs#808) --- src/client/voice/VoiceConnection.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/client/voice/VoiceConnection.js b/src/client/voice/VoiceConnection.js index 078781b53..0242d541d 100644 --- a/src/client/voice/VoiceConnection.js +++ b/src/client/voice/VoiceConnection.js @@ -10,6 +10,15 @@ const EventEmitter = require('events'); const { Error } = require('../../errors'); const PlayInterface = require('./util/PlayInterface'); const Speaking = require('../../util/Speaking'); +const Silence = require('./util/Silence'); + +// Workaround for Discord now requiring silence to be sent before being able to receive audio +class SingleSilence extends Silence { + _read() { + super._read(); + this.push(null); + } +} const SUPPORTED_MODES = [ 'xsalsa20_poly1305_lite', @@ -419,13 +428,16 @@ class VoiceConnection extends EventEmitter { onSessionDescription(data) { Object.assign(this.authentication, data); this.status = VoiceStatus.CONNECTED; - clearTimeout(this.connectTimeout); - /** - * Emitted once the connection is ready, when a promise to join a voice channel resolves, - * the connection will already be ready. - * @event VoiceConnection#ready - */ - this.emit('ready'); + const dispatcher = this.play(new SingleSilence(), { type: 'opus' }); + dispatcher.on('finish', () => { + clearTimeout(this.connectTimeout); + /** + * Emitted once the connection is ready, when a promise to join a voice channel resolves, + * the connection will already be ready. + * @event VoiceConnection#ready + */ + this.emit('ready'); + }); } /**