Deleted examples, beginning to write in EC6.

Examples and Hydrabot will soon live in a separate repo which is better
suited to learning - this is so the main package isn't bloated.
This commit is contained in:
hydrabolt
2015-08-23 16:55:23 +01:00
parent 16b007c635
commit 35b61312b9
40 changed files with 1830 additions and 2486 deletions

63
lib/Client.js Normal file
View File

@@ -0,0 +1,63 @@
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var request = require("superagent");
var defaultOptions = {
cache_tokens: false
};
var Client = (function () {
function Client() {
var options = arguments.length <= 0 || arguments[0] === undefined ? defaultOptions : arguments[0];
var token = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];
_classCallCheck(this, Client);
/*
When created, if a token is specified the Client will
try connecting with it. If the token is incorrect, no
further efforts will be made to connect.
*/
this.options = options;
this.token = token;
this.state = 0;
this.websocket = null;
this.events = new Map();
this.user = null;
/*
State values:
0 - idle
1 - logging in
2 - logged in
3 - ready
4 - disconnected
*/
}
_createClass(Client, [{
key: "login",
//def login
value: function login() {
var email = arguments.length <= 0 || arguments[0] === undefined ? "foo@bar.com" : arguments[0];
var password = arguments.length <= 1 || arguments[1] === undefined ? "pass1234s" : arguments[1];
if (this.state === 0 || this.state === 4) {
this.state = 1;
request.post();
}
}
}, {
key: "ready",
get: function get() {
return this.state === 3;
}
}]);
return Client;
})();

View File

@@ -1,6 +1,8 @@
"use strict";
var User = require("./user.js").User;
exports.PMChannel = function(user, id){
exports.PMChannel = function (user, id) {
this.user = new User(user);
this.id = id;
}
};

View File

