From 2b480cb14e6f52855efcb372da7fb455c15b13b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Rom=C3=A1n?= Date: Fri, 7 Jan 2022 23:39:49 +0100 Subject: [PATCH] feat(Collector): yield all collected values (#7073) --- .../src/structures/interfaces/Collector.js | 2 +- packages/discord.js/typings/index.d.ts | 2 +- packages/discord.js/typings/index.test-d.ts | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/discord.js/src/structures/interfaces/Collector.js b/packages/discord.js/src/structures/interfaces/Collector.js index c0793fd03..8f232d7d5 100644 --- a/packages/discord.js/src/structures/interfaces/Collector.js +++ b/packages/discord.js/src/structures/interfaces/Collector.js @@ -236,7 +236,7 @@ class Collector extends EventEmitter { */ async *[Symbol.asyncIterator]() { const queue = []; - const onCollect = item => queue.push(item); + const onCollect = (...item) => queue.push(item); this.on('collect', onCollect); try { diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index b782f755d..5ba158fa7 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -670,7 +670,7 @@ export abstract class Collector extends EventEmi public handleDispose(...args: unknown[]): Promise; public stop(reason?: string): void; public resetTimer(options?: CollectorResetTimerOptions): void; - public [Symbol.asyncIterator](): AsyncIterableIterator; + public [Symbol.asyncIterator](): AsyncIterableIterator<[V, ...F]>; public toJSON(): unknown; protected listener: (...args: any[]) => void; diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index d95161eea..52477639b 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -794,11 +794,23 @@ messageCollector.on('collect', (...args) => { expectType<[Message]>(args); }); +(async () => { + for await (const value of messageCollector) { + expectType<[Message]>(value); + } +})(); + declare const reactionCollector: ReactionCollector; reactionCollector.on('dispose', (...args) => { expectType<[MessageReaction, User]>(args); }); +(async () => { + for await (const value of reactionCollector) { + expectType<[MessageReaction, User]>(value); + } +})(); + // Make sure the properties are typed correctly, and that no backwards properties // (K -> V and V -> K) exist: expectType<'messageCreate'>(Constants.Events.MESSAGE_CREATE); @@ -1169,6 +1181,12 @@ collector.on('end', (collection, reason) => { expectType(reason); }); +(async () => { + for await (const value of collector) { + expectType<[Interaction, ...string[]]>(value); + } +})(); + expectType>(shard.eval(c => c.readyTimestamp)); // Test audit logs