Skip to content

Commit

Permalink
Fix mod log: Handle member updates properly
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Nov 23, 2024
1 parent a8641c1 commit 48f7882
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions lib/src/modules/mod_log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuditLogEvent, List<String>> handledEventTypes = {
AuditLogEvent.memberUpdate: [
'communication_disabled_until',
],
AuditLogEvent.memberBanAdd: [],
AuditLogEvent.memberKick: [],
};

@override
Future<void> init() async {
Expand All @@ -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;
}

Expand All @@ -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<bool> _isEnabledForGuild(Snowflake guildId) async {
if (!intentFeaturesEnabled) {
return false;
Expand Down

0 comments on commit 48f7882

Please sign in to comment.