refactor(Collector): make endReason a getter (#5271)

This commit is contained in:
Jan
2021-03-28 15:29:39 +02:00
committed by GitHub
parent 09846c725b
commit 8d14ee3540
4 changed files with 11 additions and 16 deletions

View File

@@ -90,12 +90,7 @@ class MessageCollector extends Collector {
return message.channel.id === this.channel.id ? message.id : null; return message.channel.id === this.channel.id ? message.id : null;
} }
/** get endReason() {
* Checks after un/collection to see if the collector is done.
* @returns {?string}
* @private
*/
endReason() {
if (this.options.max && this.collected.size >= this.options.max) return 'limit'; if (this.options.max && this.collected.size >= this.options.max) return 'limit';
if (this.options.maxProcessed && this.received === this.options.maxProcessed) return 'processedLimit'; if (this.options.maxProcessed && this.received === this.options.maxProcessed) return 'processedLimit';
return null; return null;

View File

@@ -146,7 +146,7 @@ class ReactionCollector extends Collector {
this.checkEnd(); this.checkEnd();
} }
endReason() { get endReason() {
if (this.options.max && this.total >= this.options.max) return 'limit'; if (this.options.max && this.total >= this.options.max) return 'limit';
if (this.options.maxEmojis && this.collected.size >= this.options.maxEmojis) return 'emojiLimit'; if (this.options.maxEmojis && this.collected.size >= this.options.maxEmojis) return 'emojiLimit';
if (this.options.maxUsers && this.users.size >= this.options.maxUsers) return 'userLimit'; if (this.options.maxUsers && this.users.size >= this.options.maxUsers) return 'userLimit';

View File

@@ -214,7 +214,7 @@ class Collector extends EventEmitter {
* Checks whether the collector should end, and if so, ends it. * Checks whether the collector should end, and if so, ends it.
*/ */
checkEnd() { checkEnd() {
const reason = this.endReason(); const reason = this.endReason;
if (reason) this.stop(reason); if (reason) this.stop(reason);
} }
@@ -253,7 +253,7 @@ class Collector extends EventEmitter {
return Util.flatten(this); return Util.flatten(this);
} }
/* eslint-disable no-empty-function, valid-jsdoc */ /* eslint-disable no-empty-function */
/** /**
* Handles incoming events from the `handleCollect` function. Returns null if the event should not * Handles incoming events from the `handleCollect` function. Returns null if the event should not
* be collected, or returns an object describing the data that should be stored. * be collected, or returns an object describing the data that should be stored.
@@ -273,14 +273,14 @@ class Collector extends EventEmitter {
* @abstract * @abstract
*/ */
dispose() {} dispose() {}
/* eslint-enable no-empty-function */
/** /**
* The reason this collector has ended or will end with. * The reason this collector has ended with, or null if it hasn't ended yet
* @returns {?string} Reason to end the collector, if any * @name Collector#endReason
* @type {?string}
* @abstract * @abstract
*/ */
endReason() {}
/* eslint-enable no-empty-function, valid-jsdoc */
} }
module.exports = Collector; module.exports = Collector;

6
typings/index.d.ts vendored
View File

@@ -293,6 +293,7 @@ declare module 'discord.js' {
public readonly client: Client; public readonly client: Client;
public collected: Collection<K, V>; public collected: Collection<K, V>;
public ended: boolean; public ended: boolean;
public abstract endReason: string | null;
public filter: CollectorFilter; public filter: CollectorFilter;
public readonly next: Promise<V>; public readonly next: Promise<V>;
public options: CollectorOptions; public options: CollectorOptions;
@@ -307,7 +308,6 @@ declare module 'discord.js' {
protected listener: (...args: any[]) => void; protected listener: (...args: any[]) => void;
public abstract collect(...args: any[]): K; public abstract collect(...args: any[]): K;
public abstract dispose(...args: any[]): K; public abstract dispose(...args: any[]): K;
public abstract endReason(): void;
public on(event: 'collect' | 'dispose', listener: (...args: any[]) => void): this; public on(event: 'collect' | 'dispose', listener: (...args: any[]) => void): this;
public on(event: 'end', listener: (collected: Collection<K, V>, reason: string) => void): this; public on(event: 'end', listener: (collected: Collection<K, V>, reason: string) => void): this;
@@ -1046,12 +1046,12 @@ declare module 'discord.js' {
private _handleGuildDeletion(guild: Guild): void; private _handleGuildDeletion(guild: Guild): void;
public channel: Channel; public channel: Channel;
public readonly endReason: string | null;
public options: MessageCollectorOptions; public options: MessageCollectorOptions;
public received: number; public received: number;
public collect(message: Message): Snowflake; public collect(message: Message): Snowflake;
public dispose(message: Message): Snowflake; public dispose(message: Message): Snowflake;
public endReason(): string;
} }
export class MessageEmbed { export class MessageEmbed {
@@ -1223,6 +1223,7 @@ declare module 'discord.js' {
private _handleGuildDeletion(guild: Guild): void; private _handleGuildDeletion(guild: Guild): void;
private _handleMessageDeletion(message: Message): void; private _handleMessageDeletion(message: Message): void;
public readonly endReason: string | null;
public message: Message; public message: Message;
public options: ReactionCollectorOptions; public options: ReactionCollectorOptions;
public total: number; public total: number;
@@ -1233,7 +1234,6 @@ declare module 'discord.js' {
public collect(reaction: MessageReaction): Snowflake | string; public collect(reaction: MessageReaction): Snowflake | string;
public dispose(reaction: MessageReaction, user: User): Snowflake | string; public dispose(reaction: MessageReaction, user: User): Snowflake | string;
public empty(): void; public empty(): void;
public endReason(): string | null;
public on(event: 'collect' | 'dispose' | 'remove', listener: (reaction: MessageReaction, user: User) => void): this; public on(event: 'collect' | 'dispose' | 'remove', listener: (reaction: MessageReaction, user: User) => void): this;
public on( public on(