@@ -1,68 +1,67 @@
var fs = require( "fs" );
var crypto = require( "crypto" );
var md5 = require( "md5" );
"use strict";
var fs = require("fs");
var crypto = require("crypto");
var md5 = require("md5");
var tokens = {};
exports.TokenManager = function( folder, file ) {
exports.TokenManager = function (folder, file) {
this.path = folder + file;
var self = this;
try {
var fd = fs.openSync( self.path, "wx" );
var fd = fs.openSync(self.path, "wx");
self.writeTokens();
} catch ( e ) {
} catch (e) {
self.readTokens();
}
};
}
exports.TokenManager.prototype.addToken = function( id, token, pass ) {
tokens[ md5( id ) ] = encrypt( token, pass );
exports.TokenManager.prototype.addToken = function (id, token, pass) {
tokens[md5(id)] = encrypt(token, pass);
this.writeTokens();
}
};
exports.TokenManager.prototype.readTokens = function() {
tokens = JSON.parse( fs.readFileSync( this.path, "utf8" ) );
for ( tkn in tokens ) {
tokens[ tkn ] = decrypt( tokens[ tkn ], tkn );
exports.TokenManager.prototype.readTokens = function () {
tokens = JSON.parse(fs.readFileSync(this.path, "utf8"));
for (tkn in tokens) {
tokens[tkn] = decrypt(tokens[tkn], tkn);
}
}
};
exports.TokenManager.prototype.writeTokens = function() {
exports.TokenManager.prototype.writeTokens = function () {
var tkn = {};
for ( token in tokens ) {
tkn[ token ] = encrypt( tokens[ token ], token );
for (token in tokens) {
tkn[token] = encrypt(tokens[token], token);
}
fs.writeFile( this.path, JSON.stringify( tkn ), function( err ) {
fs.writeFile(this.path, JSON.stringify(tkn), function (err) {});
};
} );
}
exports.TokenManager.prototype.exists = function (id) {
return tokens[md5(id)];
};
exports.TokenManager.prototype.exists = function( id ) {
return tokens[ md5( id ) ];
}
exports.TokenManager.prototype.getToken = function (id, pass) {
try {
return decrypt(tokens[md5(id)], pass);
} catch (e) {
return false;
}
};
exports.TokenManager.prototype.getToken = function( id, pass ) {
try{
return decrypt( tokens[ md5( id ) ], pass );
}catch(e){
return false;
}
}
function encrypt( string, password ) {
var cipher = crypto.createCipher( "aes-256-ctr", password )
var crypted = cipher.update( string, 'utf8', 'hex' )
crypted += cipher.final( 'hex' );
function encrypt(string, password) {
var cipher = crypto.createCipher("aes-256-ctr", password);
var crypted = cipher.update(string, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function decrypt( string, password ) {
var decipher = crypto.createDecipher( "aes-256-ctr", password )
var dec = decipher.update( string, 'hex', 'utf8' )
dec += decipher.final( 'utf8' );
function decrypt(string, password) {
var decipher = crypto.createDecipher("aes-256-ctr", password);
var dec = decipher.update(string, 'hex', 'utf8');
dec += decipher.final('utf8');
return dec;
}
}

63
lib/asd.js Normal file
View File

@@ -0,0 +1,63 @@
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var request = require("superagent");
var defaultOptions = {
cache_tokens: false
};
var Client = (function () {
function Client() {
var options = arguments.length <= 0 || arguments[0] === undefined ? defaultOptions : arguments[0];
var token = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];
_classCallCheck(this, Client);
/*
When created, if a token is specified the Client will
try connecting with it. If the token is incorrect, no
further efforts will be made to connect.
*/
this.options = options;
this.token = token;
this.state = 0;
this.websocket = null;
this.events = new Map();
this.user = null;
/*
State values:
0 - idle
1 - logging in
2 - logged in
3 - ready
4 - disconnected
*/
}
_createClass(Client, [{
key: "login",
//def login
value: function login() {
var email = arguments.length <= 0 || arguments[0] === undefined ? "foo@bar.com" : arguments[0];
var password = arguments.length <= 1 || arguments[1] === undefined ? "pass1234s" : arguments[1];
if (this.state === 0 || this.state === 4) {
this.state = 1;
request.post();
}
}
}, {
key: "ready",
get: function get() {
return this.state === 3;
}
}]);
return Client;
})();

View File

@@ -1,8 +1,11 @@
"use strict";
var List = require("./list.js").List;
exports.Channel = function(name, server, type, id, isPrivate){
exports.Channel = function (name, server, type, id, isPrivate) {
if(!type){ //there's no second argument
if (!type) {
//there's no second argument
var channel = name;
name = channel.name;
server = server;
@@ -17,12 +20,12 @@ exports.Channel = function(name, server, type, id, isPrivate){
this.id = id;
this.isPrivate = isPrivate;
this.messages = new List("id", 5000);
}
};
exports.Channel.equals = function(otherChannel){
if(otherChannel.id === this.id){
exports.Channel.equals = function (otherChannel) {
if (otherChannel.id === this.id) {
return true;
} else {
return false;
}
}
};

View File

@@ -1,15 +1,13 @@
var base = "https://discordapp.com/";
var apibase = base + "api";
"use strict";
exports.API = apibase;
exports.BASE_DOMAIN = "discordapp.com";
exports.BASE = "https://" + exports.BASE_DOMAIN;
exports.WEBSOCKET_HUB = "wss://" + exports.BASE_DOMAIN + "/hub";
exports.WEBSOCKET_HUB = "wss://discordapp.com/hub"
exports.USERS = apibase + "/users";
exports.LOGIN = apibase + "/auth/login";
exports.LOGOUT = apibase + "/auth/logout";
exports.SERVERS = apibase + "/guilds";
exports.CHANNELS = apibase + "/channels";
exports.API = exports.BASE + "/api";
exports.AUTH = exports.API + "/auth";
exports.LOGIN = exports.AUTH + "/login";
exports.LOGIN = exports.AUTH + "/logout";
exports.USERS = exports.API + "/users";
exports.SERVERS = exports.API + "/guilds";
exports.CHANNELS = exports.API + "/channels";

23
lib/index.js Normal file
View File

@@ -0,0 +1,23 @@
"use strict";
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 Invite = require("./lib/invite.js").Invite;
var PMChannel = require("./lib/PMChannel.js").PMChannel;
var WebSocket = require('ws');
var Internal = require("./lib/internal.js").Internal;
var TokenManager = require("./lib/TokenManager.js").TokenManager;
exports.Endpoints = Endpoints;
exports.Server = Server;
exports.Message = Message;
exports.User = User;
exports.Channel = Channel;
exports.List = List;
exports.Invite = Invite;
exports.PMChannel = PMChannel;

View File

@@ -1,5 +1,7 @@
var request = require( "superagent" );
var Endpoints = require( "./endpoints.js" );
"use strict";
var request = require("superagent");
var Endpoints = require("./endpoints.js");
var Internal = {};
@@ -14,264 +16,188 @@ Internal.WebSocket.properties = {
"$referring_domain": ""
};
Internal.XHR.login = function( email, password, callback ) {
Internal.XHR.login = function (email, password, callback) {
request
.post( Endpoints.LOGIN )
.send( {
email: email,
password: password
} )
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body.token );
}
} );
request.post(Endpoints.LOGIN).send({
email: email,
password: password
}).end(function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res.body.token);
}
});
};
}
Internal.XHR.logout = function (token, callback) {
Internal.XHR.logout = function( token, callback ) {
request.post(Endpoints.LOGOUT).end(function (err, res) {
request
.post( Endpoints.LOGOUT )
.end( function( err, res ) {
err ? callback(err) : callback(null);
});
};
err ? callback( err ) : callback( null );
Internal.XHR.createServer = function (token, name, region, callback) {
} );
request.post(Endpoints.SERVERS).set("authorization", token).send({
name: name,
region: region
}).end(function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
}
Internal.XHR.leaveServer = function (token, serverId, callback) {
Internal.XHR.createServer = function( token, name, region, callback ) {
request.del(Endpoints.SERVERS + "/" + serverId).set("authorization", token).end(function (err, res) {
request
.post( Endpoints.SERVERS )
.set( "authorization", token )
.send( {
name: name,
region: region
} )
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
} );
}
err ? callback(err) : callback(null);
});
};
Internal.XHR.leaveServer = function( token, serverId, callback ) {
Internal.XHR.createInvite = function (token, channelId, options, callback) {
request.post(Endpoints.CHANNELS + "/" + channelId + "/invites").set("authorization", token).send(options).end(function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
request
.del( Endpoints.SERVERS + "/" + serverId )
.set( "authorization", token )
.end( function( err, res ) {
Internal.XHR.startPM = function (token, selfID, userID, callback) {
err ? callback( err ) : callback( null );
request.post(Endpoints.USERS + "/" + selfID + "/channels").set("authorization", token).send({
recipient_id: userID
}).end(function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
} );
Internal.XHR.sendMessage = function (token, channelID, messageParameters, callback) {
request.post(Endpoints.CHANNELS + "/" + channelID + "/messages").set("authorization", token).send(messageParameters).end(function (err, res) {
}
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
Internal.XHR.createInvite = function( token, channelId, options, callback ) {
request
.post( Endpoints.CHANNELS + "/" + channelId + "/invites" )
.set( "authorization", token )
.send( options )
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
} )
}
Internal.XHR.sendFile = function (token, channelID, file, fileName, callback) {
request.post(Endpoints.CHANNELS + "/" + channelID + "/messages").set("authorization", token).attach("file", file, fileName).end(function (err, res) {
Internal.XHR.startPM = function( token, selfID, userID, callback ) {
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
request
.post( Endpoints.USERS + "/" + selfID + "/channels" )
.set( "authorization", token )
.send( {
recipient_id: userID
} )
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
} );
Internal.XHR.deleteMessage = function (token, channelID, messageID, callback) {
request.del(Endpoints.CHANNELS + "/" + channelID + "/messages/" + messageID).set("authorization", token).end(function (err) {
err ? callback(err) : callback(null);
});
};
}
Internal.XHR.updateMessage = function (token, channelID, messageID, messageParameters, callback) {
Internal.XHR.sendMessage = function( token, channelID, messageParameters, callback ) {
request
.post( Endpoints.CHANNELS + "/" + channelID + "/messages" )
.set( "authorization", token )
.send( messageParameters )
.end( function( err, res ) {
request.patch(Endpoints.CHANNELS + "/" + channelID + "/messages/" + messageID).set("authorization", token).send(messageParameters).end(function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
Internal.XHR.getChannelLogs = function (token, channelID, amount, callback) {
request.get(Endpoints.CHANNELS + "/" + channelID + "/messages?limit=" + amount).set("authorization", token).end(function (err, res) {
} );
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
}
Internal.XHR.createChannel = function (token, serverID, name, type, callback) {
request.post(Endpoints.SERVERS + "/" + serverID + "/channels").set("authorization", token).send({
name: name,
type: type
}).end(function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
Internal.XHR.sendFile = function( token, channelID, file, fileName, callback ) {
request
.post( Endpoints.CHANNELS + "/" + channelID + "/messages" )
.set( "authorization", token )
.attach("file", file, fileName)
.end( function( err, res ) {
Internal.XHR.deleteChannel = function (token, channelID, callback) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
request.del(Endpoints.CHANNELS + "/" + channelID).set("authorization", token).end(function (err) {
err ? callback(err) : callback(null);
});
};
Internal.XHR.deleteServer = function (token, serverID, callback) {
request.del(Endpoints.SERVERS + "/" + serverID).set("authorization", token).end(function (err) {
err ? callback(err) : callback(null);
});
};
} );
}
Internal.XHR.getChannels = function (token, serverID, callback) {
request.get(Endpoints.SERVERS + "/" + serverID + "/channels").set("authorization", token).end(function (err) {
err ? callback(err) : callback(null);
});
};
Internal.XHR.deleteMessage = function( token, channelID, messageID, callback ) {
request
.del( Endpoints.CHANNELS + "/" + channelID + "/messages/" + messageID )
.set( "authorization", token )
.end( function( err ) {
err ? callback( err ) : callback( null );
} );
}
Internal.XHR.getServer = function (token, serverID, callback) {
Internal.XHR.updateMessage = function( token, channelID, messageID, messageParameters, callback ) {
request.get(Endpoints.SERVERS + "/" + serverID).set("authorization", token).end(function (err, res) {
request
.patch( Endpoints.CHANNELS + "/" + channelID + "/messages/" + messageID )
.set( "authorization", token )
.send( messageParameters )
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
} );
}
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
Internal.XHR.getChannelLogs = function( token, channelID, amount, callback ) {
request
.get( Endpoints.CHANNELS + "/" + channelID + "/messages?limit=" + amount )
.set( "authorization", token )
.end( function( err, res ) {
Internal.XHR.acceptInvite = function (token, inviteID, callback) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
request.post(Endpoints.API + "/invite/" + inviteID).set("authorization", token).end(function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res.body);
}
});
};
} );
}
Internal.XHR.setUsername = function (token, avatar, email, newPassword, password, username, callback) {
Internal.XHR.createChannel = function( token, serverID, name, type, callback ) {
request
.post( Endpoints.SERVERS + "/" + serverID + "/channels" )
.set( "authorization", token )
.send( {
name: name,
type: type
} )
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
} );
}
request.patch(Endpoints.API + "/users/@me").set("authorization", token).send({
avatar: avatar,
email: email,
new_password: newPassword,
password: password,
username: username
}).end(function (err) {
callback(err);
});
};
Internal.XHR.deleteChannel = function( token, channelID, callback ) {
request
.del( Endpoints.CHANNELS + "/" + channelID )
.set( "authorization", token )
.end( function( err ) {
err ? callback( err ) : callback( null );
} );
}
Internal.XHR.deleteServer = function( token, serverID, callback ) {
request
.del( Endpoints.SERVERS + "/" + serverID )
.set( "authorization", token )
.end( function( err ) {
err ? callback( err ) : callback( null );
} );
}
Internal.XHR.getChannels = function( token, serverID, callback ) {
request
.get( Endpoints.SERVERS + "/" + serverID + "/channels" )
.set( "authorization", token )
.end( function( err ) {
err ? callback( err ) : callback( null );
} );
}
Internal.XHR.getServer = function( token, serverID, callback ) {
request
.get( Endpoints.SERVERS + "/" + serverID )
.set( "authorization", token )
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
} );
}
Internal.XHR.acceptInvite = function( token, inviteID, callback ) {
request
.post( Endpoints.API + "/invite/" + inviteID )
.set( "authorization", token )
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body )
}
} );
}
Internal.XHR.setUsername = function( token, avatar, email, newPassword, password, username, callback ) {
request
.patch( Endpoints.API + "/users/@me" )
.set( "authorization", token )
.send( {
avatar: avatar,
email: email,
new_password: newPassword,
password: password,
username: username
} )
.end( function( err ) {
callback( err );
} );
}
exports.Internal = Internal;
exports.Internal = Internal;

View File

@@ -1,6 +1,8 @@
"use strict";
var User = require("./user.js").User;
exports.Invite = function(json){
exports.Invite = function (json) {
this.max_age = json.max_age;
this.code = json.code;
@@ -13,9 +15,9 @@ exports.Invite = function(json){
this.inviter = new User(json.inviter);
this.xkcdpass = json.xkcdpass;
this.channel = json.channel;
}
};
exports.Invite.prototype.generateInviteURL = function(xkcd){
var code = (xkcd ? this.xkcdpass : this.code);
return "https://discord.gg/"+code;
}
exports.Invite.prototype.generateInviteURL = function (xkcd) {
var code = xkcd ? this.xkcdpass : this.code;
return "https://discord.gg/" + code;
};

View File

@@ -4,27 +4,29 @@
* when created. Generally "ID"
* @class List
*/
exports.List = function( discriminator, cap ) {
"use strict";
exports.List = function (discriminator, cap) {
/**
* What to use to distringuish duplicates
* @attribute discriminator
* @type {String}
*/
* What to use to distringuish duplicates
* @attribute discriminator
* @type {String}
*/
this.discriminator = discriminator;
/**
* The maximum amount of elements allowed in the list.
* @default Infinity
* @attribute cap
* @type {Number}
*/
* The maximum amount of elements allowed in the list.
* @default Infinity
* @attribute cap
* @type {Number}
*/
this.cap = cap || Number.MAX_SAFE_INTEGER;
/**
* The Array version of the List.
* @type {Array}
* @attribute contents
*/
* The Array version of the List.
* @type {Array}
* @attribute contents
*/
this.contents = [];
}
};
/**
* Adds an element to the list if it isn't already there.
@@ -34,40 +36,59 @@ exports.List = function( discriminator, cap ) {
* List.add( obj );
* List.add( [ obj, obj, obj ] );
*/
exports.List.prototype.add = function( child ) {
exports.List.prototype.add = function (child) {
var self = this;
if ( child.constructor === Array ) {
if (child.constructor === Array) {
children = child;
for ( child of children ) {
addChild( child );
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
child = _step.value;
addChild(child);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"]) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
} else {
addChild( child );
addChild(child);
}
function addChild( child ) {
function addChild(child) {
if ( self.length() > self.cap ) {
self.splice( 0, 1 );
if (self.length() > self.cap) {
self.splice(0, 1);
}
if ( self.filter( self.discriminator, child[ self.discriminator ] ).length() === 0 )
self.contents.push( child );
if (self.filter(self.discriminator, child[self.discriminator]).length() === 0) self.contents.push(child);
}
}
};
/**
* Returns the length of the List
* @method length
* @return {Number}
*/
exports.List.prototype.length = function() {
exports.List.prototype.length = function () {
return this.contents.length;
}
};
/**
* Gets the index of an element in the List or defaults to false
@@ -75,30 +96,28 @@ exports.List.prototype.length = function() {
* @return {Number/Boolean} The index if the object is in the list, or false.
* @method getIndex
*/
exports.List.prototype.getIndex = function( object ) {
exports.List.prototype.getIndex = function (object) {
var index = false;
for ( elementIndex in this.contents ) {
var element = this.contents[ elementIndex ];
if ( element[ this.discriminator ] == object[ this.discriminator ] ) {
for (elementIndex in this.contents) {
var element = this.contents[elementIndex];
if (element[this.discriminator] == object[this.discriminator]) {
return elementIndex;
}
}
return index;
}
};
/**
* Removes an element at the specified index
* @param {Number} index
* @method removeIndex
*/
exports.List.prototype.removeIndex = function( index ) {
this.contents.splice( index, 1 );
}
exports.List.prototype.removeIndex = function (index) {
this.contents.splice(index, 1);
};
/**
* Removes an element from the list
@@ -106,18 +125,18 @@ exports.List.prototype.removeIndex = function( index ) {
* @method removeElement
* @return {Boolean} whether the operation was successful or not.
*/
exports.List.prototype.removeElement = function( child ) {
exports.List.prototype.removeElement = function (child) {
for ( _element in this.contents ) {
var element = this.contents[ _element ];
if ( child[ this.discriminator ] == element[ this.discriminator ] ) {
this.removeIndex( _element, 1 );
for (_element in this.contents) {
var element = this.contents[_element];
if (child[this.discriminator] == element[this.discriminator]) {
this.removeIndex(_element, 1);
return true;
}
}
return false;
}
};
/**
* Replaces an element in the list with a specified element
@@ -126,123 +145,206 @@ exports.List.prototype.removeElement = function( child ) {
* @param {Object} newElement New Element
* @return {Boolean} whether the operation was successful or not.
*/
exports.List.prototype.updateElement = function( child, newChild ) {
exports.List.prototype.updateElement = function (child, newChild) {
for ( _element in this.contents ) {
var element = this.contents[ _element ];
if ( child[ this.discriminator ] == element[ this.discriminator ] ) {
this.contents[ _element ] = newChild;
for (_element in this.contents) {
var element = this.contents[_element];
if (child[this.discriminator] == element[this.discriminator]) {
this.contents[_element] = newChild;
return true;
}
}
return false;
};
}
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 );
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = this.contents[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
item = _step2.value;
var itemList = item[whereList];
concatList.add(itemList.contents);
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2["return"]) {
_iterator2["return"]();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return concatList;
}
};
exports.List.prototype.filter = function( key, value, onlyOne, caseInsen ) {
exports.List.prototype.filter = function (key, value, onlyOne, caseInsen) {
var results = [];
value = change( value );
value = change(value);
for ( index in this.contents ) {
var child = this.contents[ index ];
if ( change( child[ key ] ) == value ) {
if ( onlyOne ) {
for (index in this.contents) {
var child = this.contents[index];
if (change(child[key]) == value) {
if (onlyOne) {
return child;
} else {
results.push( child );
results.push(child);
}
}
}
function change( val ) {
if ( caseInsen ) {
function change(val) {
if (caseInsen) {
val = val.toUpperCase();
}
return val;
}
if ( onlyOne ) {
if (onlyOne) {
return false;
}
var retList = new exports.List( this.discriminator );
var retList = new exports.List(this.discriminator);
retList.contents = results;
return retList;
}
};
exports.List.prototype.getValues = function( key ){
exports.List.prototype.getValues = function (key) {
var valList = [];
for( child of this.contents){
valList.push( child[key] );
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = this.contents[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
child = _step3.value;
valList.push(child[key]);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3["return"]) {
_iterator3["return"]();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return valList;
};
}
exports.List.prototype.deepFilter = function( keys, value, onlyOne, caseInsen ) {
exports.List.prototype.deepFilter = function (keys, value, onlyOne, caseInsen) {
var results = [];
value = change( value );
value = change(value);
for ( index in this.contents ) {
var child = this.contents[ index ];
for (index in this.contents) {
var child = this.contents[index];
var buffer = child;
for ( key of keys ) {
if(buffer instanceof exports.List){
buffer = buffer.contents;
}
if(buffer instanceof Array){
for(elem of buffer){
if( change(elem[key]) == value ){
buffer = elem;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = keys[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
key = _step4.value;
if (buffer instanceof exports.List) {
buffer = buffer.contents;
}
if (buffer instanceof Array) {
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = buffer[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
elem = _step5.value;
if (change(elem[key]) == value) {
buffer = elem;
}
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5["return"]) {
_iterator5["return"]();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
}
buffer = buffer[key];
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4["return"]) {
_iterator4["return"]();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
buffer = buffer[ key ];
}
if ( change( buffer ) == value ) {
if ( onlyOne ) {
if (change(buffer) == value) {
if (onlyOne) {
return child;
} else {
results.push( child );
results.push(child);
}
}
}
function change( val ) {
if ( caseInsen ) {
function change(val) {
if (caseInsen) {
val = val.toUpperCase();
}
return val;
}
if ( onlyOne ) {
if (onlyOne) {
return false;
}
var retList = new exports.List( this.discriminator );
var retList = new exports.List(this.discriminator);
retList.contents = results;
return retList;
}
};

View File

@@ -1,10 +1,12 @@
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
var PMChannel = require( "./PMChannel.js" ).PMChannel;
"use strict";
exports.Message = function( time, author, content, channel, id, mentions, everyoneMentioned, embeds ) {
var User = require("./user.js").User;
var List = require("./list.js").List;
var PMChannel = require("./PMChannel.js").PMChannel;
if ( !content ) {
exports.Message = function (time, author, content, channel, id, mentions, everyoneMentioned, embeds) {
if (!content) {
message = time;
channel = author;
time = message.timestamp;
@@ -16,24 +18,24 @@ exports.Message = function( time, author, content, channel, id, mentions, everyo
embeds = message.embeds;
}
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.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 = new List( "id" );
this.mentions = new List("id");
this.everyoneMentioned = everyoneMentioned;
this.embeds = embeds;
for ( x in mentions ) {
var _mention = mentions[ x ];
this.mentions.add( new User( _mention ) );
for (x in mentions) {
var _mention = mentions[x];
this.mentions.add(new User(_mention));
}
}
};
exports.Message.prototype.isPM = function() {
return ( this.channel instanceof PMChannel );
}
exports.Message.prototype.isPM = function () {
return this.channel instanceof PMChannel;
};
exports.Message.prototype.isMentioned = function( user ) {
return ( this.mentions.filter( "id", user.id ).length > 0 );
}
exports.Message.prototype.isMentioned = function (user) {
return this.mentions.filter("id", user.id).length > 0;
};

View File

@@ -1,106 +1,104 @@
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
"use strict";
var User = require("./user.js").User;
var List = require("./list.js").List;
/**
* A wrapper for Server information, contains channels and users too. Developers should not instantiate the class, instead they should
* manipulate Server objects given to them.
* @class Server
* @param {String} region The region of the server
*/
exports.Server = function( region, ownerID, name, id, members, icon, afkTimeout, afkChannelId ) {
exports.Server = function (region, ownerID, name, id, members, icon, afkTimeout, afkChannelId) {
/**
* The region of the Server
* @type {String}
* @attribute region
*/
* The region of the Server
* @type {String}
* @attribute region
*/
this.region = region;
/**
* The ID of the owner of the Server (not a User!)
* @type {String}
* @attribute ownerID
*/
* The ID of the owner of the Server (not a User!)
* @type {String}
* @attribute ownerID
*/
this.ownerID = ownerID;
/**
* The name of the Server
* @type {String}
* @attribute name
*/
* The name of the Server
* @type {String}
* @attribute name
*/
this.name = name;
/**
* The ID of the Server
* @type {String}
* @attribute id
*/
* The ID of the Server
* @type {String}
* @attribute id
*/
this.id = id;
/**
* List containing members of the Server
* @param {List}
* @attribute members
*/
this.members = new List( "id" );
* List containing members of the Server
* @param {List}
* @attribute members
*/
this.members = new List("id");
/**
* List containing channelss of the Server
* @param {List}
* @attribute channels
*/
this.channels = new List( "id" );
* List containing channelss of the Server
* @param {List}
* @attribute channels
*/
this.channels = new List("id");
/**
* ID of the Icon of the Server
* @param {String}
* @attribute icon
*/
* ID of the Icon of the Server
* @param {String}
* @attribute icon
*/
this.icon = icon;
/**
* The amount of seconds that should pass before the user is
* @type {Number}
* @attribute afkTimeout
*/
* The amount of seconds that should pass before the user is
* @type {Number}
* @attribute afkTimeout
*/
this.afkTimeout = afkTimeout;
/**
* The ID of the AFK Channel, evaluates to false if doesn't exist.
* @type {String}
* @attribute afkChannelId
*/
* The ID of the AFK Channel, evaluates to false if doesn't exist.
* @type {String}
* @attribute afkChannelId
*/
this.afkChannelId = afkChannelId;
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));
}
}
};
/**
* Returns a valid URL pointing towards the server's icon if it has one.
* @method getIconURL
* @return {String/Boolean} If there is an icon, a URL is returned. If not, false is returned.
*/
exports.Server.prototype.getIconURL = function(){
if(!this.icon)
return false;
return "https://discordapp.com/api/guilds/"+this.id+"/icons/"+this.icon+".jpg";
}
exports.Server.prototype.getIconURL = function () {
if (!this.icon) return false;
return "https://discordapp.com/api/guilds/" + this.id + "/icons/" + this.icon + ".jpg";
};
/**
* Returns the AFK Channel if a server has one
* @method getAFKChannel
* @return {Channel/Boolean} If there is an AFK Channel, a Channel is returned. If not, false is returned.
*/
exports.Server.prototype.getAFKChannel = function(){
exports.Server.prototype.getAFKChannel = function () {
if(!this.afkChannelId)
return false;
if (!this.afkChannelId) return false;
return this.channels.filter("id", this.afkChannelId, true);
}
};
/**
* Returns the #general channel of the server.
* @method getDefaultChannel
* @return {Channel} Returns the #general channel of the Server.
*/
exports.Server.prototype.getDefaultChannel = function() {
exports.Server.prototype.getDefaultChannel = function () {
return this.channels.filter( "name", "general", true );
}
return this.channels.filter("name", "general", true);
};

View File

@@ -1,6 +1,9 @@
exports.User = function( username, id, discriminator, avatar ) {
"use strict";
if ( !id ) { //there's no second argument
exports.User = function (username, id, discriminator, avatar) {
if (!id) {
//there's no second argument
var user = username;
username = user.username;
id = user.id;
@@ -12,26 +15,23 @@ exports.User = function( username, id, discriminator, avatar ) {
this.discriminator = discriminator;
this.id = id;
this.avatar = avatar;
}
};
exports.User.prototype.getAvatarURL = function() {
if ( !this.avatar )
return false;
exports.User.prototype.getAvatarURL = function () {
if (!this.avatar) return false;
return "https://discordapp.com/api/users/" + this.id + "/avatars/" + this.avatar + ".jpg";
}
};
exports.User.prototype.mention = function() {
exports.User.prototype.mention = function () {
return "<@" + this.id + ">";
}
};
exports.User.prototype.equals = function( otherUser ) {
exports.User.prototype.equals = function (otherUser) {
return otherUser.id === this.id;
};
}
exports.User.prototype.equalsStrict = function (otherUser) {
exports.User.prototype.equalsStrict = function( otherUser ) {
return ( this.username === otherUser.username && this.discriminator === otherUser.discriminator && this.id === otherUser.id && this.avatar === otherUser.avatar );
}
return this.username === otherUser.username && this.discriminator === otherUser.discriminator && this.id === otherUser.id && this.avatar === otherUser.avatar;
};