Documentation

This commit is contained in:
Amish Shah
2016-08-23 21:16:49 +01:00
parent d0a76f3f38
commit f9a553a7f0
5 changed files with 76 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@@ -130,6 +130,15 @@ class Client extends EventEmitter {
return this.rest.methods.loginToken(email);
}
/**
* Returns a Collection, mapping Guild ID to Voice Connections.
* @readonly
* @type {Collection<String, VoiceConnection>}
*/
get voiceConnections() {
return this.voice.connections;
}
}
module.exports = Client;

View File

@@ -26,14 +26,19 @@ class ClientVoiceManager {
this.pending = new Collection();
}
/**
* 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 { token, sessionID, endpoint, resolve, reject } = pendingRequest;
const voiceConnection = new VoiceConnection(this, guildID, token, sessionID, endpoint, resolve, reject);
const { channel, token, sessionID, endpoint, resolve, reject } = pendingRequest;
const voiceConnection = new VoiceConnection(this, channel, token, sessionID, endpoint, resolve, reject);
this.connections.set(guildID, voiceConnection);
}
}

View File

@@ -2,23 +2,71 @@ const VoiceConnectionWebSocket = require('./VoiceConnectionWebSocket');
const VoiceConnectionUDPClient = require('./VoiceConnectionUDPClient');
const EventEmitter = require('events').EventEmitter;
/**
* Represents a connection to a Voice Channel in Discord
* @extends {EventEmitter}
*/
class VoiceConnection extends EventEmitter {
constructor(manager, serverID, token, sessionID, endpoint, resolve, reject) {
constructor(manager, channel, token, sessionID, endpoint, resolve, reject) {
super();
/**
* The voice manager of this connection
* @type {ClientVoiceManager}
* @private
*/
this.manager = manager;
/**
* The endpoint of the connection
* @type {String}
*/
this.endpoint = endpoint;
this.websocket = new VoiceConnectionWebSocket(this, serverID, token, sessionID, endpoint);
/**
* The VoiceChannel for this connection
* @type {VoiceChannel}
*/
this.channel = channel;
/**
* The WebSocket connection for this voice connection
* @type {VoiceConnectionWebSocket}
* @private
*/
this.websocket = new VoiceConnectionWebSocket(this, channel.guild.id, token, sessionID, endpoint);
/**
* Whether or not the connection is ready
* @type {Boolean}
*/
this.ready = false;
/**
* The resolve function for the promise associated with creating this connection
* @type {Function}
* @private
*/
this._resolve = resolve;
/**
* The reject function for the promise associated with creating this connection
* @type {Function}
* @private
*/
this._reject = reject;
this.bindListeners();
}
/**
* Executed whenever an error occurs with the UDP/WebSocket sub-client
* @private
* @param {Error} error
* @returns {null}
*/
_onError(e) {
this._reject(e);
this.emit('error', e);
}
/**
* Binds listeners to the WebSocket and UDP sub-clients
* @returns {null}
* @private
*/
bindListeners() {
this.websocket.on('error', err => this._onError(err));
this.websocket.on('ready-for-udp', data => {

View File

@@ -43,6 +43,15 @@ class VoiceChannel extends GuildChannel {
return this.rest.client.rest.methods.updateChannel(this, { bitrate });
}
/**
* Attempts to join this Voice Channel
* @returns {Promise<VoiceConnection, Error>}
* @example
* // join a voice channel
* voiceChannel.join()
* .then(connection => console.log('Connected!'))
* .catch(console.log);
*/
join() {
return this.client.voice.joinChannel(this);
}