From acf27a8bd28426a85270e20e38dc56a103af3a9e Mon Sep 17 00:00:00 2001 From: Tarrgon <61888458+Tarrgon@users.noreply.github.com> Date: Thu, 5 Jun 2025 08:58:45 -0400 Subject: [PATCH] Format application permission overrides better --- src/types/helper-types.d.ts | 12 ++++++++++++ src/utils/audit-log-utils.ts | 20 +++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/types/helper-types.d.ts b/src/types/helper-types.d.ts index 30612cd..304695b 100644 --- a/src/types/helper-types.d.ts +++ b/src/types/helper-types.d.ts @@ -8,6 +8,18 @@ export type RoleChangeLog = { new?: Pick[] } +type ApplicationCommandPermission = { + type: 1 | 2 | 3, + permission: boolean, + id: string +} + +export type ApplicationCommandPermissionChangeLog = { + key: sting, + old?: ApplicationCommandPermission, + new?: ApplicationCommandPermission +} + export type TimeoutChangeLog = { key: 'communication_disabled_until', old?: string, diff --git a/src/utils/audit-log-utils.ts b/src/utils/audit-log-utils.ts index 9d4ea14..4de39f5 100644 --- a/src/utils/audit-log-utils.ts +++ b/src/utils/audit-log-utils.ts @@ -1,5 +1,5 @@ import { AuditLogChange, AuditLogEvent, AuditLogOptionsType, GuildAuditLogsEntry, APIRole, time, TimestampStyles, PermissionsBitField, PermissionsString, Guild } from 'discord.js'; -import { PermissionsChangeLog, PinExtras, RoleChangeLog, TimeoutChangeLog } from '../types'; +import { ApplicationCommandPermissionChangeLog, PermissionsChangeLog, PinExtras, RoleChangeLog, TimeoutChangeLog } from '../types'; import { getArrayDifference } from './array-utils'; export const enum TargetType { @@ -55,7 +55,7 @@ export function formatSnowflake(snowflake: string, targetType: TargetType): stri } export function formatChanges(entry: GuildAuditLogsEntry): string { - return entry.changes.map(c => formatChange(c)).filter(e => e).join('\n'); + return entry.changes.map(c => formatChange(c, entry)).filter(e => e).join('\n'); } export function formatExtras(entry: GuildAuditLogsEntry, guild: Guild): string { @@ -90,7 +90,11 @@ export function formatExtras(entry: GuildAuditLogsEntry, guild: Guild): string { return ''; } -function formatChange(change: AuditLogChange): string | undefined { +function formatChange(change: AuditLogChange, entry: GuildAuditLogsEntry): string | undefined { + if (entry.action == AuditLogEvent.ApplicationCommandPermissionUpdate) { + return formatApplicationPermissionsUpdate(change as ApplicationCommandPermissionChangeLog); + } + switch (change.key) { case '$add': case '$remove': @@ -114,6 +118,16 @@ function formatChange(change: AuditLogChange): string | undefined { return `Set ${change.key} from ${change.old} to ${change.new}`; } +function formatApplicationPermissionsUpdate(change: ApplicationCommandPermissionChangeLog): string | undefined { + if (change.new !== undefined && change.old === undefined) + return `${change.new.permission ? 'Allowed' : 'Denied'} access ${change.new.type == 3 ? 'in' : (change.new.permission ? 'to' : 'from')} ${formatSnowflake(change.new.id, change.new.type)} for command: ${change.key}`; + + if (change.new === undefined && change.old !== undefined) + return `Removed permission overrides from ${formatSnowflake(change.old.id, change.old.type)} for command: ${change.key}`; + + return `Updated permission overrides ${change.new!.type == 3 ? 'in' : 'for'} ${formatSnowflake(change.new!.id, change.new!.type)}: ${change.new!.permission ? 'allowed access to' : 'revoked access to'} command: ${change.key}`; +} + function formatMemberRoleChange(change: RoleChangeLog): string | undefined { if (!change.new) return;