Merge pull request #82 from ZiomaleQ/main

Guild awaitAvailiable method and removing redundant code in guild structure
This commit is contained in:
DjDeveloper 2021-01-07 15:44:56 +05:30 committed by GitHub
commit a630e9466c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 40 deletions

View File

@ -168,8 +168,9 @@ export class Guild extends Base {
constructor(client: Client, data: GuildPayload) {
super(client, data)
this.id = data.id
this.bans = new GuildBans(client, this)
this.unavailable = data.unavailable
this.readFromData(data)
this.bans = new GuildBans(client, this)
this.members = new MembersManager(this.client, this)
this.voiceStates = new GuildVoiceStatesManager(client, this)
this.presences = new GuildPresencesManager(client, this)
@ -181,45 +182,6 @@ export class Guild extends Base {
this.roles = new RolesManager(this.client, this)
this.emojis = new GuildEmojisManager(this.client, this.client.emojis, this)
this.invites = new InviteManager(this.client, this)
if (!this.unavailable) {
this.name = data.name
this.icon = data.icon
this.iconHash = data.icon_hash
this.splash = data.splash
this.discoverySplash = data.discovery_splash
this.owner = data.owner
this.ownerID = data.owner_id
this.permissions = data.permissions
this.region = data.region
this.afkTimeout = data.afk_timeout
this.afkChannelID = data.afk_channel_id
this.widgetEnabled = data.widget_enabled
this.widgetChannelID = data.widget_channel_id
this.verificationLevel = data.verification_level
this.defaultMessageNotifications = data.default_message_notifications
this.explicitContentFilter = data.explicit_content_filter
this.features = data.features
this.mfaLevel = data.mfa_level
this.systemChannelID = data.system_channel_id
this.systemChannelFlags = data.system_channel_flags
this.rulesChannelID = data.rules_channel_id
this.joinedAt = data.joined_at
this.large = data.large
this.memberCount = data.member_count
this.maxPresences = data.max_presences
this.maxMembers = data.max_members
this.vanityURLCode = data.vanity_url_code
this.description = data.description
this.banner = data.banner
this.premiumTier = data.premium_tier
this.premiumSubscriptionCount = data.premium_subscription_count
this.preferredLocale = data.preferred_locale
this.publicUpdatesChannelID = data.public_updates_channel_id
this.maxVideoChannelUsers = data.max_video_channel_users
this.approximateNumberCount = data.approximate_number_count
this.approximatePresenceCount = data.approximate_presence_count
}
}
readFromData(data: GuildPayload): void {
@ -343,6 +305,27 @@ 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(timeout: number = 1000): Promise<Guild> {
return await new Promise((resolve, reject) => {
if(!this.unavailable) resolve(this);
const listener = (guild: Guild): void => {
if (guild.id === this.id) {
this.client.removeListener('guildLoaded', listener);
resolve(this);
}
};
this.client.on('guildLoaded', listener);
setTimeout(() => {
this.client.removeListener('guildLoaded', listener);
reject(Error("Timeout. Guild didn't arrive in time."));
}, timeout);
});
}
}
export class GuildIntegration extends Base {