adding timeout to awaitAvailability

This commit is contained in:
ZiomaleQ 2021-01-07 11:08:37 +01:00
parent 2fadcfa407
commit 1d562f6997

View file

@ -308,10 +308,11 @@ export class Guild extends Base {
/** /**
* Fulfills promise when guild becomes available * Fulfills promise when guild becomes available
* @param timeout Configurable timeout to cancel the wait to safely remove listener.
*/ */
async awaitAvailability(): Promise<Guild> { async awaitAvailability(timeout: number = 1000): Promise<Guild> {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
if(!this.unavailable) return; if(!this.unavailable) resolve(this);
const listener = (guild: Guild): void => { const listener = (guild: Guild): void => {
if (guild.id === this.id) { if (guild.id === this.id) {
this.client.removeListener('guildLoaded', listener); this.client.removeListener('guildLoaded', listener);
@ -319,6 +320,10 @@ export class Guild extends Base {
} }
}; };
this.client.on('guildLoaded', listener); this.client.on('guildLoaded', listener);
setTimeout(() => {
this.client.removeListener('guildLoaded', listener);
reject(Error("Timeout. Guild didn't arrive in time."));
}, timeout);
}); });
} }
} }