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);