Skip to content

Commit

Permalink
Add a minor delay when fetching audit logs since the bot is too fast …
Browse files Browse the repository at this point in the history
…between Discord sending the gateway event and updating the audit logs on server-side
  • Loading branch information
OoLunar committed Nov 1, 2024
1 parent a882a3a commit b635b5a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Events/Handlers/BanAddedEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ private static async ValueTask LogBanAsync(DiscordMember member)
// Ensure all audit logs are the latest
DateTimeOffset timestamp = DateTimeOffset.UtcNow.AddSeconds(-3);

// Wait 1 second so the audit logs can catch up
await Task.Delay(1000);

// Figure out who unbanned the user
await foreach (DiscordAuditLogEntry entry in member.Guild.GetAuditLogsAsync(100, null, DiscordAuditLogActionType.Ban))
{
Expand Down
3 changes: 3 additions & 0 deletions src/Events/Handlers/BanRemovedEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ private static async ValueTask LogUnbanAsync(DiscordMember member)
// Ensure all audit logs are the latest
DateTimeOffset timestamp = DateTimeOffset.UtcNow.AddSeconds(-3);

// Wait 1 second so the audit logs can catch up
await Task.Delay(1000);

// Figure out who unbanned the user
await foreach (DiscordAuditLogEntry entry in member.Guild.GetAuditLogsAsync(100, null, DiscordAuditLogActionType.Unban))
{
Expand Down
3 changes: 3 additions & 0 deletions src/Events/Handlers/GuildMemberRemovedEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ private static async ValueTask LogLeaveAsync(DiscordMember member)

// Ensure all audit logs are the latest
DateTimeOffset timestamp = DateTimeOffset.UtcNow.AddSeconds(-3);

// Wait 1 second so the audit logs can catch up
await Task.Delay(1000);
await foreach (DiscordAuditLogEntry entry in member.Guild.GetAuditLogsAsync(100))
{
if (LoggingEventHandlers.TryFilterAuditLogEntry(entry, DiscordAuditLogActionType.Ban, timestamp, out DiscordAuditLogBanEntry? banEntry) && banEntry.Target.Id == member.Id)
Expand Down
3 changes: 3 additions & 0 deletions src/Events/Handlers/GuildMemberUpdatedEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ private static async ValueTask LogMemberUpdateAsync(GuildMemberUpdatedEventArgs

// Figure out who muted the user
DateTimeOffset timestamp = DateTimeOffset.UtcNow.AddSeconds(-3);

// Wait 1 second so the audit logs can catch up
await Task.Delay(1000);
await foreach (DiscordAuditLogEntry entry in eventArgs.Guild.GetAuditLogsAsync(100, null, DiscordAuditLogActionType.MemberUpdate))
{
if (LoggingEventHandlers.TryFilterAuditLogEntry(entry, DiscordAuditLogActionType.MemberUpdate, timestamp, out DiscordAuditLogMemberUpdateEntry? updateEntry) && updateEntry.Target.Id == eventArgs.Member.Id)
Expand Down

0 comments on commit b635b5a

Please sign in to comment.