Files
discord.js/src/rest/RESTManager.js
Jan f7643f7bbe feat: interactions (#5448)
Co-authored-by: izexi <43889168+izexi@users.noreply.github.com>
Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
Co-authored-by: Advaith <advaithj1@gmail.com>
Co-authored-by: Shiaupiau <stu43005@gmail.com>
Co-authored-by: monbrey <rsm999@uowmail.edu.au>
Co-authored-by: Tiemen <ThaTiemsz@users.noreply.github.com>
Co-authored-by: Carter <carter@elhnet.net>
2021-05-07 17:22:33 +02:00

64 lines
1.7 KiB
JavaScript

'use strict';
const APIRequest = require('./APIRequest');
const routeBuilder = require('./APIRouter');
const RequestHandler = require('./RequestHandler');
const { Error } = require('../errors');
const Collection = require('../util/Collection');
const { Endpoints } = require('../util/Constants');
class RESTManager {
constructor(client, tokenPrefix = 'Bot') {
this.client = client;
this.handlers = new Collection();
this.tokenPrefix = tokenPrefix;
this.versioned = true;
this.globalLimit = client.options.restGlobalRateLimit > 0 ? client.options.restGlobalRateLimit : Infinity;
this.globalRemaining = this.globalLimit;
this.globalReset = null;
this.globalDelay = null;
if (client.options.restSweepInterval > 0) {
const interval = client.setInterval(() => {
this.handlers.sweep(handler => handler._inactive);
}, client.options.restSweepInterval * 1000);
interval.unref();
}
}
get api() {
return routeBuilder(this);
}
getAuth() {
const token = this.client.token || this.client.accessToken;
if (token) return `${this.tokenPrefix} ${token}`;
throw new Error('TOKEN_MISSING');
}
get cdn() {
return Endpoints.CDN(this.client.options.http.cdn);
}
request(method, url, options = {}) {
const apiRequest = new APIRequest(this, method, url, options);
let handler = this.handlers.get(apiRequest.route);
if (!handler) {
handler = new RequestHandler(this);
this.handlers.set(apiRequest.route, handler);
}
return handler.push(apiRequest);
}
get endpoint() {
return this.client.options.http.api;
}
set endpoint(endpoint) {
this.client.options.http.api = endpoint;
}
}
module.exports = RESTManager;