UNPKG

seyfert

Version:

The most advanced framework for discord bots

120 lines (119 loc) 4.27 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Collectors = void 0; const node_crypto_1 = require("node:crypto"); const RawEvents = __importStar(require("../events/hooks")); class Collectors { values = new Map(); generateRandomUUID(name) { const collectors = this.values.get(name); if (!collectors) return '*'; let nonce = (0, node_crypto_1.randomUUID)(); while (collectors.find(x => x.nonce === nonce)) { nonce = (0, node_crypto_1.randomUUID)(); } return nonce; } create(options) { const nonce = this.generateRandomUUID(options.event); if (!this.values.has(options.event)) { this.values.set(options.event, []); } this.values.get(options.event).push({ options: { ...options, name: options.event, }, idle: options.idle && options.idle > 0 ? setTimeout(() => { return this.delete(options.event, nonce, 'idle'); }, options.idle) : undefined, timeout: options.timeout && options.timeout > 0 ? setTimeout(() => { return this.delete(options.event, nonce, 'timeout'); }, options.timeout) : undefined, nonce, }); return options; } async delete(name, nonce, reason = 'unknown') { const collectors = this.values.get(name); if (!collectors?.length) { if (collectors) this.values.delete(name); return; } const index = collectors.findIndex(x => x.nonce === nonce); if (index === -1) return; const collector = collectors[index]; clearTimeout(collector.idle); clearTimeout(collector.timeout); collectors.splice(index, 1); try { await collector.options.onStop?.(reason); } catch (e) { await collector.options.onStopError?.(reason, e); } } /**@internal */ async run(name, raw, client) { const collectors = this.values.get(name); if (!collectors) return; const data = (await RawEvents[name]?.(client, raw)) ?? raw; for (const i of collectors) { if (await i.options.filter(data)) { i.idle?.refresh(); const stop = (reason = 'unknown') => { return this.delete(i.options.event, i.nonce, reason); }; try { await i.options.run(data, stop); } catch (e) { await i.options.onRunError?.(data, e, stop); } break; } } } } exports.Collectors = Collectors;