Skip to content

Commit

Permalink
feat(Collector): add ignore event (#7644)
Browse files Browse the repository at this point in the history
Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com>
  • Loading branch information
ImRodry and imranbarbhuiya committed Jun 5, 2022
1 parent 349766d commit 5244fe3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
40 changes: 25 additions & 15 deletions packages/discord.js/src/structures/interfaces/Collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,31 @@ class Collector extends EventEmitter {
* @emits Collector#collect
*/
async handleCollect(...args) {
const collect = await this.collect(...args);

if (collect && (await this.filter(...args, this.collected))) {
this.collected.set(collect, args[0]);

/**
* Emitted whenever an element is collected.
* @event Collector#collect
* @param {...*} args The arguments emitted by the listener
*/
this.emit('collect', ...args);

if (this._idletimeout) {
clearTimeout(this._idletimeout);
this._idletimeout = setTimeout(() => this.stop('idle'), this.options.idle).unref();
const collectedId = await this.collect(...args);

if (collectedId) {
const filterResult = await this.filter(...args, this.collected);
if (filterResult) {
this.collected.set(collectedId, args[0]);

/**
* Emitted whenever an element is collected.
* @event Collector#collect
* @param {...*} args The arguments emitted by the listener
*/
this.emit('collect', ...args);

if (this._idletimeout) {
clearTimeout(this._idletimeout);
this._idletimeout = setTimeout(() => this.stop('idle'), this.options.idle).unref();
}
} else {
/**
* Emitted whenever an element is not collected by the collector.
* @event Collector#ignore
* @param {...*} args The arguments emitted by the listener
*/
this.emit('ignore', ...args);
}
}
this.checkEnd();
Expand Down
15 changes: 11 additions & 4 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ export { Collection } from '@discordjs/collection';

export interface CollectorEventTypes<K, V, F extends unknown[] = []> {
collect: [V, ...F];
ignore: [V, ...F];
dispose: [V, ...F];
end: [collected: Collection<K, V>, reason: string];
}
Expand Down Expand Up @@ -1522,11 +1523,11 @@ export class InteractionCollector<T extends Interaction> extends Collector<Snowf
public collect(interaction: Interaction): Snowflake;
public empty(): void;
public dispose(interaction: Interaction): Snowflake;
public on(event: 'collect' | 'dispose', listener: (interaction: T) => Awaitable<void>): this;
public on(event: 'collect' | 'dispose' | 'ignore', listener: (interaction: T) => Awaitable<void>): this;
public on(event: 'end', listener: (collected: Collection<Snowflake, T>, reason: string) => Awaitable<void>): this;
public on(event: string, listener: (...args: any[]) => Awaitable<void>): this;

public once(event: 'collect' | 'dispose', listener: (interaction: T) => Awaitable<void>): this;
public once(event: 'collect' | 'dispose' | 'ignore', listener: (interaction: T) => Awaitable<void>): this;
public once(event: 'end', listener: (collected: Collection<Snowflake, T>, reason: string) => Awaitable<void>): this;
public once(event: string, listener: (...args: any[]) => Awaitable<void>): this;
}
Expand Down Expand Up @@ -2024,11 +2025,17 @@ export class ReactionCollector extends Collector<Snowflake | string, MessageReac
public dispose(reaction: MessageReaction, user: User): Snowflake | string | null;
public empty(): void;

public on(event: 'collect' | 'dispose' | 'remove', listener: (reaction: MessageReaction, user: User) => void): this;
public on(
event: 'collect' | 'dispose' | 'remove' | 'ignore',
listener: (reaction: MessageReaction, user: User) => void,
): this;
public on(event: 'end', listener: (collected: Collection<Snowflake, MessageReaction>, reason: string) => void): this;
public on(event: string, listener: (...args: any[]) => void): this;

public once(event: 'collect' | 'dispose' | 'remove', listener: (reaction: MessageReaction, user: User) => void): this;
public once(
event: 'collect' | 'dispose' | 'remove' | 'ignore',
listener: (reaction: MessageReaction, user: User) => void,
): this;
public once(
event: 'end',
listener: (collected: Collection<Snowflake, MessageReaction>, reason: string) => void,
Expand Down

0 comments on commit 5244fe3

Please sign in to comment.