mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 09:33:32 +01:00
Started rewrite
This commit is contained in:
41
lib/Client/Client.js
Normal file
41
lib/Client/Client.js
Normal file
@@ -0,0 +1,41 @@
|
||||
"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 InternalClient = require("./InternalClient.js");
|
||||
var EventEmitter = require("events");
|
||||
|
||||
var Client = (function (_EventEmitter) {
|
||||
_inherits(Client, _EventEmitter);
|
||||
|
||||
/*
|
||||
this class is an interface for the internal
|
||||
client.
|
||||
*/
|
||||
|
||||
function Client(options) {
|
||||
_classCallCheck(this, Client);
|
||||
|
||||
_EventEmitter.call(this, options);
|
||||
this.internal = new InternalClient(this);
|
||||
}
|
||||
|
||||
Client.prototype.login = function login(email, password) {
|
||||
var cb = arguments.length <= 2 || arguments[2] === undefined ? function (err, token) {} : arguments[2];
|
||||
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
|
||||
self.internal.login(email, password).then(function (token) {})["catch"](function (e) {
|
||||
cb(e);
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return Client;
|
||||
})(EventEmitter);
|
||||
|
||||
module.exports = Client;
|
||||
7
lib/Client/ConnectionState.js
Normal file
7
lib/Client/ConnectionState.js
Normal file
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
exports.IDLE = 0;
|
||||
exports.LOGGING_IN = 1;
|
||||
exports.LOGGED_IN = 2;
|
||||
exports.READY = 3;
|
||||
exports.DISCONNECTED = 4;
|
||||
132
lib/Client/InternalClient.js
Normal file
132
lib/Client/InternalClient.js
Normal file
@@ -0,0 +1,132 @@
|
||||
"use strict";
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var EventEmitter = require("events");
|
||||
var request = require("superagent");
|
||||
var ConnectionState = require("./ConnectionState.js");
|
||||
var Constants = require("../Constants.js"),
|
||||
Endpoints = Constants.Endpoints;
|
||||
|
||||
var zlib;
|
||||
|
||||
var InternalClient = (function () {
|
||||
function InternalClient(discordClient) {
|
||||
_classCallCheck(this, InternalClient);
|
||||
|
||||
this.client = discordClient;
|
||||
this.state = ConnectionState.IDLE;
|
||||
this.websocket = null;
|
||||
|
||||
if (this.client.options.compress) {
|
||||
zlib = require("zlib");
|
||||
}
|
||||
}
|
||||
|
||||
InternalClient.prototype.login = function login(email, password) {
|
||||
var self = this;
|
||||
var client = self.client;
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (self.state === ConnectionState.DISCONNECTED || self.state === ConnectionState.IDLE) {
|
||||
|
||||
self.state = ConnectionState.LOGGING_IN;
|
||||
|
||||
request.post(Endpoints.LOGIN).send({ email: email, password: password }).end(function (err, res) {
|
||||
|
||||
if (err) {
|
||||
self.state = ConnectionState.DISCONNECTED;
|
||||
self.websocket = null;
|
||||
client.emit("disconnected");
|
||||
reject(new Error(err.response.text));
|
||||
} else {
|
||||
var token = res.body.token;
|
||||
self.state = ConnectionState.LOGGED_IN;
|
||||
self.token = token;
|
||||
|
||||
self.getGateway().then(function (url) {
|
||||
|
||||
self.createWS(url);
|
||||
resolve(token);
|
||||
})["catch"](function (e) {
|
||||
self.state = ConnectionState.DISCONNECTED;
|
||||
client.emit("disconnected");
|
||||
reject(new Error(err.response.text));
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
reject(new Error("already logging in/logged in/ready!"));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
InternalClient.prototype.getGateway = function getGateway() {
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
|
||||
request.get(Endpoints.GATEWAY).set("authorization", self.token).end(function (err, res) {
|
||||
if (err) reject(err);else resolve(res.body.url);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
InternalClient.prototype.sendWS = function sendWS(object) {
|
||||
this.websocket.send(JSON.stringify(object));
|
||||
};
|
||||
|
||||
InternalClient.prototype.createWS = function createWS(url) {
|
||||
var self = this;
|
||||
|
||||
if (this.websocket) return false;
|
||||
|
||||
this.websocket = new WebSocket(url);
|
||||
|
||||
this.websocket.onopen = function () {
|
||||
|
||||
self.sendWS({
|
||||
op: 2,
|
||||
d: {
|
||||
token: self.token,
|
||||
v: 3,
|
||||
compress: self.client.options.compress,
|
||||
properties: {
|
||||
"$os": "discord.js",
|
||||
"$browser": "discord.js",
|
||||
"$device": "discord.js",
|
||||
"$referrer": "discord.js",
|
||||
"$referring_domain": "discord.js"
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this.websocket.onclose = function () {
|
||||
self.websocket = null;
|
||||
self.state = ConnectionState.DISCONNECTED;
|
||||
self.client.emit("disconnected");
|
||||
};
|
||||
|
||||
this.websocket.onmessage = function (e) {
|
||||
|
||||
if (e.type === "Binary") {
|
||||
e.data = zlib.inflateSync(e.data).toString();
|
||||
}
|
||||
|
||||
var packet, data;
|
||||
try {
|
||||
packet = JSON.parse(e.data);
|
||||
data = packet.d;
|
||||
} catch (e) {
|
||||
self.client.emit("error", e);
|
||||
}
|
||||
|
||||
self.emit("raw", packet);
|
||||
|
||||
switch (packet.t) {}
|
||||
};
|
||||
};
|
||||
|
||||
return InternalClient;
|
||||
})();
|
||||
|
||||
module.exports = InternalClient;
|
||||
Reference in New Issue
Block a user