Most arrays are now using lists.

This commit is contained in:
hydrabolt
2015-08-12 16:05:21 +01:00
parent 401073f897
commit dda5d4272e
4 changed files with 321 additions and 307 deletions

270
index.js
View File

@@ -1,13 +1,13 @@
var request = require("superagent");
var Endpoints = require("./lib/endpoints.js");
var Server = require("./lib/server.js").Server;
var Message = require("./lib/message.js").Message;
var User = require("./lib/user.js").User;
var Channel = require("./lib/channel.js").Channel;
var List = require("./lib/list.js").List;
var WebSocket = require('ws');
var request = require( "superagent" );
var Endpoints = require( "./lib/endpoints.js" );
var Server = require( "./lib/server.js" ).Server;
var Message = require( "./lib/message.js" ).Message;
var User = require( "./lib/user.js" ).User;
var Channel = require( "./lib/channel.js" ).Channel;
var List = require( "./lib/list.js" ).List;
var WebSocket = require( 'ws' );
exports.Client = function(options) {
exports.Client = function( options ) {
this.options = options || {};
this.token = "";
@@ -16,62 +16,62 @@ exports.Client = function(options) {
this.events = {};
this.user = null;
this.serverList = new List("id");
this.serverList = new List( "id" );
}
exports.Client.prototype.triggerEvent = function(event, args) {
exports.Client.prototype.triggerEvent = function( event, args ) {
if (this.events[event]) {
this.events[event].apply(this, args);
if ( this.events[ event ] ) {
this.events[ event ].apply( this, args );
} else {
return false;
}
}
exports.Client.prototype.on = function(name, fn) {
this.events[name] = fn;
exports.Client.prototype.on = function( name, fn ) {
this.events[ name ] = fn;
}
exports.Client.prototype.off = function(name) {
this.events[name] = function() {};
exports.Client.prototype.off = function( name ) {
this.events[ name ] = function() {};
}
exports.Client.prototype.cacheServer = function(id, cb, members) {
exports.Client.prototype.cacheServer = function( id, cb, members ) {
if ( this.serverList.filter("id", id).length > 0 ) {
if ( this.serverList.filter( "id", id ).length > 0 ) {
return;
}
var self = this;
request
.get(Endpoints.SERVERS + "/" + id)
.set("authorization", this.token)
.end(function(err, res) {
.get( Endpoints.SERVERS + "/" + id )
.set( "authorization", this.token )
.end( function( err, res ) {
var dat = res.body;
var server = new Server(dat.region, dat.owner_id, dat.name, dat.roles[0].id, members || dat.members);
var server = new Server( dat.region, dat.owner_id, dat.name, dat.roles[ 0 ].id, members || dat.members );
request
.get(Endpoints.SERVERS + "/" + id + "/channels")
.set("authorization", self.token)
.end(function(err, res){
.get( Endpoints.SERVERS + "/" + id + "/channels" )
.set( "authorization", self.token )
.end( function( err, res ) {
var channelList = res.body;
for(channel of channelList){
server.channels.add( new Channel(channel, server) );
for ( channel of channelList ) {
server.channels.add( new Channel( channel, server ) );
}
self.serverList.add(server);
self.serverList.add( server );
cb(server);
});
});
cb( server );
} );
} );
}
exports.Client.prototype.login = function(email, password) {
exports.Client.prototype.login = function( email, password ) {
var client = this;
@@ -81,79 +81,80 @@ exports.Client.prototype.login = function(email, password) {
};
request
.post(Endpoints.LOGIN)
.send(details)
.end(function(err, res) {
if (!res.ok) {
client.triggerEvent("disconnected", {
reason : "failed to log in",
error : err
});
.post( Endpoints.LOGIN )
.send( details )
.end( function( err, res ) {
if ( !res.ok ) {
client.triggerEvent( "disconnected", {
reason: "failed to log in",
error: err
} );
} else {
client.token = res.body.token;
client.loggedIn = true;
client.connectWebsocket();
}
});
} );
}
exports.Client.prototype.connectWebsocket = function(cb) {
exports.Client.prototype.connectWebsocket = function( cb ) {
var client = this;
this.websocket = new WebSocket(Endpoints.WEBSOCKET_HUB);
this.websocket.onclose = function(e) {
client.triggerEvent("disconnected", {
reason : "websocket disconnected",
error : e
});
this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB );
this.websocket.onclose = function( e ) {
client.triggerEvent( "disconnected", {
reason: "websocket disconnected",
error: e
} );
};
this.websocket.onmessage = function(e) {
this.websocket.onmessage = function( e ) {
var dat = JSON.parse(e.data);
switch (dat.op) {
var dat = JSON.parse( e.data );
switch ( dat.op ) {
case 0:
if (dat.t === "READY") {
if ( dat.t === "READY" ) {
var data = dat.d;
self = this;
setInterval(function() {
self.keepAlive.apply(self);
}, data.heartbeat_interval);
setInterval( function() {
self.keepAlive.apply( self );
}, data.heartbeat_interval );
var _servers = data.guilds,
servers = [];
var cached = 0, toCache = _servers.length;
var cached = 0,
toCache = _servers.length;
for (x in _servers) {
_server = _servers[x];
client.cacheServer(_server.roles[0].id, function(server) {
for ( x in _servers ) {
_server = _servers[ x ];
client.cacheServer( _server.roles[ 0 ].id, function( server ) {
cached++;
if(cached >= toCache){
client.triggerEvent("ready");
if ( cached >= toCache ) {
client.triggerEvent( "ready" );
}
}, _server.members);
}, _server.members );
}
client.user = new User(data.user.username, data.user.id, data.user.discriminator, data.user.avatar);
} else if (dat.t === "MESSAGE_CREATE") {
client.user = new User( data.user.username, data.user.id, data.user.discriminator, data.user.avatar );
} else if ( dat.t === "MESSAGE_CREATE" ) {
var data = dat.d;
var channel = client.channelFromId(data.channel_id);
var channel = client.channelFromId( data.channel_id );
var message = new Message(data, channel);
var message = new Message( data, channel );
client.triggerEvent("message", [message]);
client.triggerEvent( "message", [ message ] );
} else if (dat.t === "PRESENCE_UPDATE"){
} else if ( dat.t === "PRESENCE_UPDATE" ) {
var data = dat.d;
client.triggerEvent("presence", [new User(data.user), data.status, client.serverList.filter("id", data.guild_id, true)]);
client.triggerEvent( "presence", [ new User( data.user ), data.status, client.serverList.filter( "id", data.guild_id, true ) ] );
}
break;
@@ -161,18 +162,18 @@ exports.Client.prototype.connectWebsocket = function(cb) {
}
};
this.websocket.sendPacket = function(p) {
this.send(JSON.stringify(p));
this.websocket.sendPacket = function( p ) {
this.send( JSON.stringify( p ) );
}
this.websocket.keepAlive = function() {
if(this.readyState !== 1)
if ( this.readyState !== 1 )
return false;
this.sendPacket({
this.sendPacket( {
op: 1,
d: Date.now()
});
} );
}
this.websocket.onopen = function() {
@@ -186,14 +187,14 @@ exports.Client.prototype.connectWebsocket = function(cb) {
};
connDat.d.properties = {
"$os": "DiscordJS",
"$browser": "Dischromecord", // ;)
"$os": "discord.js",
"$browser": "discord.js",
"$device": "discord.js",
"$referrer": "",
"$referring_domain": ""
};
this.sendPacket(connDat);
this.sendPacket( connDat );
}
}
@@ -202,99 +203,118 @@ exports.Client.prototype.logout = function() {
var client = this;
request
.post(Endpoints.LOGOUT)
.end(function() {
.post( Endpoints.LOGOUT )
.end( function() {
client.loggedIn = false;
});
} );
}
exports.Client.prototype.createServer = function(details, cb) {
exports.Client.prototype.createServer = function( details, cb ) {
var client = this;
request
.post(Endpoints.SERVERS)
.set("authorization", client.token)
.send(details)
.end(function(err, res) {
if (!res.ok) {
cb(err);
.post( Endpoints.SERVERS )
.set( "authorization", client.token )
.send( details )
.end( function( err, res ) {
if ( !res.ok ) {
cb( err );
} else {
cb(new Server(res.body));
cb( new Server( res.body ) );
}
});
} );
}
exports.Client.prototype.sendMessage = function(channel, message, cb, _mentions) {
exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions, options ) {
var cb = cb || function(){};
options = options || {};
var cb = cb || function() {};
if ( _mentions === false ) {
//mentions is false, explicitly don't want to mention someone
_mentions = [];
} else if ( _mentions === true || _mentions === "auto" || _mentions == null || _mentions == undefined ) {
//want to auto sort mentions
_mentions = [];
var mentionsArray = message.match( /<[^>]*>/g ) || [];
for ( mention of mentionsArray ) {
_mentions.push( mention.substring( 2, mention.length - 1 ) );
}
} else if ( _mentions instanceof Array ) {
//specific mentions
for ( mention in _mentions ) {
_mentions[ mention ] = _mentions[ mention ].id;
}
} else {
for (mention in _mentions) {
_mentions[mention] = _mentions[mention].id;
}
var client = this;
var details = {
content: message.substring(0,2000),
content: message.substring( 0, 2000 ),
mentions: _mentions || []
};
request
.post(Endpoints.CHANNELS + "/" + channel.id + "/messages")
.set("authorization", client.token)
.send(details)
.end(function(err, res) {
cb(new Message(res.body, client.channelFromId(res.body.channel_id)));
});
.post( Endpoints.CHANNELS + "/" + channel.id + "/messages" )
.set( "authorization", client.token )
.send( details )
.end( function( err, res ) {
var msg = new Message( res.body, client.channelFromId( res.body.channel_id ) );
if ( options.selfDestruct ) {
setTimeout( function() {
client.deleteMessage( msg );
}, options.selfDestruct );
}
cb( msg );
} );
}
exports.Client.prototype.deleteMessage = function(message) {
exports.Client.prototype.deleteMessage = function( message ) {
if(!message)
if ( !message )
return false;
var client = this;
request
.del(Endpoints.CHANNELS + "/" + message.channel.id + "/messages/" + message.id)
.set("authorization", client.token)
.end(function(err, res) {
});
.del( Endpoints.CHANNELS + "/" + message.channel.id + "/messages/" + message.id )
.set( "authorization", client.token )
.end();
}
exports.Client.prototype.channelFromId = function(id){
var channelList = this.serverList.concatSublists("channels", "id");
var channel = channelList.filter("id", id, true);
exports.Client.prototype.channelFromId = function( id ) {
var channelList = this.serverList.concatSublists( "channels", "id" );
var channel = channelList.filter( "id", id, true );
return channel;
}
exports.Client.prototype.getChannelLogs = function(channel, amount, cb){
exports.Client.prototype.getChannelLogs = function( channel, amount, cb ) {
amount = amount || 0;
var client = this;
request
.get(Endpoints.CHANNELS + "/" + channel.id + "/messages?limit="+amount)
.set("authorization", client.token)
.end(function(err, res){
.get( Endpoints.CHANNELS + "/" + channel.id + "/messages?limit=" + amount )
.set( "authorization", client.token )
.end( function( err, res ) {
if(err){
cb(new List("id"));
if ( err ) {
cb( new List( "id" ) );
return;
}
var datList = new List("id");
var datList = new List( "id" );
for(item of res.body){
datList.add( new Message(item, channel) );
for ( item of res.body ) {
datList.add( new Message( item, channel ) );
}
cb(datList);
});
cb( datList );
} );
}

View File

@@ -1,72 +1,72 @@
exports.List = function(discriminator) {
exports.List = function( discriminator ) {
this.discriminator = discriminator;
this.contents = [];
}
exports.List.prototype.add = function(child){
if(child.constructor === Array){
exports.List.prototype.add = function( child ) {
if ( child.constructor === Array ) {
children = child;
for(child of children){
if( this.filter( this.discriminator, child[this.discriminator] ).length === 0 )
this.contents.push(child);
for ( child of children ) {
if ( this.filter( this.discriminator, child[ this.discriminator ] ).length === 0 )
this.contents.push( child );
}
}else{
if( this.filter( this.discriminator, child[this.discriminator] ).length === 0 )
this.contents.push(child);
} else {
if ( this.filter( this.discriminator, child[ this.discriminator ] ).length === 0 )
this.contents.push( child );
}
}
exports.List.prototype.length = function(){
exports.List.prototype.length = function() {
return this.contents.length;
}
exports.List.prototype.removeIndex = function(index){
this.contents.splice(index, 1);
exports.List.prototype.removeIndex = function( index ) {
this.contents.splice( index, 1 );
}
exports.List.prototype.removeChild = function(child){
exports.List.prototype.removeElement = function( child ) {
var index = this.contents.indexOf(child);
var index = this.contents.indexOf( child );
if( index === -1 ){
if ( index === -1 ) {
return false;
}
this.removeIndex(index);
this.removeIndex( index );
}
exports.List.prototype.concatSublists = function(whereList, discriminator){
exports.List.prototype.concatSublists = function( whereList, discriminator ) {
//this is meant to look at the contents, and assuming the contents are all lists, concatenate their values.
var concatList = new exports.List(discriminator);
var concatList = new exports.List( discriminator );
for(item of this.contents){
var itemList = item[whereList];
concatList.add(itemList.contents);
for ( item of this.contents ) {
var itemList = item[ whereList ];
concatList.add( itemList.contents );
}
return concatList;
}
exports.List.prototype.filter = function(key, value, onlyOne) {
exports.List.prototype.filter = function( key, value, onlyOne ) {
var results = [];
for (index in this.contents) {
var child = this.contents[index];
if (child[key] == value) {
if (onlyOne) {
for ( index in this.contents ) {
var child = this.contents[ index ];
if ( child[ key ] == value ) {
if ( onlyOne ) {
return child;
} else {
results.push(child);
results.push( child );
}
}
}
if(onlyOne){
if ( onlyOne ) {
return false;
}

View File

@@ -1,8 +1,9 @@
var User = require("./user.js").User;
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
exports.Message = function(time, author, content, channel, id, mentions){
exports.Message = function( time, author, content, channel, id, mentions, everyoneMentioned ) {
if(!content){
if ( !content ) {
message = time;
channel = author;
time = message.timestamp;
@@ -10,28 +11,22 @@ exports.Message = function(time, author, content, channel, id, mentions){
content = message.content;
id = message.id;
mentions = message.mentions;
everyoneMentioned = message.mention_everyone;
}
this.time = Date.parse(time);
this.author = new User(author);
this.content = content.replace(/<[^>]*>/g, "").replace(/\s+/g, ' ').trim();
this.time = Date.parse( time );
this.author = new User( author );
this.content = content.replace( /\s+/g, ' ' ).trim(); //content.replace(/<[^>]*>/g, "").replace(/\s+/g, ' ').trim();
this.channel = channel;
this.id = id;
this.mentions = mentions || [];
for(x in mentions){
var _mention = mentions[x];
this.mentions.push( new User(_mention.username, _mention.id, _mention.discriminator, _mention.avatar) );
this.mentions = new List( "id" );
this.everyoneMentioned = everyoneMentioned;
for ( x in mentions ) {
var _mention = mentions[ x ];
this.mentions.add( new User( _mention ) );
}
}
exports.Message.prototype.isMentioned = function(user){
for(mention of this.mentions){
if(user.equals(mention)){
return true;
}
}
return false;
exports.Message.prototype.isMentioned = function( user ) {
return ( this.mentions.filter( "id", user.id ).length > 0 );
}

View File

@@ -1,24 +1,23 @@
var User = require("./user.js").User;
var List = require("./list.js").List;
exports.Server = function(region, ownerID, name, id, members){
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
exports.Server = function( region, ownerID, name, id, members ) {
this.region = region;
this.ownerID = ownerID;
this.name = name;
this.id = id;
this.members = new List("id");
this.channels = new List("id");
this.members = new List( "id" );
this.channels = new List( "id" );
for(x in members){
var member = members[x].user;
this.members.add( new User(member) );
for ( x in members ) {
var member = members[ x ].user;
this.members.add( new User( member ) );
}
}
exports.Server.prototype.getDefaultChannel = function(){
exports.Server.prototype.getDefaultChannel = function() {
return this.channels.filter("name", "general", true);
return this.channels.filter( "name", "general", true );
}