Skip to content

Commit

Permalink
refactor(GuildAuditLogsEntry): add typeguard for narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
Qjuh committed Sep 28, 2024
1 parent 6c77fee commit 832683c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
10 changes: 9 additions & 1 deletion packages/discord.js/src/structures/GuildAuditLogsEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const Partials = require('../util/Partials');
const { flatten } = require('../util/Util');

const Targets = {
All: 'All',
Guild: 'Guild',
GuildScheduledEvent: 'GuildScheduledEvent',
Channel: 'Channel',
Expand Down Expand Up @@ -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 });
}
Expand Down
42 changes: 26 additions & 16 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1485,9 +1485,9 @@ export class Guild extends AnonymousGuild {
public editOnboarding(options: GuildOnboardingEditOptions): Promise<GuildOnboarding>;
public editWelcomeScreen(options: WelcomeScreenEditOptions): Promise<WelcomeScreen>;
public equals(guild: Guild): boolean;
public fetchAuditLogs<Event extends GuildAuditLogsResolvable = null>(
public fetchAuditLogs<Event extends GuildAuditLogsResolvable = AuditLogEvent>(
options?: GuildAuditLogsFetchOptions<Event>,
): Promise<GuildAuditLogs<Event>>;
): Promise<GuildAuditLogs<Event extends null ? AuditLogEvent : Event>>;
public fetchIntegrations(): Promise<Collection<Snowflake | string, Integration>>;
public fetchOnboarding(): Promise<GuildOnboarding>;
public fetchOwner(options?: BaseFetchOptions): Promise<GuildMember>;
Expand Down Expand Up @@ -1533,7 +1533,7 @@ export class Guild extends AnonymousGuild {
public toJSON(): unknown;
}

export class GuildAuditLogs<Event extends GuildAuditLogsResolvable = AuditLogEvent> {
export class GuildAuditLogs<Event extends AuditLogEvent = AuditLogEvent> {
private constructor(guild: Guild, data: RawGuildAuditLogData);
private applicationCommands: Collection<Snowflake, ApplicationCommand>;
private webhooks: Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>;
Expand All @@ -1545,36 +1545,40 @@ export class GuildAuditLogs<Event extends GuildAuditLogsResolvable = AuditLogEve
}

export class GuildAuditLogsEntry<
TAction extends GuildAuditLogsResolvable = AuditLogEvent,
TAction extends AuditLogEvent = AuditLogEvent,
TActionType extends GuildAuditLogsActionType = TAction extends keyof GuildAuditLogsTypes
? GuildAuditLogsTypes[TAction][1]
: GuildAuditLogsActionType,
: 'All',
TTargetType extends GuildAuditLogsTargetType = TAction extends keyof GuildAuditLogsTypes
? GuildAuditLogsTypes[TAction][0]
: GuildAuditLogsTargetType,
TResolvedType = TAction extends null ? AuditLogEvent : TAction,
: 'Unknown',
> {
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<TActionType>
? GuildAuditLogsEntryTargetField<TActionType>[TTargetType]
public target: TTargetType extends keyof GuildAuditLogsEntryTargetField<TAction>
? GuildAuditLogsEntryTargetField<TAction>[TTargetType]
: Role | GuildEmoji | { id: Snowflake } | null;
public targetType: TTargetType;
public static actionType(action: AuditLogEvent): GuildAuditLogsActionType;
public static targetType(target: AuditLogEvent): GuildAuditLogsTargetType;
public isAction<TCheckAction extends TAction>(
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;
}

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -5935,12 +5943,14 @@ export interface GuildAuditLogsEntryExtraField {
};
}

export interface GuildAuditLogsEntryTargetField<TActionType extends GuildAuditLogsActionType> {
export interface GuildAuditLogsEntryTargetField<TAction extends AuditLogEvent> {
User: User | null;
Guild: Guild;
Webhook: Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>;
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 };
Expand All @@ -5962,7 +5972,7 @@ export interface GuildAuditLogsFetchOptions<Event extends GuildAuditLogsResolvab

export type GuildAuditLogsResolvable = AuditLogEvent | null;

export type GuildAuditLogsTargetType = GuildAuditLogsTypes[keyof GuildAuditLogsTypes][0] | 'All' | 'Unknown';
export type GuildAuditLogsTargetType = GuildAuditLogsTypes[keyof GuildAuditLogsTypes][0] | 'Unknown';

export type GuildAuditLogsTargets = {
[key in GuildAuditLogsTargetType]: GuildAuditLogsTargetType;
Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ expectType<Promise<GuildAuditLogs<AuditLogEvent.IntegrationUpdate>>>(
guild.fetchAuditLogs({ type: AuditLogEvent.IntegrationUpdate }),
);

expectType<Promise<GuildAuditLogs<null>>>(guild.fetchAuditLogs({ type: null }));
expectType<Promise<GuildAuditLogs<AuditLogEvent>>>(guild.fetchAuditLogs({ type: null }));
expectType<Promise<GuildAuditLogs<AuditLogEvent>>>(guild.fetchAuditLogs());

expectType<Promise<GuildAuditLogsEntry<AuditLogEvent.MemberKick, 'Delete', 'User'> | undefined>>(
Expand All @@ -2158,10 +2158,10 @@ expectAssignable<Promise<GuildAuditLogsEntry<AuditLogEvent.MemberKick, 'Delete',
guild.fetchAuditLogs({ type: AuditLogEvent.MemberKick }).then(al => al.entries.first()),
);

expectType<Promise<GuildAuditLogsEntry<null, GuildAuditLogsActionType, GuildAuditLogsTargetType> | undefined>>(
expectType<Promise<GuildAuditLogsEntry<AuditLogEvent, GuildAuditLogsActionType, GuildAuditLogsTargetType> | undefined>>(
guild.fetchAuditLogs({ type: null }).then(al => al.entries.first()),
);
expectType<Promise<GuildAuditLogsEntry<null, GuildAuditLogsActionType, GuildAuditLogsTargetType> | undefined>>(
expectType<Promise<GuildAuditLogsEntry<AuditLogEvent, GuildAuditLogsActionType, GuildAuditLogsTargetType> | undefined>>(
guild.fetchAuditLogs().then(al => al.entries.first()),
);

Expand Down

0 comments on commit 832683c

Please sign in to comment.