Format application permission overrides better

This commit is contained in:
Tarrgon
2025-06-05 08:58:45 -04:00
parent f75b39484f
commit acf27a8bd2
2 changed files with 29 additions and 3 deletions
+12
View File
@@ -8,6 +8,18 @@ export type RoleChangeLog = {
new?: Pick<APIRole, 'id' | 'name'>[]
}
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,
+17 -3
View File
@@ -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;