rewriting... woo

This commit is contained in:
hydrabolt
2015-08-24 17:07:41 +01:00
parent 344f8d73a4
commit 1f77ed226a
10 changed files with 614 additions and 131 deletions

1
.gitignore vendored
View File

@@ -32,3 +32,4 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
test/auth.json

View File

@@ -5,6 +5,9 @@ The aim of this API is to make it *really* simple to start developing your bots.
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!
## 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
**[Find the website here.](http://discord-js.github.io)**

View File

@@ -7,6 +7,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var Endpoints = require("./Endpoints.js");
var User = require("./User.js");
var Server = require("./Server.js");
var Channel = require("./Channel.js");
//node modules
var request = require("superagent");
@@ -44,30 +46,65 @@ var Client = (function () {
4 - disconnected
*/
this.userCache = new Map();
this.channelCache = new Map();
this.serverCache = new Map();
this.userCache = [];
this.channelCache = [];
this.serverCache = [];
}
_createClass(Client, [{
key: "debug",
key: "sendPacket",
value: function sendPacket(JSONObject) {
if (this.websocket.readyState === 1) {
this.websocket.send(JSON.stringify(JSONObject));
}
}
//def debug
}, {
key: "debug",
value: function debug(message) {
console.log(message);
}
}, {
key: "on",
value: function on(event, fn) {
this.events.set(event, fn);
}
}, {
key: "off",
value: function off(event, fn) {
this.events["delete"](event);
}
}, {
key: "keepAlive",
value: function keepAlive() {
this.debug("keep alive triggered");
this.sendPacket({
op: 1,
d: Date.now()
});
}
//def trigger
}, {
key: "trigger",
value: function trigger(event) {}
value: function trigger(event) {
var args = [];
for (var arg in arguments) {
args.push(arguments[arg]);
}
var evt = this.events.get(event);
if (evt) {
evt.apply(this, args.slice(1));
}
}
//def login
}, {
key: "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];
var password = arguments.length <= 1 || arguments[1] === undefined ? "pass1234" : arguments[1];
var callback = arguments.length <= 2 || arguments[2] === undefined ? function () {} : arguments[2];
var self = this;
@@ -141,6 +178,40 @@ var Client = (function () {
self.user = self.addUser(data.user);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = data.guilds[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _server = _step.value;
self.addServer(_server);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"]) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
self.trigger("ready");
self.debug("cached " + self.serverCache.length + " servers, " + self.channelCache.length + " channels and " + self.userCache.length + " users.");
console.log(self.channelCache[0]);
setInterval(function () {
self.keepAlive.apply(self);
}, data.heartbeat_interval);
break;
default:
self.debug("received unknown packet");
@@ -155,10 +226,134 @@ var Client = (function () {
}, {
key: "addUser",
value: function addUser(data) {
if (!this.userCache.has(data.id)) {
this.userCache.set(data.id, new User(data));
if (!this.getUser("id", data.id)) {
this.userCache.push(new User(data));
}
return this.userCache.get(data.id);
return this.getUser("id", data.id);
}
//def addChannel
}, {
key: "addChannel",
value: function addChannel(data, serverId) {
if (!this.getChannel("id", data.id)) {
this.channelCache.push(new Channel(data, this.getServer("id", serverId)));
}
return this.getChannel("id", data.id);
}
//def addServer
}, {
key: "addServer",
value: function addServer(data) {
if (!this.getServer("id", data.id)) {
this.serverCache.push(new Server(data, this));
}
return this.getServer("id", data.id);
}
//def getUser
}, {
key: "getUser",
value: function getUser(key, value) {
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = this.userCache[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var user = _step2.value;
if (user[key] === value) {
return user;
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2["return"]) {
_iterator2["return"]();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return null;
}
//def getChannel
}, {
key: "getChannel",
value: function getChannel(key, value) {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = this.channelCache[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var channel = _step3.value;
if (channel[key] === value) {
return channel;
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3["return"]) {
_iterator3["return"]();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return null;
}
//def getServer
}, {
key: "getServer",
value: function getServer() {
var key = arguments.length <= 0 || arguments[0] === undefined ? "id" : arguments[0];
var value = arguments.length <= 1 || arguments[1] === undefined ? "abc123" : arguments[1];
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = this.serverCache[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var server = _step4.value;
if (server[key] === value) {
return server;
}
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4["return"]) {
_iterator4["return"]();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
return null;
}
//def trySendConnData
@@ -192,6 +387,21 @@ var Client = (function () {
get: function get() {
return this.state === 3;
}
}, {
key: "servers",
get: function get() {
return this.serverCache;
}
}, {
key: "channels",
get: function get() {
return this.channelCache;
}
}, {
key: "users",
get: function get() {
return this.userCache;
}
}]);
return Client;

View File

@@ -1,31 +1,33 @@
"use strict";
var List = require("./list.js").List;
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; }; })();
exports.Channel = function (name, server, type, id, isPrivate) {
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
if (!type) {
//there's no second argument
var channel = name;
name = channel.name;
server = server;
type = channel.type;
id = channel.id;
isPrivate = channel.is_private;
var Channel = (function () {
function Channel(data, server) {
_classCallCheck(this, Channel);
this.server = server;
this.name = data.name;
this.type = data.type;
this.id = data.id;
//this.isPrivate = isPrivate; //not sure about the implementation of this...
}
this.name = name;
this.server = server;
this.type = type;
this.id = id;
this.isPrivate = isPrivate;
this.messages = new List("id", 5000);
};
_createClass(Channel, [{
key: "equals",
value: function equals(object) {
return object.id === this.id;
}
}, {
key: "client",
get: function get() {
return this.server.client;
}
}]);
exports.Channel.equals = function (otherChannel) {
if (otherChannel.id === this.id) {
return true;
} else {
return false;
}
};
return Channel;
})();
module.exports = Channel;

View File

@@ -1,37 +1,172 @@
"use strict";
var User = require("./user.js").User;
var List = require("./list.js").List;
exports.Server = function (data) {
this.region = data.region;
this.ownerID = data.owner_id;
this.name = data.name;
this.id = data.id;
this.members = new Map();
this.channels = new Map();
this.icon = data.icon;
this.afkTimeout = afkTimeout;
this.afkChannelId = afkChannelId;
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; }; })();
for (var x in members) {
var member = members[x].user;
this.members.add(new User(member));
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Server = (function () {
function Server(data, client) {
_classCallCheck(this, Server);
this.client = client;
this.region = data.region;
this.ownerID = data.owner_id;
this.name = data.name;
this.id = data.id;
this.members = new Set();
this.channels = new Set();
this.icon = data.icon;
this.afkTimeout = data.afk_timeout;
this.afkChannelId = data.afk_channel_id;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = data.members[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var member = _step.value;
// first we cache the user in our Discord Client,
// then we add it to our list. This way when we
// get a user from this server's member list,
// it will be identical (unless an async change occurred)
// to the client's cache.
this.members.add(client.addUser(member.user));
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"]) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = data.channels[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var channel = _step2.value;
this.channels.add(client.addChannel(channel, this.id));
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2["return"]) {
_iterator2["return"]();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
};
exports.Server.prototype.getIconURL = function () {
if (!this.icon) return false;
return "https://discordapp.com/api/guilds/" + this.id + "/icons/" + this.icon + ".jpg";
};
_createClass(Server, [{
key: "getChannel",
exports.Server.prototype.getAFKChannel = function () {
// get/set
value: function getChannel(key, value) {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
if (!this.afkChannelId) return false;
try {
for (var _iterator3 = this.channels[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var channel = _step3.value;
return this.channels.filter("id", this.afkChannelId, true);
};
if (channel[key] === value) {
return channel;
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3["return"]) {
_iterator3["return"]();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
exports.Server.prototype.getDefaultChannel = function () {
return null;
}
}, {
key: "getMember",
value: function getMember(key, value) {
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
return this.channels.filter("name", "general", true);
};
try {
for (var _iterator4 = this.members[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var member = _step4.value;
if (member[key] === value) {
return member;
}
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4["return"]) {
_iterator4["return"]();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
return null;
}
}, {
key: "iconURL",
get: function get() {
if (!this.icon) return null;
return "https://discordapp.com/api/guilds/" + this.id + "/icons/" + this.icon + ".jpg";
}
}, {
key: "afkChannel",
get: function get() {
if (!this.afkChannelId) return false;
return this.getChannel("id", this.afkChannelId);
}
}, {
key: "defaultChannel",
get: function get() {
return this.getChannel("name", "general");
}
}, {
key: "owner",
get: function get() {
return this.client.getUser("id", this.ownerID);
}
}]);
return Server;
})();
module.exports = Server;

View File

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

View File

@@ -1,6 +1,8 @@
//discord.js modules
var Endpoints = require("./Endpoints.js");
var User = require("./User.js");
var Server = require("./Server.js");
var Channel = require("./Channel.js");
//node modules
var request = require("superagent");
@@ -34,28 +36,68 @@ class Client {
4 - disconnected
*/
this.userCache = new Map();
this.channelCache = new Map();
this.serverCache = new Map();
this.userCache = [];
this.channelCache = [];
this.serverCache = [];
}
get ready() {
return this.state === 3;
}
get servers() {
return this.serverCache;
}
get channels() {
return this.channelCache;
}
get users() {
return this.userCache;
}
sendPacket(JSONObject){
if(this.websocket.readyState === 1){
this.websocket.send(JSON.stringify(JSONObject));
}
}
//def debug
debug(message) {
console.log(message);
}
on(event, fn){
this.events.set(event, fn);
}
off(event, fn){
this.events.delete(event);
}
keepAlive(){
this.debug("keep alive triggered");
this.sendPacket({
op: 1,
d: Date.now()
});
}
//def trigger
trigger(event) {
var args = [];
for(var arg in arguments){
args.push(arguments[arg]);
}
var evt = this.events.get(event);
if(evt){
evt.apply(this, args.slice(1));
}
}
//def login
login(email = "foo@bar.com", password = "pass1234s", callback = function () { }) {
login(email = "foo@bar.com", password = "pass1234", callback = function () { }) {
var self = this;
@@ -131,6 +173,19 @@ class Client {
self.user = self.addUser( data.user );
for(var _server of data.guilds){
self.addServer(_server);
}
self.trigger("ready");
self.debug(`cached ${self.serverCache.length} servers, ${self.channelCache.length} channels and ${self.userCache.length} users.`);
console.log(self.channelCache[0]);
setInterval(function () {
self.keepAlive.apply(self);
}, data.heartbeat_interval);
break;
default:
@@ -146,10 +201,56 @@ class Client {
//def addUser
addUser(data) {
if (!this.userCache.has(data.id)){
this.userCache.set(data.id, new User(data));
if (!this.getUser("id", data.id)){
this.userCache.push(new User(data));
}
return this.userCache.get(data.id);
return this.getUser("id", data.id);
}
//def addChannel
addChannel(data, serverId) {
if (!this.getChannel("id", data.id)){
this.channelCache.push(new Channel(data, this.getServer("id", serverId)));
}
return this.getChannel("id", data.id);
}
//def addServer
addServer(data){
if(!this.getServer("id", data.id)){
this.serverCache.push(new Server(data, this));
}
return this.getServer("id", data.id);
}
//def getUser
getUser(key, value){
for(var user of this.userCache){
if(user[key] === value){
return user;
}
}
return null;
}
//def getChannel
getChannel(key, value){
for(var channel of this.channelCache){
if(channel[key] === value){
return channel;
}
}
return null;
}
//def getServer
getServer(key = "id", value = "abc123"){
for(var server of this.serverCache){
if(server[key] === value){
return server;
}
}
return null;
}
//def trySendConnData

View File

@@ -1,28 +1,21 @@
var List = require("./list.js").List;
class Channel {
exports.Channel = function(name, server, type, id, isPrivate){
if(!type){ //there's no second argument
var channel = name;
name = channel.name;
server = server;
type = channel.type;
id = channel.id;
isPrivate = channel.is_private;
constructor(data, server) {
this.server = server;
this.name = data.name;
this.type = data.type;
this.id = data.id;
//this.isPrivate = isPrivate; //not sure about the implementation of this...
}
get client() {
return this.server.client;
}
equals(object) {
return object.id === this.id;
}
this.name = name;
this.server = server;
this.type = type;
this.id = id;
this.isPrivate = isPrivate;
this.messages = new List("id", 5000);
}
exports.Channel.equals = function(otherChannel){
if(otherChannel.id === this.id){
return true;
} else {
return false;
}
}
module.exports = Channel;

View File

@@ -1,39 +1,73 @@
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
exports.Server = function( data ) {
this.region = data.region;
this.ownerID = data.owner_id;
this.name = data.name;
this.id = data.id;
this.members = new Map();
this.channels = new Map();
this.icon = data.icon;
this.afkTimeout = data.afk_timeout;
this.afkChannelId = data.afk_channel_id;
class Server {
constructor(data, client) {
this.client = client;
this.region = data.region;
this.ownerID = data.owner_id;
this.name = data.name;
this.id = data.id;
this.members = new Set();
this.channels = new Set();
this.icon = data.icon;
this.afkTimeout = data.afk_timeout;
this.afkChannelId = data.afk_channel_id;
for ( var x in members ) {
var member = members[ x ].user;
this.members.add( new User( member ) );
for (var member of data.members) {
// first we cache the user in our Discord Client,
// then we add it to our list. This way when we
// get a user from this server's member list,
// it will be identical (unless an async change occurred)
// to the client's cache.
this.members.add(client.addUser(member.user));
}
for (var channel of data.channels) {
this.channels.add(client.addChannel(channel, this.id));
}
}
get iconURL() {
if (!this.icon)
return null;
return `https://discordapp.com/api/guilds/${this.id}/icons/${this.icon}.jpg`;
}
get afkChannel() {
if (!this.afkChannelId)
return false;
return this.getChannel("id", this.afkChannelId);
}
get defaultChannel() {
return this.getChannel("name", "general");
}
get owner() {
return this.client.getUser("id", this.ownerID);
}
// get/set
getChannel(key, value) {
for (var channel of this.channels) {
if (channel[key] === value) {
return channel;
}
}
return null;
}
getMember(key, value){
for (var member of this.members) {
if (member[key] === value) {
return member;
}
}
return null;
}
}
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.getAFKChannel = function(){
if(!this.afkChannelId)
return false;
return this.channels.filter("id", this.afkChannelId, true);
}
exports.Server.prototype.getDefaultChannel = function() {
return this.channels.filter( "name", "general", true );
}
module.exports = Server;

View File

@@ -1,7 +1,11 @@
var Discord = require("../lib/index.js");
var Auth = require("./auth.json");
var mybot = new Discord.Client();
mybot.login("riftes@outlook.com", "hydrabotsecure", function(err, res){
console.log(res);
});
mybot.login(Auth.email, Auth.password, function(err, res){
});
mybot.on("ready", function(){
console.log("Ready!");
})