From d3ae0ccb8ca7a9d68da174821586d1fdf0811b36 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Thu, 13 Aug 2015 23:28:30 +0100 Subject: [PATCH] Events are only received when the Client is ready for it Without this, any messages received during startup could cause crashes --- index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 91919648a..cf4c2abbc 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ exports.Client = function( options ) { this.websocket = null; this.events = {}; this.user = null; - + this.ready = false; this.serverList = new List( "id" ); this.PMList = new List( "id" ); @@ -25,6 +25,10 @@ exports.Client = function( options ) { exports.Client.prototype.triggerEvent = function( event, args ) { + if ( !this.ready ) { //if we're not even loaded yet, don't try doing anything because it always ends badly! + return; + } + if ( this.events[ event ] ) { this.events[ event ].apply( this, args ); } else { @@ -149,6 +153,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) { client.cacheServer( sID, function( server ) { cached++; if ( cached >= toCache ) { + client.ready = true; client.triggerEvent( "ready" ); } }, _server.members ); @@ -391,7 +396,7 @@ exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions //Channel is an ID var chanId = channel; channel = { - id : chanId + id: chanId } }