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
This commit is contained in:
Octol1ttle 2023-09-30 20:25:22 +05:00 committed by GitHub
parent 804bcd6e68
commit 5d278883d5
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 35 deletions

View file

@ -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<IGuildScheduledEvent> 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<IGuildScheduledEvent> TryGetScheduledEvent(IEnumerable<IGuildScheduledEvent> from, ulong id)
{
var filtered = from.Where(schEvent => schEvent.ID == id);