Merge remote-tracking branch 'upstream/indev' into bugfix/docs

This commit is contained in:
Kelvin Wu
2016-09-24 14:10:04 -04:00
6 changed files with 26 additions and 10 deletions

File diff suppressed because one or more lines are too long

View File

@@ -32,6 +32,10 @@ class ClientManager {
this.client.rest.methods.getGateway().then(gateway => { this.client.rest.methods.getGateway().then(gateway => {
this.client.emit(Constants.Events.DEBUG, `Using gateway ${gateway}`); this.client.emit(Constants.Events.DEBUG, `Using gateway ${gateway}`);
this.client.ws.connect(gateway); this.client.ws.connect(gateway);
this.client.ws.once('close', event => {
if (event.code === 4004) reject(new Error(Constants.Errors.BAD_LOGIN));
if (event.code === 4010) reject(new Error(Constants.Errors.INVALID_SHARD));
});
this.client.once(Constants.Events.READY, () => { this.client.once(Constants.Events.READY, () => {
resolve(token); resolve(token);
this.client.clearTimeout(timeout); this.client.clearTimeout(timeout);

View File

@@ -1,6 +1,16 @@
const request = require('superagent'); const request = require('superagent');
const Constants = require('../../util/Constants'); const Constants = require('../../util/Constants');
function getRoute(url) {
let route = url.split('?')[0];
if (route.includes('/channels/') || route.includes('/guilds/')) {
const startInd = ~route.indexOf('/channels/') ? route.indexOf('/channels/') : route.indexOf('/guilds/');
const majorID = route.substring(startInd).split('/')[2];
route = route.replace(/(\d{8,})/g, ':id').replace(':id', majorID);
}
return route;
}
class APIRequest { class APIRequest {
constructor(rest, method, url, auth, data, file) { constructor(rest, method, url, auth, data, file) {
this.rest = rest; this.rest = rest;
@@ -9,10 +19,7 @@ class APIRequest {
this.auth = auth; this.auth = auth;
this.data = data; this.data = data;
this.file = file; this.file = file;
} this.route = getRoute(this.url);
getEndpoint() {
return this.url;
} }
getAuth() { getAuth() {

View File

@@ -39,12 +39,12 @@ class RESTManager {
makeRequest(method, url, auth, data, file) { makeRequest(method, url, auth, data, file) {
const apiRequest = new APIRequest(this, method, url, auth, data, file); const apiRequest = new APIRequest(this, method, url, auth, data, file);
if (!this.handlers[apiRequest.getEndpoint()]) { if (!this.handlers[apiRequest.route]) {
const RequestHandlerType = this.getRequestHandler(); const RequestHandlerType = this.getRequestHandler();
this.handlers[apiRequest.getEndpoint()] = new RequestHandlerType(this, apiRequest.getEndpoint()); this.handlers[apiRequest.route] = new RequestHandlerType(this, apiRequest.route);
} }
return this.push(this.handlers[apiRequest.getEndpoint()], apiRequest); return this.push(this.handlers[apiRequest.route], apiRequest);
} }
} }

View File

@@ -1,4 +1,5 @@
const WebSocket = require('ws'); const WebSocket = require('ws');
const EventEmitter = require('events').EventEmitter;
const Constants = require('../../util/Constants'); const Constants = require('../../util/Constants');
const zlib = require('zlib'); const zlib = require('zlib');
const PacketManager = require('./packets/WebSocketPacketManager'); const PacketManager = require('./packets/WebSocketPacketManager');
@@ -7,8 +8,9 @@ const PacketManager = require('./packets/WebSocketPacketManager');
* The WebSocket Manager of the Client * The WebSocket Manager of the Client
* @private * @private
*/ */
class WebSocketManager { class WebSocketManager extends EventEmitter {
constructor(client) { constructor(client) {
super();
/** /**
* The Client that instantiated this WebSocketManager * The Client that instantiated this WebSocketManager
* @type {Client} * @type {Client}
@@ -164,12 +166,14 @@ class WebSocketManager {
* @param {Object} event The received websocket data * @param {Object} event The received websocket data
*/ */
eventClose(event) { eventClose(event) {
this.emit('close', event);
/** /**
* Emitted whenever the client websocket is disconnected * Emitted whenever the client websocket is disconnected
* @event Client#disconnect * @event Client#disconnect
*/ */
if (!this.reconnecting) this.client.emit(Constants.Events.DISCONNECT); if (!this.reconnecting) this.client.emit(Constants.Events.DISCONNECT);
if (event.code === 4004) throw new Error(Constants.Errors.BAD_LOGIN); if (event.code === 4004) return;
if (event.code === 4010) return;
if (!this.reconnecting && event.code !== 1000) this.tryReconnect(); if (!this.reconnecting && event.code !== 1000) this.tryReconnect();
} }

View File

@@ -72,6 +72,7 @@ exports.Errors = {
NOT_A_PERMISSION: 'Invalid permission string or number.', NOT_A_PERMISSION: 'Invalid permission string or number.',
INVALID_RATE_LIMIT_METHOD: 'Unknown rate limiting method.', INVALID_RATE_LIMIT_METHOD: 'Unknown rate limiting method.',
BAD_LOGIN: 'Incorrect login details were provided.', BAD_LOGIN: 'Incorrect login details were provided.',
INVALID_SHARD: 'Invalid shard settings were provided',
}; };
const API = `https://discordapp.com/api/v${exports.DefaultOptions.protocol_version}`; const API = `https://discordapp.com/api/v${exports.DefaultOptions.protocol_version}`;