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

@ -54,8 +54,6 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
{ {
if (!data.ScheduledEvents.TryGetValue(schEvent.ID.Value, out var eventData)) 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; continue;
} }

View file

@ -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();
}
}

View file

@ -74,7 +74,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService
if (!storedEvent.ScheduleOnStatusUpdated) 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); failedResults.AddIfFailed(tickResult);
continue; continue;
} }
@ -99,9 +100,23 @@ public sealed class ScheduledEventUpdateService : BackgroundService
failedResults.AddIfFailed(statusUpdatedResponseResult); failedResults.AddIfFailed(statusUpdatedResponseResult);
} }
SyncScheduledEvents(data, events);
return failedResults.AggregateErrors(); 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) private static Result<IGuildScheduledEvent> TryGetScheduledEvent(IEnumerable<IGuildScheduledEvent> from, ulong id)
{ {
var filtered = from.Where(schEvent => schEvent.ID == id); var filtered = from.Where(schEvent => schEvent.ID == id);