woah is hydra actually working on voice?!

This commit is contained in:
Amish Shah
2016-10-23 19:51:44 +01:00
parent f3439dcf6d
commit 38ff53c3a1
4 changed files with 28 additions and 24 deletions

View File

@@ -160,25 +160,6 @@ class ClientVoiceManager {
this.client.on('self.voiceStateUpdate', this.onVoiceStateUpdate.bind(this)); this.client.on('self.voiceStateUpdate', this.onVoiceStateUpdate.bind(this));
} }
/**
* Checks whether a pending request can be processed
* @private
* @param {string} guildID The ID of the Guild
*/
_checkPendingReady(guildID) {
const pendingRequest = this.pending.get(guildID);
if (!pendingRequest) throw new Error('Guild not pending.');
if (pendingRequest.token && pendingRequest.sessionID && pendingRequest.endpoint) {
const { channel, token, sessionID, endpoint, resolve, reject } = pendingRequest;
const voiceConnection = new VoiceConnection(this, channel, token, sessionID, endpoint, resolve, reject);
this.pending.delete(guildID);
this.connections.set(guildID, voiceConnection);
voiceConnection.once('disconnected', () => {
this.connections.delete(guildID);
});
}
}
onVoiceServer(data) { onVoiceServer(data) {
if (this.pending.has(data.guild_id)) { if (this.pending.has(data.guild_id)) {
this.pending.get(data.guild_id).setTokenAndEndpoint(data.token, data.endpoint); this.pending.get(data.guild_id).setTokenAndEndpoint(data.token, data.endpoint);
@@ -260,6 +241,8 @@ class ClientVoiceManager {
pendingConnection.on('pass', voiceConnection => { pendingConnection.on('pass', voiceConnection => {
this.pending.delete(channel.guild.id); this.pending.delete(channel.guild.id);
this.connections.set(channel.guild.id, voiceConnection); this.connections.set(channel.guild.id, voiceConnection);
voiceConnection.once('ready', resolve);
voiceConnection.once('error', reject);
}); });
}); });
} }

View File

@@ -1,5 +1,5 @@
const VoiceWebSocket = require('./VoiceConnectionWebSocket'); const VoiceWebSocket = require('./VoiceWebSocket');
const VoiceUDP = require('./VoiceConnectionUDPClient'); const VoiceUDP = require('./VoiceUDPClient');
const VoiceReceiver = require('./receiver/VoiceReceiver'); const VoiceReceiver = require('./receiver/VoiceReceiver');
const Constants = require('../../util/Constants'); const Constants = require('../../util/Constants');
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
@@ -42,6 +42,7 @@ class VoiceConnection extends EventEmitter {
* @type {object} * @type {object}
*/ */
this.sockets = {}; this.sockets = {};
this.connect();
} }
connect() { connect() {
@@ -53,6 +54,21 @@ class VoiceConnection extends EventEmitter {
} }
this.sockets.ws = new VoiceWebSocket(this); this.sockets.ws = new VoiceWebSocket(this);
this.sockets.udp = new VoiceUDP(this); this.sockets.udp = new VoiceUDP(this);
this.sockets.ws.on('error', e => this.emit('error', e));
this.sockets.udp.on('error', e => this.emit('error', e));
this.sockets.ws.once('ready', d => {
this.authentication.port = d.port;
this.sockets.udp.findEndpointAddress()
.then(address => {
this.sockets.udp.createUDPSocket(address);
})
.catch(e => this.emit('error', e));
});
this.sockets.ws.once('sessionDescription', (mode, secret) => {
this.authentication.encryptionMode = mode;
this.authentication.secretKey = secret;
this.emit('ready');
});
} }
} }

View File

@@ -69,6 +69,7 @@ class VoiceConnectionUDPClient extends EventEmitter {
reject(error); reject(error);
return; return;
} }
this.discordAddress = address;
resolve(address); resolve(address);
}); });
}); });
@@ -82,11 +83,12 @@ class VoiceConnectionUDPClient extends EventEmitter {
send(packet) { send(packet) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.socket) { if (this.socket) {
if (!this.address || !this.port) { if (!this.discordAddress || !this.discordPort) {
console.log(this);
reject(new Error('malformed UDP address or port')); reject(new Error('malformed UDP address or port'));
return; return;
} }
this.socket.send(packet, 0, packet.length, this.port, this.address, error => { this.socket.send(packet, 0, packet.length, this.discordPort, this.discordAddress, error => {
if (error) { if (error) {
reject(error); reject(error);
} else { } else {

View File

@@ -21,6 +21,7 @@ class VoiceWebSocket extends EventEmitter {
* @type {number} * @type {number}
*/ */
this.attempts = 0; this.attempts = 0;
this.connect();
} }
/** /**
@@ -95,6 +96,7 @@ class VoiceWebSocket extends EventEmitter {
* @returns {Promise<string>} * @returns {Promise<string>}
*/ */
sendPacket(packet) { sendPacket(packet) {
console.log('try send', packet);
try { try {
packet = JSON.stringify(packet); packet = JSON.stringify(packet);
} catch (error) { } catch (error) {
@@ -127,7 +129,7 @@ class VoiceWebSocket extends EventEmitter {
*/ */
onMessage(event) { onMessage(event) {
try { try {
return this.onPacket(JSON.stringify(event.data)); return this.onPacket(JSON.parse(event.data));
} catch (error) { } catch (error) {
return this.onError(error); return this.onError(error);
} }
@@ -163,6 +165,7 @@ class VoiceWebSocket extends EventEmitter {
* @param {Object} packet the received packet * @param {Object} packet the received packet
* @event VoiceWebSocket#ready * @event VoiceWebSocket#ready
*/ */
console.log('hi', packet.d);
this.emit('ready', packet.d); this.emit('ready', packet.d);
break; break;
case Constants.VoiceOPCodes.SESSION_DESCRIPTION: case Constants.VoiceOPCodes.SESSION_DESCRIPTION: