feat(Collector): add ignore event (#7644)

Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com>
This commit is contained in:
Rodry
2022-06-05 17:29:13 +01:00
committed by GitHub
parent 349766dd69
commit 5244fe3c1c
2 changed files with 33 additions and 16 deletions

View File

@@ -103,21 +103,31 @@ class Collector extends EventEmitter {
* @emits Collector#collect
*/
async handleCollect(...args) {
const collect = await this.collect(...args);
const collectedId = await this.collect(...args);
if (collect && (await this.filter(...args, this.collected))) {
this.collected.set(collect, args[0]);
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);
/**
* 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();
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();

View File

@@ -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];
}
@@ -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;
}
@@ -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,