mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
woah is hydra actually working on voice?!
This commit is contained in:
@@ -160,25 +160,6 @@ class ClientVoiceManager {
|
||||
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) {
|
||||
if (this.pending.has(data.guild_id)) {
|
||||
this.pending.get(data.guild_id).setTokenAndEndpoint(data.token, data.endpoint);
|
||||
@@ -260,6 +241,8 @@ class ClientVoiceManager {
|
||||
pendingConnection.on('pass', voiceConnection => {
|
||||
this.pending.delete(channel.guild.id);
|
||||
this.connections.set(channel.guild.id, voiceConnection);
|
||||
voiceConnection.once('ready', resolve);
|
||||
voiceConnection.once('error', reject);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const VoiceWebSocket = require('./VoiceConnectionWebSocket');
|
||||
const VoiceUDP = require('./VoiceConnectionUDPClient');
|
||||
const VoiceWebSocket = require('./VoiceWebSocket');
|
||||
const VoiceUDP = require('./VoiceUDPClient');
|
||||
const VoiceReceiver = require('./receiver/VoiceReceiver');
|
||||
const Constants = require('../../util/Constants');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
@@ -42,6 +42,7 @@ class VoiceConnection extends EventEmitter {
|
||||
* @type {object}
|
||||
*/
|
||||
this.sockets = {};
|
||||
this.connect();
|
||||
}
|
||||
|
||||
connect() {
|
||||
@@ -53,6 +54,21 @@ class VoiceConnection extends EventEmitter {
|
||||
}
|
||||
this.sockets.ws = new VoiceWebSocket(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');
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@ class VoiceConnectionUDPClient extends EventEmitter {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
this.discordAddress = address;
|
||||
resolve(address);
|
||||
});
|
||||
});
|
||||
@@ -82,11 +83,12 @@ class VoiceConnectionUDPClient extends EventEmitter {
|
||||
send(packet) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.socket) {
|
||||
if (!this.address || !this.port) {
|
||||
if (!this.discordAddress || !this.discordPort) {
|
||||
console.log(this);
|
||||
reject(new Error('malformed UDP address or port'));
|
||||
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) {
|
||||
reject(error);
|
||||
} else {
|
||||
@@ -21,6 +21,7 @@ class VoiceWebSocket extends EventEmitter {
|
||||
* @type {number}
|
||||
*/
|
||||
this.attempts = 0;
|
||||
this.connect();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,6 +96,7 @@ class VoiceWebSocket extends EventEmitter {
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
sendPacket(packet) {
|
||||
console.log('try send', packet);
|
||||
try {
|
||||
packet = JSON.stringify(packet);
|
||||
} catch (error) {
|
||||
@@ -127,7 +129,7 @@ class VoiceWebSocket extends EventEmitter {
|
||||
*/
|
||||
onMessage(event) {
|
||||
try {
|
||||
return this.onPacket(JSON.stringify(event.data));
|
||||
return this.onPacket(JSON.parse(event.data));
|
||||
} catch (error) {
|
||||
return this.onError(error);
|
||||
}
|
||||
@@ -163,6 +165,7 @@ class VoiceWebSocket extends EventEmitter {
|
||||
* @param {Object} packet the received packet
|
||||
* @event VoiceWebSocket#ready
|
||||
*/
|
||||
console.log('hi', packet.d);
|
||||
this.emit('ready', packet.d);
|
||||
break;
|
||||
case Constants.VoiceOPCodes.SESSION_DESCRIPTION:
|
||||
|
||||
Reference in New Issue
Block a user