From f1433a2d97fb037660a33dc577d005d7f9676dda Mon Sep 17 00:00:00 2001 From: Eduardo Londero Date: Thu, 11 Jul 2019 16:40:12 -0300 Subject: [PATCH] feat(Collector): add idle time for a Collector to stop itself (#2942) * Implement idle feature * Add typings * Minimal fixes * Make everything in Collector and not attached to ReactionCollector * set this._idletimeout to null when collector ends * also set this._timeout to null when collector ends --- src/structures/interfaces/Collector.js | 23 ++++++++++++++++++++++- typings/index.d.ts | 2 ++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/structures/interfaces/Collector.js b/src/structures/interfaces/Collector.js index 8e6d8463f..cadb44c65 100644 --- a/src/structures/interfaces/Collector.js +++ b/src/structures/interfaces/Collector.js @@ -16,6 +16,7 @@ const EventEmitter = require('events'); * Options to be applied to the collector. * @typedef {Object} CollectorOptions * @property {number} [time] How long to run the collector for in milliseconds + * @property {number} [idle] How long to stop the collector after inactivity in milliseconds * @property {boolean} [dispose=false] Whether to dispose data when it's deleted */ @@ -66,10 +67,18 @@ class Collector extends EventEmitter { */ this._timeout = null; + /** + * Timeout for cleanup due to inactivity + * @type {?Timeout} + * @private + */ + this._idletimeout = null; + this.handleCollect = this.handleCollect.bind(this); this.handleDispose = this.handleDispose.bind(this); if (options.time) this._timeout = this.client.setTimeout(() => this.stop('time'), options.time); + if (options.idle) this._idletimeout = this.client.setTimeout(() => this.stop('idle'), options.idle); } /** @@ -89,6 +98,11 @@ class Collector extends EventEmitter { * @param {...*} args The arguments emitted by the listener */ this.emit('collect', ...args); + + if (this._idletimeout) { + this.client.clearTimeout(this._idletimeout); + this._idletimeout = this.client.setTimeout(() => this.stop('idle'), this.options.idle); + } } this.checkEnd(); } @@ -155,7 +169,14 @@ class Collector extends EventEmitter { stop(reason = 'user') { if (this.ended) return; - if (this._timeout) this.client.clearTimeout(this._timeout); + if (this._timeout) { + this.client.clearTimeout(this._timeout); + this._timeout = null; + } + if (this._idletimeout) { + this.client.clearTimeout(this._idletimeout); + this._idletimeout = null; + } this.ended = true; /** diff --git a/typings/index.d.ts b/typings/index.d.ts index 42e8d2dc0..b97c62a82 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -350,6 +350,7 @@ declare module 'discord.js' { export abstract class Collector extends EventEmitter { constructor(client: Client, filter: CollectorFilter, options?: CollectorOptions); private _timeout: NodeJS.Timer | null; + private _idletimeout: NodeJS.Timer | null; public readonly client: Client; public collected: Collection; @@ -1759,6 +1760,7 @@ declare module 'discord.js' { interface CollectorOptions { time?: number; + idle?: number; dispose?: boolean; }