diff --git a/src/rest/RESTManager.js b/src/rest/RESTManager.js index 4ca4cb174..586125a54 100644 --- a/src/rest/RESTManager.js +++ b/src/rest/RESTManager.js @@ -3,23 +3,19 @@ const APIRequest = require('./APIRequest'); const routeBuilder = require('./APIRouter'); const { Error } = require('../errors'); const { Endpoints } = require('../util/Constants'); +const Collection = require('../util/Collection'); class RESTManager { constructor(client, tokenPrefix = 'Bot') { this.client = client; - this.handlers = {}; - this.rateLimitedEndpoints = {}; + this.handlers = new Collection(); this.globallyRateLimited = false; this.tokenPrefix = tokenPrefix; this.versioned = true; this.timeDifferences = []; if (client.options.restSweepInterval > 0) { client.setInterval(() => { - for (const handler in this.handlers) { - if (this.handlers[handler]._inactive) { - delete this.handlers[handler]; - } - } + this.handlers.sweep(handler => handler._inactive); }, client.options.restSweepInterval * 1000); } } @@ -69,11 +65,14 @@ class RESTManager { request(method, url, options = {}) { const apiRequest = new APIRequest(this, method, url, options); - if (!this.handlers[apiRequest.route]) { - this.handlers[apiRequest.route] = new handlers.RequestHandler(this, this.getRequestHandler()); + let handler = this.handlers.get(apiRequest.route); + + if (!handler) { + handler = new handlers.RequestHandler(this, this.getRequestHandler()); + this.handlers.set(apiRequest.route, handler); } - return this.push(this.handlers[apiRequest.route], apiRequest); + return this.push(handler, apiRequest); } set endpoint(endpoint) {