Finished most of structure

This commit is contained in:
hydrabolt
2015-10-31 21:56:13 +00:00
parent c5e5ab54db
commit 5ccaca915b
23 changed files with 323 additions and 70 deletions

View File

@@ -44,7 +44,7 @@ var Client = (function (_EventEmitter) {
};
/*
def logout
*/
return Client;

View File

@@ -18,7 +18,8 @@ var User = require("../Structures/User.js"),
TextChannel = require("../Structures/TextChannel.js"),
VoiceChannel = require("../Structures/VoiceChannel.js"),
PMChannel = require("../Structures/PMChannel.js"),
Server = require("../Structures/Server.js");
Server = require("../Structures/Server.js"),
Message = require("../Structures/Message.js");
var zlib;
@@ -41,6 +42,8 @@ var InternalClient = (function () {
this.private_channels = new Cache();
}
// def login
InternalClient.prototype.login = function login(email, password) {
var self = this;
var client = self.client;
@@ -60,6 +63,8 @@ var InternalClient = (function () {
var token = res.body.token;
self.state = ConnectionState.LOGGED_IN;
self.token = token;
self.email = email;
self.password = password;
self.getGateway().then(function (url) {
@@ -78,6 +83,37 @@ var InternalClient = (function () {
});
};
InternalClient.prototype.logout = function logout() {
var _this = this;
var self = this;
return new Promise(function (resolve, reject) {
if (self.state === ConnectionState.DISCONNECTED || self.state === ConnectionState.IDLE) {
reject(new Error("Client is not logged in!"));
return;
}
request.post(Endpoints.LOGOUT).set("authorization", self.token).end(function (err, res) {
if (err) {
reject(new Error(err.response.text));
} else {
if (_this.websocket) {
_this.websocket.close();
_this.websocket = null;
}
self.token = null;
self.email = null;
self.password = null;
self.state = ConnectionState.DISCONNECTED;
resolve();
}
});
});
};
// def getGateway
InternalClient.prototype.getGateway = function getGateway() {
var self = this;
return new Promise(function (resolve, reject) {
@@ -146,12 +182,34 @@ var InternalClient = (function () {
switch (packet.t) {
case PacketType.READY:
var startTime = Date.now();
self.users.add(new User(data.user, client));
data.guilds.forEach(function (server) {
self.servers.add(new Server(server, client));
});
data.private_channels.forEach(function (pm) {
self.private_channels.add(new PMChannel(pm, client));
});
self.state = ConnectionState.READY;
setInterval(function () {
return self.sendWS({ op: 1, d: Date.now() });
}, data.heartbeat_interval);
client.emit("ready");
client.emit("debug", "ready packet took " + (Date.now() - startTime) + "ms to process");
client.emit("debug", "ready with " + self.servers.length + " servers, " + self.channels.length + " channels and " + self.users.length + " users cached.");
break;
case PacketType.MESSAGE_CREATE:
// format: https://discordapi.readthedocs.org/en/latest/reference/channels/messages.html#message-format
var channel = self.channels.get("id", data.channel_id);
if (channel) {
channel.messages.add(new Message(data, channel, client));
} else {
client.emit("warn", "message created but channel is not cached");
}
break;

View File

@@ -96,7 +96,8 @@ var Permissions = {
};
var PacketType = {
READY: "READY"
READY: "READY",
MESSAGE_CREATE: "MESSAGE_CREATE"
};
exports.API_ENDPOINT = API;

View File

@@ -5,6 +5,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Equality = require("../Util/Equality.js");
var Cache = require("../Util/Cache.js");
var PermissionOverwrite = require("./PermissionOverwrite.js");
var Channel = (function (_Equality) {
_inherits(Channel, _Equality);
@@ -13,7 +15,6 @@ var Channel = (function (_Equality) {
_classCallCheck(this, Channel);
_Equality.call(this);
this.type = data.type || "text";
this.id = data.id;
this.client = client;
}

38
lib/Structures/Message.js Normal file
View File

@@ -0,0 +1,38 @@
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Cache = require("../Util/Cache.js");
var User = require("./User.js");
var Message = function Message(data, channel, client) {
var _this = this;
_classCallCheck(this, Message);
this.channel = channel;
this.client = client;
this.nonce = data.nonce;
this.attachments = data.attachments;
this.tts = data.tts;
this.embeds = data.embeds;
this.timestamp = Date.parse(data.timestamp);
this.everyoneMentioned = data.mention_everyone;
this.id = data.id;
if (data.edited_timestamp) this.editedTimestamp = Date.parse(data.edited_timestamp);
this.author = client.internal.users.add(new User(data.author, client));
this.content = data.content;
this.mentions = new Cache();
data.mentions.forEach(function (mention) {
// this is .add and not .get because it allows the bot to cache
// users from messages from logs who may have left the server and were
// not previously cached.
console.log(mention);
_this.mentions.add(client.internal.users.add(new User(mention, client)));
});
};
module.exports = Message;

View File

@@ -1,5 +1,7 @@
"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"); } }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
@@ -7,16 +9,31 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
var Channel = require("./Channel.js");
var Equality = require("../Util/Equality.js");
var PMChannel = (function (_Channel) {
_inherits(PMChannel, _Channel);
var PMChannel = (function (_Equality) {
_inherits(PMChannel, _Equality);
function PMChannel(data, client) {
_classCallCheck(this, PMChannel);
_Channel.call(this, data, client);
_Equality.call(this);
this.client = client;
this.type = data.type || "text";
this.id = data.id;
this.lastMessageId = data.last_message_id;
this.recipient = this.client.internal.users.add(data.recipient);
}
/* warning! may return null */
_createClass(PMChannel, [{
key: "lastMessage",
get: function get() {
return this.messages.get("id", this.lastMessageID);
}
}]);
return PMChannel;
})(Channel);
})(Equality);
module.exports = PMChannel;

View File

@@ -10,7 +10,6 @@ var Equality = require("../Util/Equality.js");
var Endpoints = require("../Constants.js").Endpoints;
var Cache = require("../Util/Cache.js");
var User = require("./User.js");
var Member = require("./Member.js");
var TextChannel = require("./TextChannel.js");
var VoiceChannel = require("./VoiceChannel.js");
var Role = require("./Role.js");
@@ -38,10 +37,17 @@ var Server = (function (_Equality) {
this.icon = data.icon;
this.afkTimeout = data.afkTimeout;
this.afkChannelID = data.afk_channel_id;
this.memberMap = {};
data.members.forEach(function (dataUser) {
var user = client.internal.users.add(new User(dataUser, client));
_this.members.add(new Member(dataUser, client, self));
_this.memberMap[dataUser.user.id] = {
roles: dataUser.roles,
mute: dataUser.mute,
deaf: dataUser.deaf,
joinedAt: Date.parse(dataUser.joined_at)
};
var user = client.internal.users.add(new User(dataUser.user, client));
_this.members.add(user);
});
data.channels.forEach(function (dataChannel) {

View File

@@ -0,0 +1,30 @@
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Channel = require("./Channel.js");
var Cache = require("../Util/Cache.js");
var PermissionOverwrite = require("./PermissionOverwrite.js");
var ServerChannel = (function (_Channel) {
_inherits(ServerChannel, _Channel);
function ServerChannel(data, client) {
var _this = this;
_classCallCheck(this, ServerChannel);
_Channel.call(this, data, client);
this.type = data.type;
this.permissionOverwrites = new Cache();
data.permission_overwrites.forEach(function (permission) {
_this.permissionOverwrites.add(new PermissionOverwrite(permission));
});
}
return ServerChannel;
})(Channel);
module.exports = ServerChannel;

View File

@@ -6,30 +6,22 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Channel = require("./Channel.js");
var ServerChannel = require("./ServerChannel.js");
var Cache = require("../Util/Cache.js");
var PermissionOverwrite = require("./PermissionOverwrite.js");
var TextChannel = (function (_Channel) {
_inherits(TextChannel, _Channel);
var TextChannel = (function (_ServerChannel) {
_inherits(TextChannel, _ServerChannel);
function TextChannel(data, client) {
var _this = this;
_classCallCheck(this, TextChannel);
_Channel.call(this, data, client);
_ServerChannel.call(this, data, client);
this.name = data.name;
this.topic = data.topic;
this.position = data.position;
this.lastMessageID = data.last_message_id;
this.messages = new Cache("id", client.options.maximumMessages);
this.permissionOverwrites = new Cache();
data.permission_overwrites.forEach(function (permission) {
_this.permissionOverwrites.add(new PermissionOverwrite(permission));
});
}
/* warning! may return null */
@@ -42,6 +34,6 @@ var TextChannel = (function (_Channel) {
}]);
return TextChannel;
})(Channel);
})(ServerChannel);
module.exports = TextChannel;

View File

@@ -4,18 +4,18 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Channel = require("./Channel.js");
var ServerChannel = require("./ServerChannel.js");
var VoiceChannel = (function (_Channel) {
_inherits(VoiceChannel, _Channel);
var VoiceChannel = (function (_ServerChannel) {
_inherits(VoiceChannel, _ServerChannel);
function VoiceChannel(data, client) {
_classCallCheck(this, VoiceChannel);
_Channel.call(this, data, client);
_ServerChannel.call(this, data, client);
}
return VoiceChannel;
})(Channel);
})(ServerChannel);
module.exports = VoiceChannel;

View File

@@ -5,6 +5,9 @@ module.exports = {
};
var a = new module.exports.Client();
a.on("debug", function (m) {
return console.log("[debug]", m);
});
a.login(process.env["discordEmail"], process.env["discordPass"])["catch"](function (e) {
return console.log(e);
});

View File

@@ -35,7 +35,7 @@ class Client extends EventEmitter{
}
/*
def logout
*/
}

View File

@@ -16,7 +16,8 @@ var User = require("../Structures/User.js"),
TextChannel = require("../Structures/TextChannel.js"),
VoiceChannel = require("../Structures/VoiceChannel.js"),
PMChannel = require("../Structures/PMChannel.js"),
Server = require("../Structures/Server.js");
Server = require("../Structures/Server.js"),
Message = require("../Structures/Message.js");
var zlib;
@@ -36,7 +37,7 @@ class InternalClient {
this.servers = new Cache();
this.private_channels = new Cache();
}
// def login
login(email, password) {
var self = this;
var client = self.client;
@@ -59,6 +60,8 @@ class InternalClient {
var token = res.body.token;
self.state = ConnectionState.LOGGED_IN;
self.token = token;
self.email = email;
self.password = password;
self.getGateway().then((url) => {
@@ -80,6 +83,38 @@ class InternalClient {
});
}
logout() {
var self = this;
return new Promise((resolve, reject)=>{
if(self.state === ConnectionState.DISCONNECTED || self.state === ConnectionState.IDLE){
reject(new Error("Client is not logged in!"));
return;
}
request
.post(Endpoints.LOGOUT)
.set("authorization", self.token)
.end((err, res) => {
if(err){
reject(new Error(err.response.text));
}else{
if(this.websocket){
this.websocket.close();
this.websocket = null;
}
self.token = null;
self.email = null;
self.password = null;
self.state = ConnectionState.DISCONNECTED;
resolve();
}
});
});
}
// def getGateway
getGateway() {
var self = this;
return new Promise((resolve, reject) => {
@@ -156,12 +191,33 @@ class InternalClient {
switch (packet.t) {
case PacketType.READY:
var startTime = Date.now();
self.users.add(new User(data.user, client));
data.guilds.forEach((server) => {
self.servers.add(new Server(server, client));
});
data.private_channels.forEach((pm) => {
self.private_channels.add(new PMChannel(pm, client));
});
self.state = ConnectionState.READY;
setInterval( ()=> self.sendWS({op : 1, d : Date.now()}), data.heartbeat_interval);
client.emit("ready");
client.emit("debug", `ready packet took ${Date.now() - startTime}ms to process`);
client.emit("debug", `ready with ${self.servers.length} servers, ${self.channels.length} channels and ${self.users.length} users cached.`);
break;
case PacketType.MESSAGE_CREATE:
// format: https://discordapi.readthedocs.org/en/latest/reference/channels/messages.html#message-format
var channel = self.channels.get("id", data.channel_id);
if(channel){
channel.messages.add( new Message(data, channel, client) );
}else{
client.emit("warn", "message created but channel is not cached");
}
break;

View File

@@ -60,7 +60,8 @@ var Permissions = {
};
var PacketType = {
READY : "READY"
READY : "READY",
MESSAGE_CREATE : "MESSAGE_CREATE"
}
exports.API_ENDPOINT = API;

View File

@@ -1,14 +1,15 @@
"use strict";
var Equality = require("../Util/Equality.js");
var Cache = require("../Util/Cache.js");
var PermissionOverwrite = require("./PermissionOverwrite.js");
class Channel extends Equality{
constructor(data, client){
super();
this.type = data.type || "text";
this.id = data.id;
this.client = client;
this.client = client;
}
}

View File

@@ -1,16 +0,0 @@
"use strict";
var User = require("./User.js");
class Member extends User{
constructor(data, client, server){
super(data, client);
this.serverID = server;
}
get server(){
return this.client.internal.servers.get("id", this.serverID);
}
}
module.exports = Member;

35
src/Structures/Message.js Normal file
View File

@@ -0,0 +1,35 @@
"use strict";
var Cache = require("../Util/Cache.js");
var User = require("./User.js");
class Message{
constructor(data, channel, client){
this.channel = channel;
this.client = client;
this.nonce = data.nonce;
this.attachments = data.attachments;
this.tts = data.tts;
this.embeds = data.embeds;
this.timestamp = Date.parse(data.timestamp);
this.everyoneMentioned = data.mention_everyone;
this.id = data.id;
if(data.edited_timestamp)
this.editedTimestamp = Date.parse(data.edited_timestamp);
this.author = client.internal.users.add(new User(data.author, client));
this.content = data.content;
this.mentions = new Cache();
data.mentions.forEach((mention) => {
// this is .add and not .get because it allows the bot to cache
// users from messages from logs who may have left the server and were
// not previously cached.
console.log(mention);
this.mentions.add(client.internal.users.add(new User(mention, client)));
});
}
}
module.exports = Message;

View File

@@ -3,9 +3,20 @@
var Channel = require("./Channel.js");
var Equality = require("../Util/Equality.js");
class PMChannel extends Channel{
class PMChannel extends Equality{
constructor(data, client){
super(data, client);
super();
this.client = client;
this.type = data.type || "text";
this.id = data.id;
this.lastMessageId = data.last_message_id;
this.recipient = this.client.internal.users.add(data.recipient);
}
/* warning! may return null */
get lastMessage(){
return this.messages.get("id", this.lastMessageID);
}
}

View File

@@ -4,7 +4,6 @@ var Equality = require("../Util/Equality.js");
var Endpoints = require("../Constants.js").Endpoints;
var Cache = require("../Util/Cache.js");
var User = require("./User.js");
var Member = require("./Member.js");
var TextChannel = require("./TextChannel.js");
var VoiceChannel = require("./VoiceChannel.js");
var Role = require("./Role.js");
@@ -27,10 +26,17 @@ class Server extends Equality {
this.icon = data.icon;
this.afkTimeout = data.afkTimeout;
this.afkChannelID = data.afk_channel_id;
this.memberMap = {};
data.members.forEach( (dataUser) => {
var user = client.internal.users.add(new User(dataUser, client));
this.members.add( new Member(dataUser, client, self) );
this.memberMap[dataUser.user.id] = {
roles : dataUser.roles,
mute : dataUser.mute,
deaf : dataUser.deaf,
joinedAt : Date.parse(dataUser.joined_at)
};
var user = client.internal.users.add(new User(dataUser.user, client));
this.members.add( user );
} );
data.channels.forEach( (dataChannel) => {

View File

@@ -0,0 +1,18 @@
"use strict";
var Channel = require("./Channel.js");
var Cache = require("../Util/Cache.js");
var PermissionOverwrite = require("./PermissionOverwrite.js");
class ServerChannel extends Channel{
constructor(data, client){
super(data, client);
this.type = data.type;
this.permissionOverwrites = new Cache();
data.permission_overwrites.forEach((permission) => {
this.permissionOverwrites.add( new PermissionOverwrite(permission) );
});
}
}
module.exports = ServerChannel;

View File

@@ -1,10 +1,9 @@
"use strict";
var Channel = require("./Channel.js");
var ServerChannel = require("./ServerChannel.js");
var Cache = require("../Util/Cache.js");
var PermissionOverwrite = require("./PermissionOverwrite.js");
class TextChannel extends Channel{
class TextChannel extends ServerChannel{
constructor(data, client){
super(data, client);
@@ -13,11 +12,6 @@ class TextChannel extends Channel{
this.position = data.position;
this.lastMessageID = data.last_message_id;
this.messages = new Cache("id", client.options.maximumMessages);
this.permissionOverwrites = new Cache();
data.permission_overwrites.forEach((permission) => {
this.permissionOverwrites.add( new PermissionOverwrite(permission) );
});
}
/* warning! may return null */

View File

@@ -1,8 +1,8 @@
"use strict";
var Channel = require("./Channel.js");
var ServerChannel = require("./ServerChannel.js");
class VoiceChannel extends Channel{
class VoiceChannel extends ServerChannel{
constructor(data, client){
super(data, client);
}

View File

@@ -3,4 +3,5 @@ module.exports = {
}
var a = new module.exports.Client();
a.on("debug", (m) => console.log("[debug]",m));
a.login(process.env["discordEmail"], process.env["discordPass"]).catch((e)=>console.log(e));