From 1d562f6997801d86e109d93d607f4567eea71498 Mon Sep 17 00:00:00 2001 From: ZiomaleQ Date: Thu, 7 Jan 2021 11:08:37 +0100 Subject: [PATCH] adding timeout to awaitAvailability --- src/structures/guild.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 771eea0..4fe7f9c 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -308,10 +308,11 @@ export class Guild extends Base { /** * Fulfills promise when guild becomes available + * @param timeout Configurable timeout to cancel the wait to safely remove listener. */ - async awaitAvailability(): Promise { + async awaitAvailability(timeout: number = 1000): Promise { return await new Promise((resolve, reject) => { - if(!this.unavailable) return; + if(!this.unavailable) resolve(this); const listener = (guild: Guild): void => { if (guild.id === this.id) { this.client.removeListener('guildLoaded', listener); @@ -319,6 +320,10 @@ export class Guild extends Base { } }; this.client.on('guildLoaded', listener); + setTimeout(() => { + this.client.removeListener('guildLoaded', listener); + reject(Error("Timeout. Guild didn't arrive in time.")); + }, timeout); }); } }