From 5d278883d55e70f18219a3f77a1df6db1bd234ef Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Sat, 30 Sep 2023 20:25:22 +0500 Subject: [PATCH] Synchronize scheduled events on sch. events tick (#129) Because the Discord API sucks at providing data when needed, this PR makes it so that events are synchronized every tick instead of on gateway events. This fixes an issue where the bot won't know about a scheduled event if it was created before the bot was started --- src/Responders/GuildLoadedResponder.cs | 2 -- .../ScheduledEventCreatedResponder.cs | 32 ------------------- .../Update/ScheduledEventUpdateService.cs | 17 +++++++++- 3 files changed, 16 insertions(+), 35 deletions(-) delete mode 100644 src/Responders/ScheduledEventCreatedResponder.cs diff --git a/src/Responders/GuildLoadedResponder.cs b/src/Responders/GuildLoadedResponder.cs index a6b44dd..cfc33fa 100644 --- a/src/Responders/GuildLoadedResponder.cs +++ b/src/Responders/GuildLoadedResponder.cs @@ -54,8 +54,6 @@ public class GuildLoadedResponder : IResponder { if (!data.ScheduledEvents.TryGetValue(schEvent.ID.Value, out var eventData)) { - data.ScheduledEvents.Add(schEvent.ID.Value, new ScheduledEventData(schEvent.ID.Value, - schEvent.Name, schEvent.ScheduledStartTime, schEvent.Status)); continue; } diff --git a/src/Responders/ScheduledEventCreatedResponder.cs b/src/Responders/ScheduledEventCreatedResponder.cs deleted file mode 100644 index 4d87b6c..0000000 --- a/src/Responders/ScheduledEventCreatedResponder.cs +++ /dev/null @@ -1,32 +0,0 @@ -using JetBrains.Annotations; -using Octobot.Data; -using Octobot.Services; -using Remora.Discord.API.Abstractions.Gateway.Events; -using Remora.Discord.Gateway.Responders; -using Remora.Results; - -namespace Octobot.Responders; - -/// -/// Handles adding a scheduled event to a guild's ScheduledEventData. -/// -[UsedImplicitly] -public class ScheduledEventCreatedResponder : IResponder -{ - private readonly GuildDataService _guildData; - - public ScheduledEventCreatedResponder(GuildDataService guildData) - { - _guildData = guildData; - } - - public async Task RespondAsync(IGuildScheduledEventCreate gatewayEvent, CancellationToken ct = default) - { - var data = await _guildData.GetData(gatewayEvent.GuildID, ct); - data.ScheduledEvents.Add(gatewayEvent.ID.Value, - new ScheduledEventData(gatewayEvent.ID.Value, - gatewayEvent.Name, gatewayEvent.ScheduledStartTime, gatewayEvent.Status)); - - return Result.FromSuccess(); - } -} diff --git a/src/Services/Update/ScheduledEventUpdateService.cs b/src/Services/Update/ScheduledEventUpdateService.cs index 85d532a..2a8beca 100644 --- a/src/Services/Update/ScheduledEventUpdateService.cs +++ b/src/Services/Update/ScheduledEventUpdateService.cs @@ -74,7 +74,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService if (!storedEvent.ScheduleOnStatusUpdated) { - var tickResult = await TickScheduledEventAsync(guildId, data, scheduledEvent.Entity, storedEvent, ct); + var tickResult = + await TickScheduledEventAsync(guildId, data, scheduledEvent.Entity, storedEvent, ct); failedResults.AddIfFailed(tickResult); continue; } @@ -99,9 +100,23 @@ public sealed class ScheduledEventUpdateService : BackgroundService failedResults.AddIfFailed(statusUpdatedResponseResult); } + SyncScheduledEvents(data, events); + return failedResults.AggregateErrors(); } + private static void SyncScheduledEvents(GuildData data, IReadOnlyCollection events) + { + if (data.ScheduledEvents.Count < events.Count) + { + foreach (var @event in events.Where(@event => !data.ScheduledEvents.ContainsKey(@event.ID.Value))) + { + data.ScheduledEvents.Add(@event.ID.Value, new ScheduledEventData(@event.ID.Value, + @event.Name, @event.ScheduledStartTime, @event.Status)); + } + } + } + private static Result TryGetScheduledEvent(IEnumerable from, ulong id) { var filtered = from.Where(schEvent => schEvent.ID == id);