UNPKG

discord.js-self

Version:

A fork of discord.js with support of user accounts

71 lines (59 loc) 1.72 kB
'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) { this.client = client; this.handlers = new Collection(); this.versioned = true; this.globalTimeout = null; if (client.options.restSweepInterval > 0) { client.setInterval(() => { this.handlers.sweep(handler => handler._inactive); }, client.options.restSweepInterval * 1000); } } get api() { return routeBuilder(this); } getAuth() { if (this.client.token && this.client.user && this.client.user.bot) { return `Bot ${this.client.token}`; } else if (this.client.token) { return this.client.token; } throw new Error('TOKEN_MISSING'); } get cdn() { return Endpoints.CDN(this.client.options.http.cdn); } push(handler, apiRequest) { return new Promise((resolve, reject) => { handler .push({ request: apiRequest, resolve, reject, retries: 0, }) .catch(reject); }); } 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 this.push(handler, apiRequest); } set endpoint(endpoint) { this.client.options.http.api = endpoint; } } module.exports = RESTManager;