mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
feat(Collector): add idle option (#3746)
This commit is contained in:
@@ -13,6 +13,7 @@ const EventEmitter = require('events').EventEmitter;
|
||||
* Options to be applied to the collector.
|
||||
* @typedef {Object} CollectorOptions
|
||||
* @property {number} [time] How long to run the collector for
|
||||
* @property {number} [idle] How long to stop the collector after inactivity in milliseconds
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -62,6 +63,13 @@ class Collector extends EventEmitter {
|
||||
*/
|
||||
this._timeout = null;
|
||||
|
||||
/**
|
||||
* Timeout for cleanup due to inactivity
|
||||
* @type {?Timeout}
|
||||
* @private
|
||||
*/
|
||||
this._idletimeout = null;
|
||||
|
||||
/**
|
||||
* Call this to handle an event as a collectable element
|
||||
* Accepts any event data as parameters
|
||||
@@ -70,6 +78,7 @@ class Collector extends EventEmitter {
|
||||
*/
|
||||
this.listener = this._handle.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 {Collector} collector The collector
|
||||
*/
|
||||
this.emit('collect', collect.value, this);
|
||||
|
||||
if (this._idletimeout) {
|
||||
this.client.clearTimeout(this._idletimeout);
|
||||
this._idletimeout = this.client.setTimeout(() => this.stop('idle'), this.options.idle);
|
||||
}
|
||||
}
|
||||
|
||||
const post = this.postCheck(...args);
|
||||
@@ -136,7 +150,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;
|
||||
this.cleanup();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user