UNPKG

royal-selfbot

Version:

A Discord self-bot library. USE WITH EXTREME CAUTION - AGAINST DISCORD TOS.

126 lines (112 loc) 4.31 kB
// src/managers/BaseManager.js /** * Base class for managing collections of Discord structures. * Provides basic caching functionality. */ class BaseManager { /** * @param {Client} client The instantiating client * @param {Function} holds The class constructor that this manager holds instances of (e.g., Guild, Channel) */ constructor(client, holds) { /** * The client that instantiated this Manager * @type {Client} * @readonly */ this.client = client; /** * The data structure that this manager holds * @type {Function} * @readonly */ this.holds = holds; /** * The cache of items for this manager. * @type {Map<string, object>} // Replace 'object' with the specific structure type (e.g., Guild) in subclasses */ this.cache = new Map(); } /** * Resolves a data object into an instance of the structure that this manager holds. * @param {string|object} idOrInstance The ID or instance of the structure to resolve * @returns {?object} An instance of the structure, if it could be resolved * @protected - Typically used internally by managers */ _resolve(idOrInstance) { if (idOrInstance instanceof this.holds) { return idOrInstance; } if (typeof idOrInstance === 'string') { return this.cache.get(idOrInstance) || null; } return null; } /** * Resolves an ID into an instance of the structure that this manager holds. * @param {string} id The ID of the structure to resolve * @returns {?object} An instance of the structure, if it could be resolved */ resolve(id) { if (typeof id === 'string') { return this.cache.get(id) || null; } return null; } /** * Adds or updates an item in the cache. * @param {object} data The data for the item to add. Must include an 'id' property. * @param {boolean} [cache=true] Whether to cache the item. * @param {object} [options={}] Options for adding the item. * @param {boolean} [options.extras=[]] Extra parameters to pass to the structure constructor. * @returns {object} The added or updated structure instance. * @protected - Typically used internally by managers handling gateway events or REST responses. */ _add(data, cache = true, { extras = [] } = {}) { if (!data || typeof data.id !== 'string') { console.warn(`[BaseManager._add] Received invalid data, missing 'id'. Data:`, data); return null; // Cannot cache without an ID } const existing = this.cache.get(data.id); if (existing && existing._patch && typeof existing._patch === 'function') { // If exists and has a _patch method, update it existing._patch(data); return existing; } // Create a new instance // Dynamically pass client and data, plus any extras const entry = new this.holds(this.client, data, ...extras); if (cache) this.cache.set(data.id, entry); return entry; } /** * Removes an item from the cache. * @param {string|object} idOrInstance The ID or instance to remove. * @returns {?object} The removed structure instance, if found. */ remove(idOrInstance) { const id = this._resolveId(idOrInstance); if (!id) return null; const existing = this.cache.get(id); if (!existing) return null; this.cache.delete(id); return existing; } /** * Resolves a structure ID from potential input types. * @param {string|object} idOrInstance The ID or instance. * @returns {?string} The resolved ID, or null if not resolvable. */ _resolveId(idOrInstance) { if (idOrInstance instanceof this.holds) { return idOrInstance.id; } if (typeof idOrInstance === 'string') { return idOrInstance; } return null; } // Future enhancements could include fetching logic (optional) // async fetch(id, { force = false, cache = true } = {}) { ... } } module.exports = BaseManager;