add disconnect event, document warn and debug events (#702)

* add documentation for events, and add a disconnect event, because i know people use that

* generate docs, and fix a hastily copied docstring

* fix permissions freak out
This commit is contained in:
Gus Caplan
2016-09-13 23:12:10 -05:00
committed by Schuyler Cebulskie
parent 25531170ec
commit 7cb2e8eef7
6 changed files with 26 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@@ -25,10 +25,14 @@ class ClientManager {
* @param {function} reject Function to run when connection fails
*/
connectToWebSocket(token, resolve, reject) {
this.client.emit('debug', `Authenticated using token ${token}`);
/**
* Emitted when there is debug information
* @event Client#debug
*/
this.client.emit(Constants.Events.DEBUG, `Authenticated using token ${token}`);
this.client.token = token;
this.client.rest.methods.getGateway().then(gateway => {
this.client.emit('debug', `Using gateway ${gateway}`);
this.client.emit(Constants.Events.DEBUG, `Using gateway ${gateway}`);
this.client.ws.connect(gateway);
this.client.once(Constants.Events.READY, () => resolve(token));
}).catch(reject);

View File

@@ -48,7 +48,7 @@ class VoiceConnectionPlayer extends EventEmitter {
killStream(stream) {
const streams = this.processMap.get(stream);
this._streamingData = this.dispatcher.streamingData;
this.emit('debug', 'Cleaning up player after audio stream ended or encountered an error');
this.emit(Constants.Events.DEBUG, 'Cleaning up player after audio stream ended or encountered an error');
const dummyHandler = () => null;
@@ -62,25 +62,25 @@ class VoiceConnectionPlayer extends EventEmitter {
streams.pcmConverter.stdout.once('error', dummyHandler);
if (streams.inputStream.unpipe) {
streams.inputStream.unpipe(streams.pcmConverter.stdin);
this.emit('debug', '- Unpiped input stream');
this.emit(Constants.Events.DEBUG, '- Unpiped input stream');
} else if (streams.inputStream.destroy) {
streams.inputStream.destroy();
this.emit('debug', '- Couldn\'t unpipe input stream, so destroyed input stream');
this.emit(Constants.Events.DEBUG, '- Couldn\'t unpipe input stream, so destroyed input stream');
}
if (streams.pcmConverter.stdin) {
streams.pcmConverter.stdin.end();
this.emit('debug', '- Ended input stream to PCM converter');
this.emit(Constants.Events.DEBUG, '- Ended input stream to PCM converter');
}
if (streams.pcmConverter && streams.pcmConverter.kill) {
streams.pcmConverter.kill('SIGINT');
this.emit('debug', '- Killed the pcm converter');
this.emit(Constants.Events.DEBUG, '- Killed the pcm converter');
}
} catch (err) {
// if an error happened make sure the pcm converter is killed anyway
try {
if (streams.pcmConverter && streams.pcmConverter.kill) {
streams.pcmConverter.kill('SIGINT');
this.emit('debug', '- Killed the pcm converter after previous error (abnormal)');
this.emit(Constants.Events.DEBUG, '- Killed the pcm converter after previous error (abnormal)');
}
} catch (e) {
return e;

View File

@@ -157,6 +157,11 @@ class WebSocketManager {
* @param {Object} event The received websocket data
*/
eventClose(event) {
/**
* Emitted whenever the client websocket is disconnected
* @event Client#disconnect
*/
if (!this.reconnecting) this.client.emit(Constants.Events.DISCONNECT);
if (event.code === 4004) throw new Error(Constants.Errors.BAD_LOGIN);
if (!this.reconnecting && event.code !== 1000) this.tryReconnect();
}
@@ -224,7 +229,11 @@ class WebSocketManager {
if (this.client.options.fetch_all_members) {
const promises = this.client.guilds.array().map(g => g.fetchMembers());
Promise.all(promises).then(() => this._emitReady()).catch(e => {
this.client.emit('warn', `Error on pre-ready guild member fetching - ${e}`);
/**
* Emitted when there is a warning
* @event Client#warn
*/
this.client.emit(Constants.Event.WARN, `Error on pre-ready guild member fetching - ${e}`);
this._emitReady();
});
return;

View File

@@ -11,7 +11,7 @@ class TypingStartHandler extends AbstractHandler {
if (channel && user) {
if (channel.type === 'voice') {
client.emit('warn', `Discord sent a typing packet to voice channel ${channel.id}`);
client.emit(Constants.Events.WARN, `Discord sent a typing packet to voice channel ${channel.id}`);
return;
}
if (channel._typing.has(user.id)) {

View File

@@ -163,10 +163,12 @@ exports.Events = {
MESSAGE_CREATE: 'message',
MESSAGE_DELETE: 'messageDelete',
MESSAGE_UPDATE: 'messageUpdate',
DISCONNECT: 'disconnect',
RECONNECTING: 'reconnecting',
GUILD_MEMBER_SPEAKING: 'guildMemberSpeaking',
MESSAGE_BULK_DELETE: 'messageDeleteBulk',
CHANNEL_PINS_UPDATE: 'channelPinsUpdate',
DEBUG: 'debug',
};
exports.WSEvents = {