mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-04-19 16:33:36 +03:00
Merge branch 'master' into synchronous-event-update
This commit is contained in:
commit
d3b532d246
3 changed files with 16 additions and 35 deletions
|
@ -54,8 +54,6 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
|
|||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Handles adding a scheduled event to a guild's ScheduledEventData.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class ScheduledEventCreatedResponder : IResponder<IGuildScheduledEventCreate>
|
||||
{
|
||||
private readonly GuildDataService _guildData;
|
||||
|
||||
public ScheduledEventCreatedResponder(GuildDataService guildData)
|
||||
{
|
||||
_guildData = guildData;
|
||||
}
|
||||
|
||||
public async Task<Result> 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();
|
||||
}
|
||||
}
|
|
@ -69,7 +69,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;
|
||||
}
|
||||
|
@ -94,9 +95,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);
|
||||
|
|
Loading…
Add table
Reference in a new issue