1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-10 16:03:15 +03:00

Fix various issues with ScheduledEventUpdateService (#89)

This PR closes #85.

This PR fixes many issues related to scheduled events. Most importantly,
scheduled events that are no longer present in the guild, but still have
data related to them, won't be left rotting.

This requires deletion of `ScheduledEvents.json` files in all guilds.
Maybe I'll start writing datafixers one day...

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-08-14 18:24:22 +05:00 committed by GitHub
parent 501c51b865
commit 4252613dd3
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 190 additions and 112 deletions

View file

@ -50,6 +50,21 @@ 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))
{
data.ScheduledEvents.Add(schEvent.ID.Value, new ScheduledEventData(schEvent.ID.Value,
schEvent.Name, schEvent.Status, schEvent.ScheduledStartTime));
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();

View file

@ -1,53 +0,0 @@
using Boyfriend.Data;
using Boyfriend.Services;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Extensions.Embeds;
using Remora.Discord.Gateway.Responders;
using Remora.Results;
namespace Boyfriend.Responders;
/// <summary>
/// Handles sending a notification when a scheduled event has been cancelled
/// in a guild's <see cref="GuildSettings.EventNotificationChannel" /> if one is set.
/// </summary>
[UsedImplicitly]
public class GuildScheduledEventDeleteResponder : IResponder<IGuildScheduledEventDelete>
{
private readonly IDiscordRestChannelAPI _channelApi;
private readonly GuildDataService _guildData;
public GuildScheduledEventDeleteResponder(IDiscordRestChannelAPI channelApi, GuildDataService guildData)
{
_channelApi = channelApi;
_guildData = guildData;
}
public async Task<Result> RespondAsync(IGuildScheduledEventDelete gatewayEvent, CancellationToken ct = default)
{
var guildData = await _guildData.GetData(gatewayEvent.GuildID, ct);
guildData.ScheduledEvents.Remove(gatewayEvent.ID.Value);
if (GuildSettings.EventNotificationChannel.Get(guildData.Settings).Empty())
{
return Result.FromSuccess();
}
var embed = new EmbedBuilder()
.WithSmallTitle(string.Format(Messages.EventCancelled, gatewayEvent.Name))
.WithDescription(":(")
.WithColour(ColorsList.Red)
.WithCurrentTimestamp()
.Build();
if (!embed.IsDefined(out var built))
{
return Result.FromError(embed);
}
return (Result)await _channelApi.CreateMessageAsync(
GuildSettings.EventNotificationChannel.Get(guildData.Settings), embeds: new[] { built }, ct: ct);
}
}

View file

@ -0,0 +1,32 @@
using Boyfriend.Data;
using Boyfriend.Services;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.Gateway.Responders;
using Remora.Results;
namespace Boyfriend.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.Status, gatewayEvent.ScheduledStartTime));
return Result.FromSuccess();
}
}

View file

@ -0,0 +1,30 @@
using Boyfriend.Services;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.Gateway.Responders;
using Remora.Results;
namespace Boyfriend.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();
}
}