Skip to content

Commit

Permalink
Handle audit log entries for message deletion being empty (#317)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Octol1ttle authored Jun 21, 2024
1 parent 2b0c4b6 commit a953053
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions TeamOctolings.Octobot/Responders/MessageDeletedResponder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public async Task<Result> RespondAsync(IMessageDelete gatewayEvent, Cancellation
return ResultExtensions.FromError(auditLogResult);
}

var auditLog = auditLogPage.AuditLogEntries.Single();

var deleterResult = Result<IUser>.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)
{
Expand Down

0 comments on commit a953053

Please sign in to comment.