From 6307a4b7f23877e1679ff30e16704a8ce3020e44 Mon Sep 17 00:00:00 2001 From: Elysia <71698422+aiko-chan-ai@users.noreply.github.com> Date: Tue, 30 Jan 2024 12:26:40 +0700 Subject: [PATCH] fix: temporary invite & feat: InviteFlags --- src/client/Client.js | 7 ++++++- src/client/websocket/handlers/READY.js | 1 + src/structures/ClientUser.js | 10 ++++++--- src/structures/Invite.js | 11 ++++++++++ src/util/InviteFlags.js | 29 ++++++++++++++++++++++++++ typings/index.d.ts | 12 +++++++++-- 6 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/util/InviteFlags.js diff --git a/src/client/Client.js b/src/client/Client.js index 11c7b1c..6c5b138 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -553,6 +553,11 @@ class Client extends BaseClient { this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Joined`); // Guild if (i.guild?.id) { + const guild = this.guilds.cache.get(i.guild?.id); + if (i.flags.has('GUEST')) { + this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Guest invite`); + return guild; + } if (options.bypassOnboarding) { const onboardingData = await this.api.guilds[i.guild?.id].onboarding.get(); // Onboarding @@ -607,7 +612,7 @@ class Client extends BaseClient { this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Bypassed verify`); } } - return this.guilds.cache.get(i.guild?.id); + return guild; } else { return this.channels.cache.has(i.channelId); } diff --git a/src/client/websocket/handlers/READY.js b/src/client/websocket/handlers/READY.js index c39cc6d..df770f7 100644 --- a/src/client/websocket/handlers/READY.js +++ b/src/client/websocket/handlers/READY.js @@ -43,6 +43,7 @@ module.exports = (client, { d: data }, shard) => { const guild = client.guilds.cache.get(gSetting.guild_id); if (guild) guild.settings._patch(gSetting); } + // Todo: data.auth_session_id_hash if (largeGuilds.length) { client.ws.broadcast({ diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index e251d77..4c8a638 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -47,17 +47,21 @@ class ClientUser extends User { if ('purchased_flags' in data) { /** * Purchased state of the client user. - * @type {?PurchasedFlags} + * @type {Readonly} */ - this.purchasedFlags = new PurchasedFlags(data.purchased_flags || 0); + this.purchasedFlags = new PurchasedFlags(data.purchased_flags || 0).freeze(); + } else { + this.purchasedFlags = new PurchasedFlags().freeze(); } if ('premium_usage_flags' in data) { /** * Premium usage state of the client user. - * @type {?PremiumUsageFlags} + * @type {Readonly} */ this.premiumUsageFlags = new PremiumUsageFlags(data.premium_usage_flags || 0); + } else { + this.premiumUsageFlags = new PremiumUsageFlags().freeze(); } if ('phone' in data) { diff --git a/src/structures/Invite.js b/src/structures/Invite.js index c033171..d308e7f 100644 --- a/src/structures/Invite.js +++ b/src/structures/Invite.js @@ -6,6 +6,7 @@ const IntegrationApplication = require('./IntegrationApplication'); const InviteStageInstance = require('./InviteStageInstance'); const { Error } = require('../errors'); const { Endpoints } = require('../util/Constants'); +const InviteFlags = require('../util/InviteFlags'); const Permissions = require('../util/Permissions'); // TODO: Convert `inviter` and `channel` in this class to a getter. @@ -242,6 +243,16 @@ class Invite extends Base { } else { this.guildScheduledEvent ??= null; } + + if ('flags' in data) { + /** + * The flags that are applied to the invite. + * @type {?Readonly} + */ + this.flags = new InviteFlags(data.flags).freeze(); + } else { + this.flags ??= new InviteFlags().freeze(); + } } /** diff --git a/src/util/InviteFlags.js b/src/util/InviteFlags.js new file mode 100644 index 00000000..6066a74 --- /dev/null +++ b/src/util/InviteFlags.js @@ -0,0 +1,29 @@ +'use strict'; + +const BitField = require('./BitField'); + +/** + * Data structure that makes it easy to interact with an {@link InviteFlags#flags} bitfield. + * @extends {BitField} + */ +class InviteFlags extends BitField {} + +/** + * @name InviteFlags + * @kind constructor + * @memberof InviteFlags + * @param {BitFieldResolvable} [bits=0] Bit(s) to read from + */ + +/** + * Numeric the Discord invite flags. All available properties: + * * `GUEST` + * * `VIEWED` + * @type {Object} + */ +InviteFlags.FLAGS = { + GUEST: 1 << 0, + VIEWED: 1 << 1, +}; + +module.exports = InviteFlags; diff --git a/typings/index.d.ts b/typings/index.d.ts index 2859d0a..a985753 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -334,6 +334,11 @@ export class PremiumUsageFlags extends BitField { public static resolve(bit?: BitFieldResolvable): number; } +export class InviteFlags extends BitField { + public static FLAGS: Record; + public static resolve(bit?: BitFieldResolvable): number; +} + export abstract class AnonymousGuild extends BaseGuild { protected constructor(client: Client, data: RawAnonymousGuildData, immediatePatch?: boolean); public banner: string | null; @@ -873,8 +878,8 @@ export class ClientUser extends User { public setPresence(data: PresenceData): ClientPresence; public setStatus(status: PresenceStatusData, shardId?: number | number[]): ClientPresence; public setUsername(username: string, password: string): Promise; - public purchasedFlags: PurchasedFlags; - public premiumUsageFlags: PremiumUsageFlags; + public purchasedFlags: Readonly; + public premiumUsageFlags: Readonly; public phone: string | null; public nsfwAllowed?: boolean; public email: string | null; @@ -1751,6 +1756,7 @@ export class Invite extends Base { public static INVITES_PATTERN: RegExp; public stageInstance: InviteStageInstance | null; public guildScheduledEvent: GuildScheduledEvent | null; + public flags: Readonly; } export class InviteStageInstance extends Base { @@ -4365,6 +4371,8 @@ export type PurchasedFlagsString = 'NITRO_CLASSIC' | 'NITRO' | 'GUILD_BOOST' | ' export type PremiumUsageFlagsString = 'PREMIUM_DISCRIMINATOR' | 'ANIMATED_AVATAR' | 'PROFILE_BANNER'; +export type InviteFlagsString = 'GUEST' | 'VIEWED'; + export type ActivitiesOptions = Omit; export interface ActivityOptions {