voice: remove createReceiver, just use VoiceConnection.receiver

This commit is contained in:
Amish Shah
2018-08-16 18:30:54 +01:00
parent 97c34b5b6f
commit e2726f5a9a
3 changed files with 13 additions and 28 deletions

View File

@@ -54,12 +54,6 @@ class VoiceConnection extends EventEmitter {
*/ */
this.speaking = false; this.speaking = false;
/**
* An array of Voice Receivers that have been created for this connection
* @type {VoiceReceiver[]}
*/
this.receivers = [];
/** /**
* The authentication data needed to connect to the voice server * The authentication data needed to connect to the voice server
* @type {Object} * @type {Object}
@@ -114,6 +108,12 @@ class VoiceConnection extends EventEmitter {
*/ */
this.sockets = {}; this.sockets = {};
/**
* The voice receiver of this connection
* @type {VoiceReceiver}
*/
this.receiver = null;
this.authenticate(); this.authenticate();
} }
@@ -417,6 +417,7 @@ class VoiceConnection extends EventEmitter {
Object.assign(this.authentication, data); Object.assign(this.authentication, data);
this.status = VoiceStatus.CONNECTED; this.status = VoiceStatus.CONNECTED;
clearTimeout(this.connectTimeout); clearTimeout(this.connectTimeout);
this.receiver = new VoiceReceiver(this);
/** /**
* Emitted once the connection is ready, when a promise to join a voice channel resolves, * Emitted once the connection is ready, when a promise to join a voice channel resolves,
* the connection will already be ready. * the connection will already be ready.
@@ -446,9 +447,7 @@ class VoiceConnection extends EventEmitter {
if (this.status === VoiceStatus.CONNECTED) { if (this.status === VoiceStatus.CONNECTED) {
this.emit('speaking', user, speaking); this.emit('speaking', user, speaking);
if (!speaking) { if (!speaking) {
for (const receiver of this.receivers) { this.receiver.packets._stoppedSpeaking(user_id);
receiver.packets._stoppedSpeaking(user_id);
}
} }
} }
@@ -466,17 +465,6 @@ class VoiceConnection extends EventEmitter {
} }
} }
/**
* Creates a VoiceReceiver so you can start listening to voice data.
* It's recommended to only create one of these.
* @returns {VoiceReceiver}
*/
createReceiver() {
const receiver = new VoiceReceiver(this);
this.receivers.push(receiver);
return receiver;
}
play() {} // eslint-disable-line no-empty-function play() {} // eslint-disable-line no-empty-function
} }

View File

@@ -180,12 +180,10 @@ class VoiceWebSocket extends EventEmitter {
this.connection.ssrcMap.set(+packet.d.audio_ssrc, packet.d.user_id); this.connection.ssrcMap.set(+packet.d.audio_ssrc, packet.d.user_id);
break; break;
case VoiceOPCodes.CLIENT_DISCONNECT: case VoiceOPCodes.CLIENT_DISCONNECT:
for (const receiver of this.connection.receivers) { const streamInfo = this.connection.receiver.packets.streams.get(packet.d.user_id);
const streamInfo = receiver.packets.streams.get(packet.d.user_id); if (streamInfo) {
if (streamInfo) { this.connection.receiver.packets.streams.delete(packet.d.user_id);
receiver.packets.streams.delete(packet.d.user_id); streamInfo.stream.push(null);
streamInfo.stream.push(null);
}
} }
break; break;
case VoiceOPCodes.SPEAKING: case VoiceOPCodes.SPEAKING:

View File

@@ -41,8 +41,7 @@ client.on('message', m => {
const channel = m.guild.channels.get(m.content.split(' ')[1]) || m.member.voice.channel; const channel = m.guild.channels.get(m.content.split(' ')[1]) || m.member.voice.channel;
if (channel && channel.type === 'voice') { if (channel && channel.type === 'voice') {
channel.join().then(conn => { channel.join().then(conn => {
const receiver = conn.createReceiver(); conn.receiver.createStream(m.author, true).on('data', b => console.log(b.toString()));
receiver.createStream(m.author, true).on('data', b => console.log(b.toString()));
conn.player.on('error', (...e) => console.log('player', ...e)); conn.player.on('error', (...e) => console.log('player', ...e));
if (!connections.has(m.guild.id)) connections.set(m.guild.id, { conn, queue: [] }); if (!connections.has(m.guild.id)) connections.set(m.guild.id, { conn, queue: [] });
m.reply('ok!'); m.reply('ok!');