Merge pull request #3 from hydrabolt/indev

bring minor bug fixes so developers don't encounter them
This commit is contained in:
hydrabolt
2015-08-13 23:33:43 +01:00
2 changed files with 23 additions and 6 deletions

View File

@@ -17,7 +17,7 @@ exports.Client = function( options ) {
this.websocket = null; this.websocket = null;
this.events = {}; this.events = {};
this.user = null; this.user = null;
this.ready = false;
this.serverList = new List( "id" ); this.serverList = new List( "id" );
this.PMList = new List( "id" ); this.PMList = new List( "id" );
@@ -25,6 +25,10 @@ exports.Client = function( options ) {
exports.Client.prototype.triggerEvent = function( event, args ) { 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 ] ) { if ( this.events[ event ] ) {
this.events[ event ].apply( this, args ); this.events[ event ].apply( this, args );
} else { } else {
@@ -149,6 +153,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
client.cacheServer( sID, function( server ) { client.cacheServer( sID, function( server ) {
cached++; cached++;
if ( cached >= toCache ) { if ( cached >= toCache ) {
client.ready = true;
client.triggerEvent( "ready" ); client.triggerEvent( "ready" );
} }
}, _server.members ); }, _server.members );
@@ -210,7 +215,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
if ( !srv.channels.filter( "id", dat.d.d, true ) ) { if ( !srv.channels.filter( "id", dat.d.d, true ) ) {
var chann = new Channel(dat.d, srv); var chann = new Channel( dat.d, srv );
srv.channels.add( new Channel( dat.d, srv ) ); srv.channels.add( new Channel( dat.d, srv ) );
client.triggerEvent( "channelCreate", [ chann ] ); client.triggerEvent( "channelCreate", [ chann ] );
@@ -383,6 +388,18 @@ exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions
} }
} }
if ( channel instanceof Message ) { //if the channel is actually a message, get the channel
channel = channel.channel;
}
if ( typeof channel === 'string' || channel instanceof String || !isNaN( channel ) ) {
//Channel is an ID
var chanId = channel;
channel = {
id: chanId
}
}
var cb = cb || function() {}; var cb = cb || function() {};
if ( _mentions === false ) { if ( _mentions === false ) {
@@ -462,8 +479,8 @@ exports.Client.prototype.getChannelLogs = function( channel, amount, cb ) {
.set( "authorization", client.token ) .set( "authorization", client.token )
.end( function( err, res ) { .end( function( err, res ) {
if ( err ) { if ( !res.ok ) {
cb( new List( "id" ) ); cb( err );
return; return;
} }
@@ -473,7 +490,7 @@ exports.Client.prototype.getChannelLogs = function( channel, amount, cb ) {
datList.add( new Message( item, channel ) ); datList.add( new Message( item, channel ) );
} }
cb( datList ); cb( null, datList );
} ); } );
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "discord.js", "name": "discord.js",
"version": "2.1.1", "version": "2.2.0",
"description": "A way to interface with the Discord API", "description": "A way to interface with the Discord API",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {