Skip to content

Commit

Permalink
fix(GuildAuditLogEntry)!: Fix some incorrect types and runtime logic (#…
Browse files Browse the repository at this point in the history
…10591)

BREAKING CHANGE: It also doesn't have a `options.channel_id`, so I stopped `.extra.channel` from being set to `{ id: undefined }`
BREAKING CHANGE: Fixed both types and runtime logic here, it previously created a broken `AutoModerationRule`
BREAKING CHANGE: Removed `Targets.GuildOnboarding`, it will fallback to `Targets.Unknown` and generate a placeholder `target` from the `changes`

---------

Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com>
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: Denis-Adrian Cristea <didinele.dev@gmail.com>
Co-authored-by: Almeida <github@almeidx.dev>
  • Loading branch information
4 people authored Jan 26, 2025
1 parent b7e0fe3 commit b820daa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
40 changes: 25 additions & 15 deletions packages/discord.js/src/structures/GuildAuditLogsEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ const Targets = {
Thread: 'Thread',
ApplicationCommand: 'ApplicationCommand',
AutoModeration: 'AutoModeration',
GuildOnboarding: 'GuildOnboarding',
GuildOnboardingPrompt: 'GuildOnboardingPrompt',
SoundboardSound: 'SoundboardSound',
Unknown: 'Unknown',
};

// TODO: Add soundboard sounds when https://github.com/discordjs/discord.js/pull/10590 is merged
/**
* The target of a guild audit log entry. It can be one of:
* * A guild
Expand All @@ -42,8 +43,7 @@ const Targets = {
* * A role
* * An invite
* * A webhook
* * An emoji
* * A message
* * A guild emoji
* * An integration
* * A stage instance
* * A sticker
Expand All @@ -54,7 +54,7 @@ const Targets = {
* * A guild onboarding prompt
* * An object with an id key if target was deleted or fake entity
* * An object where the keys represent either the new value or the old value
* @typedef {?(Object|Guild|BaseChannel|User|Role|Invite|Webhook|GuildEmoji|Message|Integration|StageInstance|Sticker|
* @typedef {?(Object|Guild|BaseChannel|User|Role|Invite|Webhook|GuildEmoji|Integration|StageInstance|Sticker|
* GuildScheduledEvent|ApplicationCommand|AutoModerationRule|GuildOnboardingPrompt)} AuditLogEntryTarget
*/

