Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(GuildAuditLogsEntry)!: add type guard for narrowing #10521

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 22 additions & 16 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1467,9 +1467,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 @@ -1515,7 +1515,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 @@ -1527,36 +1527,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 @@ -5726,12 +5730,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 @@ -5753,7 +5759,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 @@ -2134,7 +2134,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 @@ -2144,10 +2144,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