From a953053f1d7ad7012f80d2bb338c7ee107e5bf9c Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Sat, 22 Jun 2024 00:28:29 +0500 Subject: [PATCH] Handle audit log entries for message deletion being empty (#317) In order to determine who deleted a message, Octobot fetches the audit log with a filter on the action "Message Delete", gets the latest entry and uses its author if the timestamps roughly match. However, if the filter returns no entries (as in, no message deletions are present in the audit log), `Single()` will throw an exception with the message `Sequence contains no elements`. To fix this, this PR replaces `Single()` with `SingleOrDefault()` and adds a null-check on `auditLog` in the form of a pattern access Signed-off-by: Octol1ttle --- TeamOctolings.Octobot/Responders/MessageDeletedResponder.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TeamOctolings.Octobot/Responders/MessageDeletedResponder.cs b/TeamOctolings.Octobot/Responders/MessageDeletedResponder.cs index 88a8de2..f0e3d22 100644 --- a/TeamOctolings.Octobot/Responders/MessageDeletedResponder.cs +++ b/TeamOctolings.Octobot/Responders/MessageDeletedResponder.cs @@ -66,10 +66,10 @@ public sealed class MessageDeletedResponder : IResponder return ResultExtensions.FromError(auditLogResult); } - var auditLog = auditLogPage.AuditLogEntries.Single(); - var deleterResult = Result.FromSuccess(message.Author); - if (auditLog.UserID is not null + + var auditLog = auditLogPage.AuditLogEntries.SingleOrDefault(); + if (auditLog is { UserID: not null } && auditLog.Options.Value.ChannelID == gatewayEvent.ChannelID && DateTimeOffset.UtcNow.Subtract(auditLog.ID.Timestamp).TotalSeconds <= 2) {