Clean up some stuff

This commit is contained in:
Schuyler Cebulskie
2016-09-19 04:32:24 -04:00
parent 6ede7a32fd
commit 6a94658dd7
7 changed files with 83 additions and 57 deletions

File diff suppressed because one or more lines are too long

View File

@@ -20,93 +20,152 @@ class Client extends EventEmitter {
*/
constructor(options) {
super();
/**
* The options the client was instantiated with
* @type {ClientOptions}
*/
this.options = mergeDefault(Constants.DefaultOptions, options);
/**
* The REST manager of the client
* @type {RESTManager}
* @private
*/
this.rest = new RESTManager(this);
/**
* The data manager of the Client
* @type {ClientDataManager}
* @private
*/
this.dataManager = new ClientDataManager(this);
/**
* The manager of the Client
* @type {ClientManager}
* @private
*/
this.manager = new ClientManager(this);
/**
* The WebSocket Manager of the Client
* @type {WebSocketManager}
* @private
*/
this.ws = new WebSocketManager(this);
/**
* The Data Resolver of the Client
* @type {ClientDataResolver}
* @private
*/
this.resolver = new ClientDataResolver(this);
/**
* The Action Manager of the Client
* @type {ActionsManager}
* @private
*/
this.actions = new ActionsManager(this);
/**
* The Voice Manager of the Client
* @type {ClientVoiceManager}
* @private
*/
this.voice = new ClientVoiceManager(this);
/**
* A Collection of the Client's stored users
* @type {Collection<string, User>}
*/
this.users = new Collection();
/**
* A Collection of the Client's stored guilds
* @type {Collection<string, Guild>}
*/
this.guilds = new Collection();
/**
* A Collection of the Client's stored channels
* @type {Collection<string, Channel>}
*/
this.channels = new Collection();
/**
* The authorization token for the logged in user/bot.
* @type {?string}
*/
this.token = null;
/**
* The ClientUser representing the logged in Client
* @type {?ClientUser}
*/
this.user = null;
/**
* The email, if there is one, for the logged in Client
* @type {?string}
*/
this.email = null;
/**
* The password, if there is one, for the logged in Client
* @type {?string}
*/
this.password = null;
/**
* The ClientUser representing the logged in Client
* @type {?ClientUser}
*/
this.user = null;
/**
* The date at which the Client was regarded as being in the `READY` state.
* @type {?Date}
*/
this.readyTime = null;
this._timeouts = new Set();
this._intervals = new Set();
}
/**
* The status for the logged in Client.
* @readonly
* @type {?number}
*/
get status() {
return this.ws.status;
}
/**
* The uptime for the logged in Client.
* @readonly
* @type {?number}
*/
get uptime() {
return this.readyTime ? Date.now() - this.readyTime : null;
}
/**
* Returns a Collection, mapping Guild ID to Voice Connections.
* @readonly
* @type {Collection<string, VoiceConnection>}
*/
get voiceConnections() {
return this.voice.connections;
}
/**
* The emojis that the client can use. Mapped by emoji ID.
* @type {Collection<string, Emoji>}
* @readonly
*/
get emojis() {
const emojis = new Collection();
this.guilds.map(g => g.emojis.map(e => emojis.set(e.id, e)));
return emojis;
}
/**
* Logs the client in. If successful, resolves with the account's token. <warn>If you're making a bot, it's
* much better to use a bot account rather than a user account.
@@ -184,44 +243,6 @@ class Client extends EventEmitter {
return this.rest.methods.getInvite(code);
}
/**
* Returns a Collection, mapping Guild ID to Voice Connections.
* @readonly
* @type {Collection<string, VoiceConnection>}
*/
get voiceConnections() {
return this.voice.connections;
}
/**
* The uptime for the logged in Client.
* @readonly
* @type {?number}
*/
get uptime() {
return this.readyTime ? Date.now() - this.readyTime : null;
}
/**
* The emojis that the client can use. Mapped by emoji ID.
* @type {Collection<string, Emoji>}
* @readonly
*/
get emojis() {
const emojis = new Collection();
this.guilds.map(g => g.emojis.map(e => emojis.set(e.id, e)));
return emojis;
}
/**
* The status for the logged in Client.
* @readonly
* @type {?number}
*/
get status() {
return this.ws.status;
}
setTimeout(fn, ...params) {
const timeout = setTimeout(() => {
fn();

View File

@@ -11,6 +11,7 @@ class ClientManager {
* @type {Client}
*/
this.client = client;
/**
* The heartbeat interval, null if not yet set
* @type {?number}

View File

@@ -13,9 +13,15 @@ class RESTMethods {
this.rest = restManager;
}
loginToken(token) {
return new Promise((resolve, reject) => {
this.rest.client.manager.connectToWebSocket(token, resolve, reject);
});
}
loginEmailPassword(email, password) {
return new Promise((resolve, reject) => {
this.rest.client.emit('debug', 'Client launched using email and password - should use token instead');
this.rest.client.emit('warn', 'Client launched using email and password - should use token instead');
this.rest.client.email = email;
this.rest.client.password = password;
this.rest.makeRequest('post', Constants.Endpoints.login, false, { email, password })
@@ -26,12 +32,6 @@ class RESTMethods {
});
}
loginToken(token) {
return new Promise((resolve, reject) => {
this.rest.client.manager.connectToWebSocket(token, resolve, reject);
});
}
logout() {
return this.rest.makeRequest('post', Constants.Endpoints.logout, true);
}

View File

@@ -1,7 +1,6 @@
const RequestHandler = require('./RequestHandler');
class BurstRequestHandler extends RequestHandler {
constructor(restManager, endpoint) {
super(restManager, endpoint);
this.requestRemaining = 1;
@@ -58,7 +57,6 @@ class BurstRequestHandler extends RequestHandler {
});
}
handle() {
super.handle();
if (this.requestRemaining < 1 || this.queue.length === 0 || this.globalLimit) return;
@@ -67,7 +65,6 @@ class BurstRequestHandler extends RequestHandler {
this.requestRemaining--;
}
}
}
module.exports = BurstRequestHandler;

View File

@@ -14,36 +14,43 @@ class WebSocketManager {
* @type {Client}
*/
this.client = client;
/**
* A WebSocket Packet manager, it handles all the messages
* @type {PacketManager}
*/
this.packetManager = new PacketManager(this);
/**
* The status of the WebSocketManager, a type of Constants.Status. It defaults to IDLE.
* @type {number}
*/
this.status = Constants.Status.IDLE;
/**
* The session ID of the connection, null if not yet available.
* @type {?string}
*/
this.sessionID = null;
/**
* The packet count of the client, null if not yet available.
* @type {?number}
*/
this.sequence = -1;
/**
* The gateway address for this WebSocket connection, null if not yet available.
* @type {?string}
*/
this.gateway = null;
/**
* Whether READY was emitted normally (all packets received) or not
* @type {boolean}
*/
this.normalReady = false;
/**
* The WebSocket connection to the gateway
* @type {?WebSocket}
@@ -197,9 +204,7 @@ class WebSocketManager {
* @event Client#error
* @param {Error} error The encountered error
*/
if (this.client.listenerCount('error') > 0) {
this.client.emit('error', err);
}
if (this.client.listenerCount('error') > 0) this.client.emit('error', err);
this.tryReconnect();
}

View File

@@ -15,11 +15,13 @@ class Shard {
* @type {ShardingManager}
*/
this.manager = manager;
/**
* The shard ID
* @type {number}
*/
this.id = id;
/**
* The process of the shard
* @type {ChildProcess}