Expand Down Expand Up @@ -82,9 +82,10 @@ const Targets = {
* * Sticker
* * Thread
* * GuildScheduledEvent
* * ApplicationCommandPermission
* * GuildOnboarding
* * ApplicationCommand
* * GuildOnboardingPrompt
* * SoundboardSound
* * AutoModeration
* * Unknown
* @typedef {string} AuditLogTargetType
*/
Expand Down Expand Up @@ -198,7 +199,6 @@ class GuildAuditLogsEntry {

case AuditLogEvent.MemberMove:
case AuditLogEvent.MessageDelete:
case AuditLogEvent.MessageBulkDelete:
this.extra = {
channel: guild.channels.cache.get(data.options.channel_id) ?? { id: data.options.channel_id },
count: Number(data.options.count),
Expand All @@ -213,6 +213,7 @@ class GuildAuditLogsEntry {
};
break;

case AuditLogEvent.MessageBulkDelete:
case AuditLogEvent.MemberDisconnect:
this.extra = {
count: Number(data.options.count),
Expand Down Expand Up @@ -364,10 +365,15 @@ class GuildAuditLogsEntry {
data.action_type === AuditLogEvent.OnboardingPromptCreate
? new GuildOnboardingPrompt(guild.client, changesReduce(this.changes, { id: data.target_id }), guild.id)
: changesReduce(this.changes, { id: data.target_id });
} else if (targetType === Targets.GuildOnboarding) {
this.target = changesReduce(this.changes, { id: data.target_id });
} else if (targetType === Targets.Role) {
this.target = guild.roles.cache.get(data.target_id) ?? { id: data.target_id };
} else if (targetType === Targets.Emoji) {
this.target = guild.emojis.cache.get(data.target_id) ?? { id: data.target_id };
// TODO: Uncomment after https://github.com/discordjs/discord.js/pull/10590 is merged
// } else if (targetType === Targets.SoundboardSound) {
// this.target = guild.soundboardSounds.cache.get(data.target_id) ?? { id: data.target_id };
} else if (data.target_id) {
this.target = guild[`${targetType.toLowerCase()}s`]?.cache.get(data.target_id) ?? { id: data.target_id };
this.target = { id: data.target_id };
}
}

Expand All @@ -391,9 +397,10 @@ class GuildAuditLogsEntry {
if (target < 110) return Targets.GuildScheduledEvent;
if (target < 120) return Targets.Thread;
if (target < 130) return Targets.ApplicationCommand;
if (target >= 140 && target < 150) return Targets.AutoModeration;
if (target < 140) return Targets.SoundboardSound;
if (target < 143) return Targets.AutoModeration;
if (target < 146) return Targets.User;
if (target >= 163 && target <= 165) return Targets.GuildOnboardingPrompt;
if (target >= 160 && target < 170) return Targets.GuildOnboarding;
return Targets.Unknown;
}

Expand All @@ -419,10 +426,9 @@ class GuildAuditLogsEntry {
AuditLogEvent.StickerCreate,
AuditLogEvent.GuildScheduledEventCreate,
AuditLogEvent.ThreadCreate,
AuditLogEvent.SoundboardSoundCreate,
AuditLogEvent.AutoModerationRuleCreate,
AuditLogEvent.AutoModerationBlockMessage,
AuditLogEvent.OnboardingPromptCreate,
AuditLogEvent.OnboardingCreate,
].includes(action)
) {
return 'Create';
Expand All @@ -448,6 +454,7 @@ class GuildAuditLogsEntry {
AuditLogEvent.StickerDelete,
AuditLogEvent.GuildScheduledEventDelete,
AuditLogEvent.ThreadDelete,
AuditLogEvent.SoundboardSoundDelete,
AuditLogEvent.AutoModerationRuleDelete,
AuditLogEvent.OnboardingPromptDelete,
].includes(action)
Expand All @@ -473,9 +480,12 @@ class GuildAuditLogsEntry {
AuditLogEvent.GuildScheduledEventUpdate,
AuditLogEvent.ThreadUpdate,
AuditLogEvent.ApplicationCommandPermissionUpdate,
AuditLogEvent.SoundboardSoundUpdate,
AuditLogEvent.AutoModerationRuleUpdate,
AuditLogEvent.AutoModerationBlockMessage,
AuditLogEvent.AutoModerationFlagToChannel,
AuditLogEvent.AutoModerationUserCommunicationDisabled,
AuditLogEvent.OnboardingPromptUpdate,
AuditLogEvent.OnboardingUpdate,
].includes(action)
) {
return 'Update';
Expand Down
34 changes: 19 additions & 15 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1506,14 +1506,14 @@ export class GuildAuditLogsEntry<
public get createdAt(): Date;
public get createdTimestamp(): number;
public executorId: Snowflake | null;
public executor: User | null;
public executor: User | PartialUser | 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<TAction>
? GuildAuditLogsEntryTargetField<TAction>[TTargetType]
: Role | GuildEmoji | { id: Snowflake } | null;
: { id: Snowflake | undefined; [x: string]: unknown } | null;
public targetType: TTargetType;
public static actionType(action: AuditLogEvent): GuildAuditLogsActionType;
public static targetType(target: AuditLogEvent): GuildAuditLogsTargetType;
Expand Down Expand Up @@ -5659,17 +5659,19 @@ interface GuildAuditLogsTypes {
[AuditLogEvent.ThreadUpdate]: ['Thread', 'Update'];
[AuditLogEvent.ThreadDelete]: ['Thread', 'Delete'];
[AuditLogEvent.ApplicationCommandPermissionUpdate]: ['ApplicationCommand', 'Update'];
[AuditLogEvent.SoundboardSoundCreate]: ['SoundboardSound', 'Create'];
[AuditLogEvent.SoundboardSoundUpdate]: ['SoundboardSound', 'Update'];
[AuditLogEvent.SoundboardSoundDelete]: ['SoundboardSound', 'Delete'];
[AuditLogEvent.AutoModerationRuleCreate]: ['AutoModeration', 'Create'];
[AuditLogEvent.AutoModerationRuleUpdate]: ['AutoModeration', 'Update'];
[AuditLogEvent.AutoModerationRuleDelete]: ['AutoModeration', 'Delete'];
[AuditLogEvent.AutoModerationBlockMessage]: ['AutoModeration', 'Create'];
[AuditLogEvent.AutoModerationFlagToChannel]: ['AutoModeration', 'Create'];
[AuditLogEvent.AutoModerationUserCommunicationDisabled]: ['AutoModeration', 'Create'];
[AuditLogEvent.AutoModerationBlockMessage]: ['User', 'Update'];
[AuditLogEvent.AutoModerationFlagToChannel]: ['User', 'Update'];
[AuditLogEvent.AutoModerationUserCommunicationDisabled]: ['User', 'Update'];
[AuditLogEvent.OnboardingPromptCreate]: ['GuildOnboardingPrompt', 'Create'];
[AuditLogEvent.OnboardingPromptUpdate]: ['GuildOnboardingPrompt', 'Update'];
[AuditLogEvent.OnboardingPromptDelete]: ['GuildOnboardingPrompt', 'Delete'];
[AuditLogEvent.OnboardingCreate]: ['GuildOnboarding', 'Create'];
[AuditLogEvent.OnboardingUpdate]: ['GuildOnboarding', 'Update'];
// Never have a target: CreatorMonetizationRequestCreated, CreatorMonetizationTermsAccepted, OnboardingCreate, OnboardingUpdate, HomeSettingsCreate, HomeSettingsUpdate
}

export type GuildAuditLogsActionType = GuildAuditLogsTypes[keyof GuildAuditLogsTypes][1] | 'All';
Expand All @@ -5680,7 +5682,7 @@ export interface GuildAuditLogsEntryExtraField {
[AuditLogEvent.MemberPrune]: { removed: number; days: number };
[AuditLogEvent.MemberMove]: { channel: VoiceBasedChannel | { id: Snowflake }; count: number };
[AuditLogEvent.MessageDelete]: { channel: GuildTextBasedChannel | { id: Snowflake }; count: number };
[AuditLogEvent.MessageBulkDelete]: { channel: GuildTextBasedChannel | { id: Snowflake }; count: number };
[AuditLogEvent.MessageBulkDelete]: { count: number };
[AuditLogEvent.MessagePin]: { channel: GuildTextBasedChannel | { id: Snowflake }; messageId: Snowflake };
[AuditLogEvent.MessageUnpin]: { channel: GuildTextBasedChannel | { id: Snowflake }; messageId: Snowflake };
[AuditLogEvent.MemberDisconnect]: { count: number };
Expand Down Expand Up @@ -5721,22 +5723,24 @@ export interface GuildAuditLogsEntryExtraField {
}

export interface GuildAuditLogsEntryTargetField<TAction extends AuditLogEvent> {
User: User | null;
User: User | PartialUser | null;
Guild: Guild;
Webhook: Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>;
Invite: Invite;
Emoji: GuildEmoji;
Role: Role;
Message: TAction extends AuditLogEvent.MessageBulkDelete ? Guild | { id: Snowflake } : User;
Emoji: GuildEmoji | { id: Snowflake };
Role: Role | { id: Snowflake };
Message: TAction extends AuditLogEvent.MessageBulkDelete ? GuildTextBasedChannel | { id: Snowflake } : User | null;
Integration: Integration;
Channel: NonThreadGuildBasedChannel | { id: Snowflake; [x: string]: unknown };
Thread: AnyThreadChannel | { id: Snowflake; [x: string]: unknown };
StageInstance: StageInstance;
Sticker: Sticker;
GuildScheduledEvent: GuildScheduledEvent;
ApplicationCommand: ApplicationCommand | { id: Snowflake };
AutoModerationRule: AutoModerationRule;
GuildOnboardingPrompt: GuildOnboardingPrompt;
AutoModeration: AutoModerationRule;
GuildOnboardingPrompt: GuildOnboardingPrompt | { id: Snowflake; [x: string]: unknown };
// TODO: Update when https://github.com/discordjs/discord.js/pull/10590 is merged
SoundboardSound: { id: Snowflake };
}

export interface GuildAuditLogsFetchOptions<Event extends GuildAuditLogsResolvable> {
Expand All @@ -5752,7 +5756,7 @@ export type GuildAuditLogsResolvable = AuditLogEvent | null;
export type GuildAuditLogsTargetType = GuildAuditLogsTypes[keyof GuildAuditLogsTypes][0] | 'Unknown';

export type GuildAuditLogsTargets = {
[Key in GuildAuditLogsTargetType]: GuildAuditLogsTargetType;
[Key in GuildAuditLogsTargetType]: Key;
};

export type GuildBanResolvable = GuildBan | UserResolvable;
Expand Down
7 changes: 5 additions & 2 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2258,15 +2258,18 @@ expectType<Promise<{ channel: GuildTextBasedChannel | { id: Snowflake }; count:
guild.fetchAuditLogs({ type: AuditLogEvent.MessageDelete }).then(al => al.entries.first()?.extra),
);

expectType<Promise<User | null | undefined>>(
expectType<Promise<User | PartialUser | null | undefined>>(
guild.fetchAuditLogs({ type: AuditLogEvent.MemberKick }).then(al => al.entries.first()?.target),
);
expectType<Promise<StageInstance | undefined>>(
guild.fetchAuditLogs({ type: AuditLogEvent.StageInstanceCreate }).then(al => al.entries.first()?.target),
);
expectType<Promise<User | undefined>>(
expectType<Promise<User | null | undefined>>(
guild.fetchAuditLogs({ type: AuditLogEvent.MessageDelete }).then(al => al.entries.first()?.target),
);
expectType<Promise<GuildTextBasedChannel | { id: string } | undefined>>(
guild.fetchAuditLogs({ type: AuditLogEvent.MessageBulkDelete }).then(al => al.entries.first()?.target),
);

declare const AuditLogChange: AuditLogChange;
// @ts-expect-error
Expand Down

0 comments on commit b820daa

Please sign in to comment.