From 832683ca448369a38e12dcfb040496167124353b Mon Sep 17 00:00:00 2001 From: Qjuh <76154676+Qjuh@users.noreply.github.com> Date: Sat, 28 Sep 2024 14:30:59 +0200 Subject: [PATCH 1/2] refactor(GuildAuditLogsEntry): add typeguard for narrowing --- .../src/structures/GuildAuditLogsEntry.js | 10 ++++- packages/discord.js/typings/index.d.ts | 42 ++++++++++++------- packages/discord.js/typings/index.test-d.ts | 6 +-- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/packages/discord.js/src/structures/GuildAuditLogsEntry.js b/packages/discord.js/src/structures/GuildAuditLogsEntry.js index 53c5b3d3fcd3..2d6430b42659 100644 --- a/packages/discord.js/src/structures/GuildAuditLogsEntry.js +++ b/packages/discord.js/src/structures/GuildAuditLogsEntry.js @@ -14,7 +14,6 @@ const Partials = require('../util/Partials'); const { flatten } = require('../util/Util'); const Targets = { - All: 'All', Guild: 'Guild', GuildScheduledEvent: 'GuildScheduledEvent', Channel: 'Channel', @@ -503,6 +502,15 @@ class GuildAuditLogsEntry { return new Date(this.createdTimestamp); } + /** + * Checks whether this GuildAuditLogsEntry is of the specified {@link AuditLogEvent} type. + * @param {AuditLogEvent} action The type to check for + * @returns {boolean} + */ + isAction(action) { + return this.action === action; + } + toJSON() { return flatten(this, { createdTimestamp: true }); } diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 25eaab5839ed..ef6a42f5b334 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1485,9 +1485,9 @@ export class Guild extends AnonymousGuild { public editOnboarding(options: GuildOnboardingEditOptions): Promise; public editWelcomeScreen(options: WelcomeScreenEditOptions): Promise; public equals(guild: Guild): boolean; - public fetchAuditLogs( + public fetchAuditLogs( options?: GuildAuditLogsFetchOptions, - ): Promise>; + ): Promise>; public fetchIntegrations(): Promise>; public fetchOnboarding(): Promise; public fetchOwner(options?: BaseFetchOptions): Promise; @@ -1533,7 +1533,7 @@ export class Guild extends AnonymousGuild { public toJSON(): unknown; } -export class GuildAuditLogs { +export class GuildAuditLogs { private constructor(guild: Guild, data: RawGuildAuditLogData); private applicationCommands: Collection; private webhooks: Collection>; @@ -1545,36 +1545,40 @@ export class GuildAuditLogs { private constructor(guild: Guild, data: RawGuildAuditLogEntryData, logs?: GuildAuditLogs); public static Targets: GuildAuditLogsTargets; - public action: TResolvedType; + public action: TAction; public actionType: TActionType; public changes: AuditLogChange[]; public get createdAt(): Date; public get createdTimestamp(): number; public executorId: Snowflake | null; public executor: User | null; - public extra: TResolvedType extends keyof GuildAuditLogsEntryExtraField - ? GuildAuditLogsEntryExtraField[TResolvedType] - : null; + public extra: TAction extends keyof GuildAuditLogsEntryExtraField ? GuildAuditLogsEntryExtraField[TAction] : null; public id: Snowflake; public reason: string | null; public targetId: Snowflake | null; - public target: TTargetType extends keyof GuildAuditLogsEntryTargetField - ? GuildAuditLogsEntryTargetField[TTargetType] + public target: TTargetType extends keyof GuildAuditLogsEntryTargetField + ? GuildAuditLogsEntryTargetField[TTargetType] : Role | GuildEmoji | { id: Snowflake } | null; public targetType: TTargetType; public static actionType(action: AuditLogEvent): GuildAuditLogsActionType; public static targetType(target: AuditLogEvent): GuildAuditLogsTargetType; + public isAction( + action: TCheckAction, + ): this is GuildAuditLogsEntry< + TCheckAction, + TCheckAction extends keyof GuildAuditLogsTypes ? GuildAuditLogsTypes[TCheckAction][1] : 'All', + TCheckAction extends keyof GuildAuditLogsTypes ? GuildAuditLogsTypes[TCheckAction][0] : 'Unknown' + >; public toJSON(): unknown; } @@ -5885,6 +5889,10 @@ interface GuildAuditLogsTypes { [AuditLogEvent.OnboardingPromptDelete]: ['GuildOnboardingPrompt', 'Delete']; [AuditLogEvent.OnboardingCreate]: ['GuildOnboarding', 'Create']; [AuditLogEvent.OnboardingUpdate]: ['GuildOnboarding', 'Update']; + // [AuditLogEvent.CreatorMonetizationRequestCreated]: ['CreatorMonetizationRequest', 'Create']; + // [AuditLogEvent.CreatorMonetizationTermsAccepted]: ['CreatorMonetizationRequest', 'Update']; + // [AuditLogEvent.HomeSettingsCreate]: ['HomeSetting', 'Create']; + // [AuditLogEvent.HomeSettingsUpdate]: ['HomeSetting', 'Update']; } export type GuildAuditLogsActionType = GuildAuditLogsTypes[keyof GuildAuditLogsTypes][1] | 'All'; @@ -5935,12 +5943,14 @@ export interface GuildAuditLogsEntryExtraField { }; } -export interface GuildAuditLogsEntryTargetField { +export interface GuildAuditLogsEntryTargetField { User: User | null; Guild: Guild; Webhook: Webhook; Invite: Invite; - Message: TActionType extends AuditLogEvent.MessageBulkDelete ? Guild | { id: Snowflake } : User; + Emoji: GuildEmoji; + Role: Role; + Message: TAction extends AuditLogEvent.MessageBulkDelete ? Guild | { id: Snowflake } : User; Integration: Integration; Channel: NonThreadGuildBasedChannel | { id: Snowflake; [x: string]: unknown }; Thread: AnyThreadChannel | { id: Snowflake; [x: string]: unknown }; @@ -5962,7 +5972,7 @@ export interface GuildAuditLogsFetchOptions>>( guild.fetchAuditLogs({ type: AuditLogEvent.IntegrationUpdate }), ); -expectType>>(guild.fetchAuditLogs({ type: null })); +expectType>>(guild.fetchAuditLogs({ type: null })); expectType>>(guild.fetchAuditLogs()); expectType | undefined>>( @@ -2158,10 +2158,10 @@ expectAssignable al.entries.first()), ); -expectType | undefined>>( +expectType | undefined>>( guild.fetchAuditLogs({ type: null }).then(al => al.entries.first()), ); -expectType | undefined>>( +expectType | undefined>>( guild.fetchAuditLogs().then(al => al.entries.first()), ); From 941e8e1d1060d94ffbbd63edeedbfdb0630b3c4a Mon Sep 17 00:00:00 2001 From: Qjuh <76154676+Qjuh@users.noreply.github.com> Date: Sat, 28 Sep 2024 14:37:36 +0200 Subject: [PATCH 2/2] fix: remove irrelevante change --- packages/discord.js/typings/index.d.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index ef6a42f5b334..c8a0f04c2fe0 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -5889,10 +5889,6 @@ interface GuildAuditLogsTypes { [AuditLogEvent.OnboardingPromptDelete]: ['GuildOnboardingPrompt', 'Delete']; [AuditLogEvent.OnboardingCreate]: ['GuildOnboarding', 'Create']; [AuditLogEvent.OnboardingUpdate]: ['GuildOnboarding', 'Update']; - // [AuditLogEvent.CreatorMonetizationRequestCreated]: ['CreatorMonetizationRequest', 'Create']; - // [AuditLogEvent.CreatorMonetizationTermsAccepted]: ['CreatorMonetizationRequest', 'Update']; - // [AuditLogEvent.HomeSettingsCreate]: ['HomeSetting', 'Create']; - // [AuditLogEvent.HomeSettingsUpdate]: ['HomeSetting', 'Update']; } export type GuildAuditLogsActionType = GuildAuditLogsTypes[keyof GuildAuditLogsTypes][1] | 'All';