From 48f78820b24a34f37cf33de164aa92e1974da279 Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Sat, 23 Nov 2024 17:22:14 +0100 Subject: [PATCH] Fix mod log: Handle member updates properly --- lib/src/modules/mod_log.dart | 40 +++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/src/modules/mod_log.dart b/lib/src/modules/mod_log.dart index 0283478..739a6f1 100644 --- a/lib/src/modules/mod_log.dart +++ b/lib/src/modules/mod_log.dart @@ -13,11 +13,13 @@ class ModLogsModule implements RequiresInitialization { final FeatureSettingsModule _featureSettingsService = Injector.appInstance.get(); final Logger _logger = Logger('ROD.ModLogs'); - final handledEventTypes = [ - AuditLogEvent.memberUpdate, - AuditLogEvent.memberBanAdd, - AuditLogEvent.memberKick, - ]; + Map> handledEventTypes = { + AuditLogEvent.memberUpdate: [ + 'communication_disabled_until', + ], + AuditLogEvent.memberBanAdd: [], + AuditLogEvent.memberKick: [], + }; @override Future init() async { @@ -36,7 +38,9 @@ class ModLogsModule implements RequiresInitialization { } final entry = event.entry; - if (!handledEventTypes.contains(entry.actionType)) { + final handledEventType = handledEventTypes[entry.actionType]; + if (handledEventType == null || + (handledEventType.isNotEmpty && !handledEventType.contains(entry.changes?.firstOrNull?.key))) { return; } @@ -62,26 +66,30 @@ class ModLogsModule implements RequiresInitialization { _ => throw UnimplementedError(), }; - var timeoutUntilMessage = ""; + final messageBuffer = StringBuffer('$eventTypeName | ${DateTime.now().format(TimestampStyle.longDateTime)}') + ..writeln('User: ${targetUser.username} (${targetUser.mention})'); + final auditLogChange = auditLogEntry.changes?.first; - if (auditLogEntry.actionType == AuditLogEvent.memberUpdate && - auditLogChange?.key == 'communication_disabled_until') { + if (isMemberTimeoutEntry(auditLogEntry, auditLogChange)) { final timeoutUntil = DateTime.parse(auditLogChange!.newValue as String); - timeoutUntilMessage = "\nUntil: ${timeoutUntil.format(TimestampStyle.relativeTime)}"; + messageBuffer.writeln("Until: ${timeoutUntil.format(TimestampStyle.relativeTime)}"); + } + + if (auditLogEntry.reason != null) { + messageBuffer.writeln('Reason: ${auditLogEntry.reason}'); } - final messageContent = """$eventTypeName | ${DateTime.now().format(TimestampStyle.longDateTime)} -User: ${targetUser.username} (${targetUser.mention})$timeoutUntilMessage -Reason: ${auditLogEntry.reason} -Moderator: ${modUser.username} (${modUser.mention}) -"""; + messageBuffer.writeln('Moderator: ${modUser.username} (${modUser.mention})'); return MessageBuilder( - content: messageContent, + content: messageBuffer.toString(), allowedMentions: AllowedMentions.users([targetUser.id]), ); } + bool isMemberTimeoutEntry(AuditLogEntry auditLogEntry, AuditLogChange? auditLogChange) => + auditLogEntry.actionType == AuditLogEvent.memberUpdate && auditLogChange?.key == 'communication_disabled_until'; + Future _isEnabledForGuild(Snowflake guildId) async { if (!intentFeaturesEnabled) { return false;