1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

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 <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2024-06-22 00:28:29 +05:00 committed by GitHub
parent 2b0c4b62d3
commit a953053f1d
Signed by: GitHub
GPG key ID: B5690EEEBB952194

View file

@ -66,10 +66,10 @@ public sealed class MessageDeletedResponder : IResponder<IMessageDelete>
return ResultExtensions.FromError(auditLogResult); return ResultExtensions.FromError(auditLogResult);
} }
var auditLog = auditLogPage.AuditLogEntries.Single();
var deleterResult = Result<IUser>.FromSuccess(message.Author); 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 && auditLog.Options.Value.ChannelID == gatewayEvent.ChannelID
&& DateTimeOffset.UtcNow.Subtract(auditLog.ID.Timestamp).TotalSeconds <= 2) && DateTimeOffset.UtcNow.Subtract(auditLog.ID.Timestamp).TotalSeconds <= 2)
{ {