Clean up a bunch of new voice stuff

This commit is contained in:
Schuyler Cebulskie
2016-10-25 20:26:57 -04:00
parent a04094f0ff
commit d1e9d15a1c
9 changed files with 57 additions and 76 deletions

View File

@@ -11,22 +11,26 @@ const EventEmitter = require('events').EventEmitter;
class PendingVoiceConnection extends EventEmitter {
constructor(voiceManager, channel) {
super();
/**
* The ClientVoiceManager that instantiated this pending connection
* @type {ClientVoiceManager}
*/
this.voiceManager = voiceManager;
/**
* The channel that this pending voice connection will attempt to join
* @type {VoiceChannel}
*/
this.channel = channel;
/**
* The timeout that will be invoked after 15 seconds signifying a failure to connect
* @type {Timeout}
*/
this.deathTimer = this.voiceManager.client.setTimeout(
() => this.fail(new Error('Automatic failure after 15 seconds')), 15000);
() => this.fail(new Error('Connection not established within 15 seconds.')), 15000);
/**
* An object containing data required to connect to the voice servers with
* @type {object}
@@ -53,26 +57,26 @@ class PendingVoiceConnection extends EventEmitter {
*/
setTokenAndEndpoint(token, endpoint) {
if (!token) {
this.fail(new Error('Token not provided from voice server packet'));
this.fail(new Error('Token not provided from voice server packet.'));
return;
}
if (!endpoint) {
this.fail(new Error('Endpoint not provided from voice server packet'));
this.fail(new Error('Endpoint not provided from voice server packet.'));
return;
}
if (this.data.token) {
this.fail(new Error('There is already a registered token for this connection'));
this.fail(new Error('There is already a registered token for this connection.'));
return;
}
if (this.data.endpoint) {
this.fail(new Error('There is already a registered endpoint for this connection'));
this.fail(new Error('There is already a registered endpoint for this connection.'));
return;
}
endpoint = endpoint.match(/([^:]*)/)[0];
if (!endpoint) {
this.fail(new Error('failed to find an endpoint'));
this.fail(new Error('Failed to find an endpoint.'));
return;
}
@@ -88,11 +92,11 @@ class PendingVoiceConnection extends EventEmitter {
*/
setSessionID(sessionID) {
if (!sessionID) {
this.fail(new Error('Session ID not supplied'));
this.fail(new Error('Session ID not supplied.'));
return;
}
if (this.data.session_id) {
this.fail(new Error('There is already a registered session ID for this connection'));
this.fail(new Error('There is already a registered session ID for this connection.'));
return;
}
this.data.session_id = sessionID;
@@ -161,15 +165,11 @@ class ClientVoiceManager {
}
onVoiceServer(data) {
if (this.pending.has(data.guild_id)) {
this.pending.get(data.guild_id).setTokenAndEndpoint(data.token, data.endpoint);
}
if (this.pending.has(data.guild_id)) this.pending.get(data.guild_id).setTokenAndEndpoint(data.token, data.endpoint);
}
onVoiceStateUpdate(data) {
if (this.pending.has(data.guild_id)) {
this.pending.get(data.guild_id).setSessionID(data.session_id);
}
if (this.pending.has(data.guild_id)) this.pending.get(data.guild_id).setSessionID(data.session_id);
}
/**
@@ -178,15 +178,13 @@ class ClientVoiceManager {
* @param {Object} [options] The options to provide
*/
sendVoiceStateUpdate(channel, options = {}) {
if (!this.client.user) {
throw new Error('You cannot join because there is no client user');
}
if (!this.client.user) throw new Error('Unable to join because there is no client user.');
if (channel.permissionsFor) {
const permissions = channel.permissionsFor(this.client.user);
if (permissions) {
if (!permissions.hasPermission('CONNECT')) {
throw new Error('You do not have permission to connect to this voice channel');
throw new Error('You do not have permission to connect to this voice channel.');
}
} else {
throw new Error('There is no permission set for the client user in this channel - are they part of the guild?');
@@ -215,10 +213,7 @@ class ClientVoiceManager {
*/
joinChannel(channel) {
return new Promise((resolve, reject) => {
// if already connecting to this voice server, error
if (this.pending.get(channel.guild.id)) {
throw new Error(`Already connecting to this guild's voice server.`);
}
if (this.pending.get(channel.guild.id)) throw new Error('Already connecting to this guild\'s voice server.');
const existingConnection = this.connections.get(channel.guild.id);
if (existingConnection) {