UNPKG

guilded.ts

Version:

A powerful NPM module that allows you to easily interact with the Guilded API.

103 lines 3.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Collector = void 0; const collection_1 = require("@discordjs/collection"); const events_1 = __importDefault(require("events")); /** * The collector of a data model. * @example new Collector(client); */ class Collector extends events_1.default { client; options; /** The collected data. */ collected; /** The date the collector was created. */ createdAt; /** The date the collector was ended. */ endedAt; /** The idle timeout for the collector. */ idleTimeout; /** * @param client The client the collector belongs to. * @param options The options of the collector. */ constructor(client, options = {}) { super(); this.client = client; this.options = options; this.createdAt = new Date(); this.collected = new collection_1.Collection(); if (options.time) setTimeout(this.end.bind(this), options.time); if (options.idle) this.idleTimeout = setTimeout(this.end.bind(this), options.idle); } /** The timestamp the collector was created. */ get createdTimestamp() { return this.createdAt.getTime(); } /** Whether the collector has ended. */ get isEnded() { return !!this.endedAt; } /** The timestamp the collector was ended. */ get endedTimestamp() { return this.endedAt?.getTime(); } /** The time the collector has been running. */ get uptime() { return this.isEnded ? this.endedAt.getTime() - this.createdAt.getTime() : Date.now() - this.createdAt.getTime(); } /** * End the collector. * @example collector.end(); */ end() { if (this.isEnded) return; this.endedAt = new Date(); this.emit('end', this.collected); } /** * Collect a item. * @param item The item to collect. * @returns The collected item. * @example collector.collect(item); */ async collect(item) { const filter = this.options.filter ? await this.options.filter(item) : true; if (this.isEnded || !filter) return; this.collected.set(item.id, item); this.emit('collect', item); if (this.idleTimeout) { clearTimeout(this.idleTimeout); this.idleTimeout = setTimeout(this.end.bind(this), this.options.idle); } if (this.collected.size >= (this.options.max ?? Infinity)) this.end(); return item; } /** * Dispose a collected item. * @param itemId The ID of the item to dispose. * @returns The disposed item. * @example collector.dispose('abc'); */ dispose(itemId) { const item = this.collected.get(itemId); if (this.options.dispose === false || this.isEnded || !item) return; this.collected.delete(itemId); this.emit('dispose', item); return item; } } exports.Collector = Collector; //# sourceMappingURL=Collector.js.map