mirror of
https://github.com/danbulant/discord.js
synced 2026-05-29 12:20:23 +00:00
* tojson things * fix client * ignore private properties * remove extra property descriptors * handle primitive flattening * remove unused import * add toJSON to collections * reduce stateful props * state * allow custom prop names when flattening * fix client * fix build * fix flatten docs * remove guild.available, cleanup permissions, remove arbitrary id reduction * fix util import * add valueOf as needed, update member props * fix incorrect merge * update permissionoverwrites and permissions remove serialization of permissions in PermissionOverwrites#toJSON. change Permissions#toJSON to serialize permissions, by default excluding admin checks. * change Permissions#toJSON to return the primitive * Permissions#toJSON explicitly return bitfield
114 lines
2.7 KiB
JavaScript
114 lines
2.7 KiB
JavaScript
const EventEmitter = require('events');
|
|
const RESTManager = require('../rest/RESTManager');
|
|
const Util = require('../util/Util');
|
|
const { DefaultOptions } = require('../util/Constants');
|
|
|
|
/**
|
|
* The base class for all clients.
|
|
* @extends {EventEmitter}
|
|
*/
|
|
class BaseClient extends EventEmitter {
|
|
constructor(options = {}) {
|
|
super();
|
|
|
|
/**
|
|
* The options the client was instantiated with
|
|
* @type {ClientOptions}
|
|
*/
|
|
this.options = Util.mergeDefault(DefaultOptions, options);
|
|
|
|
/**
|
|
* The REST manager of the client
|
|
* @type {RESTManager}
|
|
* @private
|
|
*/
|
|
this.rest = new RESTManager(this, options._tokenType);
|
|
|
|
/**
|
|
* Timeouts set by {@link BaseClient#setTimeout} that are still active
|
|
* @type {Set<Timeout>}
|
|
* @private
|
|
*/
|
|
this._timeouts = new Set();
|
|
|
|
/**
|
|
* Intervals set by {@link BaseClient#setInterval} that are still active
|
|
* @type {Set<Timeout>}
|
|
* @private
|
|
*/
|
|
this._intervals = new Set();
|
|
}
|
|
|
|
/**
|
|
* API shortcut
|
|
* @type {Object}
|
|
* @readonly
|
|
* @private
|
|
*/
|
|
get api() {
|
|
return this.rest.api;
|
|
}
|
|
|
|
/**
|
|
* Destroys all assets used by the base client.
|
|
*/
|
|
destroy() {
|
|
for (const t of this._timeouts) clearTimeout(t);
|
|
for (const i of this._intervals) clearInterval(i);
|
|
this._timeouts.clear();
|
|
this._intervals.clear();
|
|
}
|
|
|
|
/**
|
|
* Sets a timeout that will be automatically cancelled if the client is destroyed.
|
|
* @param {Function} fn Function to execute
|
|
* @param {number} delay Time to wait before executing (in milliseconds)
|
|
* @param {...*} args Arguments for the function
|
|
* @returns {Timeout}
|
|
*/
|
|
setTimeout(fn, delay, ...args) {
|
|
const timeout = setTimeout(() => {
|
|
fn(...args);
|
|
this._timeouts.delete(timeout);
|
|
}, delay);
|
|
this._timeouts.add(timeout);
|
|
return timeout;
|
|
}
|
|
|
|
/**
|
|
* Clears a timeout.
|
|
* @param {Timeout} timeout Timeout to cancel
|
|
*/
|
|
clearTimeout(timeout) {
|
|
clearTimeout(timeout);
|
|
this._timeouts.delete(timeout);
|
|
}
|
|
|
|
/**
|
|
* Sets an interval that will be automatically cancelled if the client is destroyed.
|
|
* @param {Function} fn Function to execute
|
|
* @param {number} delay Time to wait between executions (in milliseconds)
|
|
* @param {...*} args Arguments for the function
|
|
* @returns {Timeout}
|
|
*/
|
|
setInterval(fn, delay, ...args) {
|
|
const interval = setInterval(fn, delay, ...args);
|
|
this._intervals.add(interval);
|
|
return interval;
|
|
}
|
|
|
|
/**
|
|
* Clears an interval.
|
|
* @param {Timeout} interval Interval to cancel
|
|
*/
|
|
clearInterval(interval) {
|
|
clearInterval(interval);
|
|
this._intervals.delete(interval);
|
|
}
|
|
|
|
toJSON(...props) {
|
|
return Util.flatten(this, { domain: false }, ...props);
|
|
}
|
|
}
|
|
|
|
module.exports = BaseClient;
|