2022-03-19 10:37:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { setInterval } = require('node:timers');
|
|
|
|
const { Collection } = require('@discordjs/collection');
|
|
|
|
const APIRequest = require('./APIRequest');
|
|
|
|
const routeBuilder = require('./APIRouter');
|
|
|
|
const RequestHandler = require('./RequestHandler');
|
|
|
|
const { Error } = require('../errors');
|
|
|
|
const { Endpoints } = require('../util/Constants');
|
|
|
|
|
|
|
|
class RESTManager {
|
|
|
|
constructor(client) {
|
|
|
|
this.client = client;
|
|
|
|
this.handlers = new Collection();
|
|
|
|
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) {
|
|
|
|
this.sweepInterval = setInterval(() => {
|
|
|
|
this.handlers.sweep(handler => handler._inactive);
|
|
|
|
}, client.options.restSweepInterval * 1_000).unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get api() {
|
|
|
|
return routeBuilder(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAuth() {
|
2022-05-14 08:06:15 +00:00
|
|
|
if ((this.client.token && this.client.user && this.client.user.bot) || this.client.accessToken) {
|
2022-03-24 10:55:32 +00:00
|
|
|
return `Bot ${this.client.token}`;
|
|
|
|
} else if (this.client.token) {
|
|
|
|
return this.client.token;
|
|
|
|
}
|
2022-05-14 08:06:15 +00:00
|
|
|
/*
|
|
|
|
// v13.7
|
|
|
|
const token = this.client.token ?? this.client.accessToken;
|
|
|
|
if (token) return `Bot ${token}`;
|
|
|
|
*/
|
2022-03-19 10:37:45 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 10:55:32 +00:00
|
|
|
module.exports = RESTManager;
|