MOOver.js/node_modules/discord.js/src/rest/RESTManager.js

63 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-10-30 21:52:39 +00:00
'use strict';
2022-01-24 18:05:21 +00:00
const { setInterval } = require('node:timers');
const { Collection } = require('@discordjs/collection');
2020-10-30 21:52:39 +00:00
const APIRequest = require('./APIRequest');
const routeBuilder = require('./APIRouter');
const RequestHandler = require('./RequestHandler');
const { Error } = require('../errors');
const { Endpoints } = require('../util/Constants');
class RESTManager {
2022-01-24 18:05:21 +00:00
constructor(client) {
2020-10-30 21:52:39 +00:00
this.client = client;
this.handlers = new Collection();
this.versioned = true;
2022-01-24 18:05:21 +00:00
this.globalLimit = client.options.restGlobalRateLimit > 0 ? client.options.restGlobalRateLimit : Infinity;
this.globalRemaining = this.globalLimit;
this.globalReset = null;
this.globalDelay = null;
2020-10-30 21:52:39 +00:00
if (client.options.restSweepInterval > 0) {
2022-01-24 18:05:21 +00:00
this.sweepInterval = setInterval(() => {
2020-10-30 21:52:39 +00:00
this.handlers.sweep(handler => handler._inactive);
2022-01-24 18:05:21 +00:00
}, client.options.restSweepInterval * 1_000).unref();
2020-10-30 21:52:39 +00:00
}
}
get api() {
return routeBuilder(this);
}
getAuth() {
2022-01-24 18:05:21 +00:00
const token = this.client.token ?? this.client.accessToken;
if (token) return `Bot ${token}`;
2020-10-30 21:52:39 +00:00
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;