Skip to content

Commit

Permalink
feat(Collector): add lastCollectedTimestamp (#9044)
Browse files Browse the repository at this point in the history
* feat(Collector): add lastCollectedTimestamp

* docs: mark props as nullable

Co-authored-by: Synbulat Biishev <[email protected]>

* refactor: assign property in constructor

---------

Co-authored-by: Synbulat Biishev <[email protected]>
Co-authored-by: Vlad Frangu <[email protected]>
  • Loading branch information
3 people authored Mar 12, 2023
1 parent 334a512 commit 4458a13
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/discord.js/src/structures/interfaces/Collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ class Collector extends EventEmitter {

if (options.time) this._timeout = setTimeout(() => this.stop('time'), options.time).unref();
if (options.idle) this._idletimeout = setTimeout(() => this.stop('idle'), options.idle).unref();

/**
* The timestamp at which this collector last collected an item
* @type {?number}
*/
this.lastCollectedTimestamp = null;
}

/**
* The Date at which this collector last collected an item
* @type {?Date}
*/
get lastCollectedAt() {
return this.lastCollectedTimestamp && new Date(this.lastCollectedTimestamp);
}

/**
Expand All @@ -118,6 +132,7 @@ class Collector extends EventEmitter {
*/
this.emit('collect', ...args);

this.lastCollectedTimestamp = Date.now();
if (this._idletimeout) {
clearTimeout(this._idletimeout);
this._idletimeout = setTimeout(() => this.stop('idle'), this.options.idle).unref();
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,8 @@ export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmi

public readonly client: Client;
public collected: Collection<K, V>;
public lastCollectedTimestamp: number | null;
public get lastCollectedAt(): Date | null;
public ended: boolean;
public get endReason(): string | null;
public filter: CollectorFilter<[V, ...F]>;
Expand Down

2 comments on commit 4458a13

@vercel
Copy link

@vercel vercel bot commented on 4458a13 Mar 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 4458a13 Mar 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.