forked from TeamInklings/Octobot
Synchronize events only on sch. event updates (#131)
This PR moves all code related to synchronization of scheduled events to ScheduledEventUpdateService. Just like #130, this reduces possible points of failures, maintenance burden and reliance on gateway events
This commit is contained in:
parent
d713b977f0
commit
e073c5a572
3 changed files with 14 additions and 50 deletions
|
@ -50,19 +50,6 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
|
|||
data.GetOrCreateMemberData(member.User.Value.ID);
|
||||
}
|
||||
|
||||
foreach (var schEvent in guild.GuildScheduledEvents)
|
||||
{
|
||||
if (!data.ScheduledEvents.TryGetValue(schEvent.ID.Value, out var eventData))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
eventData.Name = schEvent.Name;
|
||||
eventData.ScheduledStartTime = schEvent.ScheduledStartTime;
|
||||
eventData.ScheduleOnStatusUpdated = eventData.Status != schEvent.Status;
|
||||
eventData.Status = schEvent.Status;
|
||||
}
|
||||
|
||||
if (!GuildSettings.ReceiveStartupMessages.Get(cfg))
|
||||
{
|
||||
return Result.FromSuccess();
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
using JetBrains.Annotations;
|
||||
using Octobot.Services;
|
||||
using Remora.Discord.API.Abstractions.Gateway.Events;
|
||||
using Remora.Discord.Gateway.Responders;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Octobot.Responders;
|
||||
|
||||
[UsedImplicitly]
|
||||
public class ScheduledEventUpdatedResponder : IResponder<IGuildScheduledEventUpdate>
|
||||
{
|
||||
private readonly GuildDataService _guildData;
|
||||
|
||||
public ScheduledEventUpdatedResponder(GuildDataService guildData)
|
||||
{
|
||||
_guildData = guildData;
|
||||
}
|
||||
|
||||
public async Task<Result> RespondAsync(IGuildScheduledEventUpdate gatewayEvent, CancellationToken ct = default)
|
||||
{
|
||||
var data = await _guildData.GetData(gatewayEvent.GuildID, ct);
|
||||
var eventData = data.ScheduledEvents[gatewayEvent.ID.Value];
|
||||
eventData.Name = gatewayEvent.Name;
|
||||
eventData.ScheduledStartTime = gatewayEvent.ScheduledStartTime;
|
||||
eventData.ScheduleOnStatusUpdated = eventData.Status != gatewayEvent.Status;
|
||||
eventData.Status = gatewayEvent.Status;
|
||||
|
||||
return Result.FromSuccess();
|
||||
}
|
||||
}
|
|
@ -61,6 +61,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|
|||
return Result.FromError(eventsResult);
|
||||
}
|
||||
|
||||
SyncScheduledEvents(data, events);
|
||||
|
||||
foreach (var storedEvent in data.ScheduledEvents.Values)
|
||||
{
|
||||
var scheduledEvent = TryGetScheduledEvent(events, storedEvent.Id);
|
||||
|
@ -100,20 +102,25 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|
|||
failedResults.AddIfFailed(statusUpdatedResponseResult);
|
||||
}
|
||||
|
||||
SyncScheduledEvents(data, events);
|
||||
|
||||
return failedResults.AggregateErrors();
|
||||
}
|
||||
|
||||
private static void SyncScheduledEvents(GuildData data, IReadOnlyCollection<IGuildScheduledEvent> events)
|
||||
private static void SyncScheduledEvents(GuildData data, IEnumerable<IGuildScheduledEvent> events)
|
||||
{
|
||||
if (data.ScheduledEvents.Count < events.Count)
|
||||
foreach (var @event in events)
|
||||
{
|
||||
foreach (var @event in events.Where(@event => !data.ScheduledEvents.ContainsKey(@event.ID.Value)))
|
||||
if (!data.ScheduledEvents.ContainsKey(@event.ID.Value))
|
||||
{
|
||||
data.ScheduledEvents.Add(@event.ID.Value, new ScheduledEventData(@event.ID.Value,
|
||||
@event.Name, @event.ScheduledStartTime, @event.Status));
|
||||
data.ScheduledEvents.Add(@event.ID.Value,
|
||||
new ScheduledEventData(@event.ID.Value, @event.Name, @event.ScheduledStartTime, @event.Status));
|
||||
continue;
|
||||
}
|
||||
|
||||
var eventData = data.ScheduledEvents[@event.ID.Value];
|
||||
eventData.Name = @event.Name;
|
||||
eventData.ScheduledStartTime = @event.ScheduledStartTime;
|
||||
eventData.ScheduleOnStatusUpdated = eventData.Status != @event.Status;
|
||||
eventData.Status = @event.Status;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue