diff --git a/README.md b/README.md index b80c65f0d..ed50cc5c4 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,12 @@ Discord.js is a node module that allows you to interface with the [Discord](http The aim of this API is to make it *really* simple to start developing your bots. This API has server, channel and user tracking, as well as tools to make identification really simple. -New update features **big speed boosts** (everything cached and sorted with around 1 second of calling the login function) upon connection and allows editing of messages! +The new rewrite of the API (version 3+) is written in ECMAScript 6 and compiled down to EC5 using Babel. It allows the code to be written faster and more consistently, and take use of new features. -## PLEASE BE AWARE - I'm currently rewriting this _entire_ module in ECMAScript 6 and then using Babel to compile it so it works in node. That means for a few days I won't be adding features, but after that features will come out faster and better than ever! -back to +## New update break your code? Read why [here](https://github.com/discord-js/discord.js/wiki#why-did-my-code-break-with-the-new-update). **[Find the website here.](http://discord-js.github.io)** - **[For more information, click here.](https://github.com/hydrabolt/discord.js/wiki)** ### This module is still in alpha - especially the newer versions! diff --git a/lib/TokenManager.js b/lib/TokenManager.js deleted file mode 100644 index 22732a462..000000000 --- a/lib/TokenManager.js +++ /dev/null @@ -1,67 +0,0 @@ -"use strict"; - -var fs = require("fs"); -var crypto = require("crypto"); -var md5 = require("md5"); - -var tokens = {}; - -exports.TokenManager = function (folder, file) { - - this.path = folder + file; - - var self = this; - - try { - var fd = fs.openSync(self.path, "wx"); - self.writeTokens(); - } catch (e) { - self.readTokens(); - } -}; - -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.writeTokens = function () { - var tkn = {}; - for (token in tokens) { - tkn[token] = encrypt(tokens[token], token); - } - fs.writeFile(this.path, JSON.stringify(tkn), function (err) {}); -}; - -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; - } -}; - -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'); - return dec; -} \ No newline at end of file diff --git a/lib/asd.js b/lib/asd.js deleted file mode 100644 index c74b5e209..000000000 --- a/lib/asd.js +++ /dev/null @@ -1,63 +0,0 @@ -"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; -})(); \ No newline at end of file diff --git a/lib/list.js b/lib/list.js deleted file mode 100644 index df07cad86..000000000 --- a/lib/list.js +++ /dev/null @@ -1,350 +0,0 @@ -/** - * Similar to a Java set. Contains no duplicate elements and includes filter - * functions. Discriminates between elements based on a discriminator passed - * when created. Generally "ID" - * @class List - */ -"use strict"; - -exports.List = function (discriminator, cap) { - /** - * 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} - */ - this.cap = cap || Number.MAX_SAFE_INTEGER; - /** - * The Array version of the List. - * @type {Array} - * @attribute contents - */ - this.contents = []; -}; - -/** - * Adds an element to the list if it isn't already there. - * @method add - * @param {Object/Array} element The element(s) to add - * @example - * List.add( obj ); - * List.add( [ obj, obj, obj ] ); - */ -exports.List.prototype.add = function (child) { - - var self = this; - - if (child.constructor === Array) { - - children = 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); - } - - function addChild(child) { - - if (self.length() > self.cap) { - self.splice(0, 1); - } - - 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 () { - return this.contents.length; -}; - -/** - * Gets the index of an element in the List or defaults to false - * @param {Object} object The element we want to get the index of - * @return {Number/Boolean} The index if the object is in the list, or false. - * @method getIndex - */ -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]) { - 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); -}; - -/** - * Removes an element from the list - * @param {Object} element - * @method removeElement - * @return {Boolean} whether the operation was successful or not. - */ -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); - return true; - } - } - - return false; -}; - -/** - * Replaces an element in the list with a specified element - * @method updateElement - * @param {Object} element Element to update. - * @param {Object} newElement New Element - * @return {Boolean} whether the operation was successful or not. - */ -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; - return true; - } - } - - return false; -}; - -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 _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) { - - var results = []; - - value = change(value); - - for (index in this.contents) { - var child = this.contents[index]; - if (change(child[key]) == value) { - if (onlyOne) { - return child; - } else { - results.push(child); - } - } - } - - function change(val) { - if (caseInsen) { - val = val.toUpperCase(); - } - return val; - } - - if (onlyOne) { - return false; - } - - var retList = new exports.List(this.discriminator); - retList.contents = results; - - return retList; -}; - -exports.List.prototype.getValues = function (key) { - - var valList = []; - 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) { - - var results = []; - - value = change(value); - - for (index in this.contents) { - var child = this.contents[index]; - var buffer = child; - - 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; - } - } - } - - if (change(buffer) == value) { - if (onlyOne) { - return child; - } else { - results.push(child); - } - } - } - - function change(val) { - if (caseInsen) { - val = val.toUpperCase(); - } - return val; - } - - if (onlyOne) { - return false; - } - - var retList = new exports.List(this.discriminator); - retList.contents = results; - - return retList; -}; \ No newline at end of file diff --git a/package.json b/package.json index 1b8a533f6..f832c5cac 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "discord.js", - "version": "2.7.2", + "version": "3", "description": "A way to interface with the Discord API", - "main": "index.js", + "main": "./lib/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -25,7 +25,6 @@ }, "homepage": "https://github.com/discord-js/discord.js#readme", "dependencies": { - "md5": "^2.0.0", "superagent": "^1.3.0", "ws": "^0.7.2" } diff --git a/src/TokenManager.js b/src/TokenManager.js deleted file mode 100644 index ceeed9ded..000000000 --- a/src/TokenManager.js +++ /dev/null @@ -1,68 +0,0 @@ -var fs = require( "fs" ); -var crypto = require( "crypto" ); -var md5 = require( "md5" ); - -var tokens = {}; - -exports.TokenManager = function( folder, file ) { - - this.path = folder + file; - - var self = this; - - try { - var fd = fs.openSync( self.path, "wx" ); - self.writeTokens(); - } catch ( e ) { - self.readTokens(); - } - -} - -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.writeTokens = function() { - var tkn = {}; - for ( token in tokens ) { - tkn[ token ] = encrypt( tokens[ token ], token ); - } - fs.writeFile( this.path, JSON.stringify( tkn ), function( err ) { - - } ); -} - -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; - } -} - -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' ); - return dec; -}