Skip to content

Commit

Permalink
fix(GuildAuditLogsEntry): correct mapped AuditLogChange objects (#1…
Browse files Browse the repository at this point in the history
…0438)

* refactor(GuildAuditLogsEntry): correct mapped AuditLogChange objects

* test: check union narrowing behaviour of AuditLogChange

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
Renegade334 and kodiakhq[bot] authored Aug 20, 2024
1 parent 69adc6f commit 45f7e1a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/discord.js/src/structures/GuildAuditLogsEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ class GuildAuditLogsEntry {
* @type {AuditLogChange[]}
*/
this.changes =
data.changes?.map(change => ({ key: change.key, old: change.old_value, new: change.new_value })) ?? [];
data.changes?.map(change => ({
key: change.key,
...('old_value' in change ? { old: change.old_value } : {}),
...('new_value' in change ? { new: change.new_value } : {}),
})) ?? [];

/**
* The entry's id
Expand Down
12 changes: 7 additions & 5 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4991,11 +4991,13 @@ export interface ApplicationRoleConnectionMetadataEditOptions {
type: ApplicationRoleConnectionMetadataType;
}

export interface AuditLogChange {
key: APIAuditLogChange['key'];
old?: APIAuditLogChange['old_value'];
new?: APIAuditLogChange['new_value'];
}
export type AuditLogChange = {
[SourceElement in APIAuditLogChange as SourceElement['key']]: {
key: SourceElement['key'];
old?: SourceElement['old_value'];
new?: SourceElement['new_value'];
};
}[APIAuditLogChange['key']];

export interface AutoModerationAction {
type: AutoModerationActionType;
Expand Down
11 changes: 11 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import {
Collector,
GuildAuditLogsEntry,
GuildAuditLogs,
type AuditLogChange,
StageInstance,
ActionRowBuilder,
ButtonComponent,
Expand Down Expand Up @@ -2171,6 +2172,16 @@ expectType<Promise<User | undefined>>(
guild.fetchAuditLogs({ type: AuditLogEvent.MessageDelete }).then(al => al.entries.first()?.target),
);

declare const AuditLogChange: AuditLogChange;
// @ts-expect-error
expectType<boolean | undefined>(AuditLogChange.old);
// @ts-expect-error
expectType<boolean | undefined>(AuditLogChange.new);
if (AuditLogChange.key === 'available') {
expectType<boolean | undefined>(AuditLogChange.old);
expectType<boolean | undefined>(AuditLogChange.new);
}

declare const TextBasedChannel: TextBasedChannel;
declare const TextBasedChannelTypes: TextBasedChannelTypes;
declare const VoiceBasedChannel: VoiceBasedChannel;
Expand Down

0 comments on commit 45f7e1a

Please sign in to comment.