From acf724e691678998cb6774846454913a183c9614 Mon Sep 17 00:00:00 2001 From: matthewfripp <50251454+matthewfripp@users.noreply.github.com> Date: Mon, 24 Feb 2020 17:17:24 +0000 Subject: [PATCH] feat(Collector): Addition of resetTimer() (#3825) * feat(Collector): Addition of resetTimer() * typings --- src/structures/interfaces/Collector.js | 17 +++++++++++++++++ typings/index.d.ts | 1 + 2 files changed, 18 insertions(+) diff --git a/src/structures/interfaces/Collector.js b/src/structures/interfaces/Collector.js index 3cd5d387e..6231605e7 100644 --- a/src/structures/interfaces/Collector.js +++ b/src/structures/interfaces/Collector.js @@ -188,6 +188,23 @@ class Collector extends EventEmitter { this.emit('end', this.collected, reason); } + /** + * Resets the collectors timeout and idle timer. + * @param {Object} [options] Options + * @param {number} [options.time] How long to run the collector for in milliseconds + * @param {number} [options.idle] How long to stop the collector after inactivity in milliseconds + */ + resetTimer({ time, idle } = {}) { + if (this._timeout) { + this.client.clearTimeout(this._timeout); + this._timeout = this.client.setTimeout(() => this.stop('time'), time || this.options.time); + } + if (this._idletimeout) { + this.client.clearTimeout(this._idletimeout); + this._idletimeout = this.client.setTimeout(() => this.stop('idle'), idle || this.options.idle); + } + } + /** * Checks whether the collector should end, and if so, ends it. */ diff --git a/typings/index.d.ts b/typings/index.d.ts index 0788fd057..1b12e82d2 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -337,6 +337,7 @@ declare module 'discord.js' { public handleCollect(...args: any[]): void; public handleDispose(...args: any[]): void; public stop(reason?: string): void; + public resetTimer(options?: { time?: number, idle?: number }): void; public [Symbol.asyncIterator](): AsyncIterableIterator; public toJSON(): object;