From a1408e88e2dd2a21f09a32bf4e890ba3c9ce7547 Mon Sep 17 00:00:00 2001 From: Macintosh II <mctaylxrs@outlook.com> Date: Wed, 4 Oct 2023 15:03:26 +0300 Subject: [PATCH] Use IGuildDelete Signed-off-by: Macintosh II <mctaylxrs@outlook.com> --- src/Octobot.cs | 1 - src/Responders/GuildUnloadedResponder.cs | 37 ++++++++++++++++ src/Services/Update/GuildUpdateService.cs | 51 ----------------------- 3 files changed, 37 insertions(+), 52 deletions(-) create mode 100644 src/Responders/GuildUnloadedResponder.cs delete mode 100644 src/Services/Update/GuildUpdateService.cs diff --git a/src/Octobot.cs b/src/Octobot.cs index 8e60caf..662d1bf 100644 --- a/src/Octobot.cs +++ b/src/Octobot.cs @@ -89,7 +89,6 @@ public sealed class Octobot // Services .AddSingleton<GuildDataService>() .AddSingleton<UtilityService>() - .AddHostedService<GuildUpdateService>() .AddHostedService<MemberUpdateService>() .AddHostedService<ScheduledEventUpdateService>() .AddHostedService<SongUpdateService>() diff --git a/src/Responders/GuildUnloadedResponder.cs b/src/Responders/GuildUnloadedResponder.cs new file mode 100644 index 0000000..de72628 --- /dev/null +++ b/src/Responders/GuildUnloadedResponder.cs @@ -0,0 +1,37 @@ +using JetBrains.Annotations; +using Microsoft.Extensions.Logging; +using Octobot.Data; +using Octobot.Services; +using Remora.Discord.API.Abstractions.Gateway.Events; +using Remora.Discord.API.Gateway.Events; +using Remora.Discord.Gateway.Responders; +using Remora.Results; + +namespace Octobot.Responders; + +/// <summary> +/// Handles sending a <see cref="Ready" /> message to a guild that has just initialized if that guild +/// has <see cref="GuildSettings.ReceiveStartupMessages" /> enabled +/// </summary> +[UsedImplicitly] +public class GuildUnloadedResponder : IResponder<IGuildDelete> +{ + private readonly GuildDataService _guildData; + private readonly ILogger<GuildUnloadedResponder> _logger; + + public GuildUnloadedResponder( + GuildDataService guildData, ILogger<GuildUnloadedResponder> logger) + { + _guildData = guildData; + _logger = logger; + } + + public async Task<Result> RespondAsync(IGuildDelete gatewayEvent, CancellationToken ct = default) + { + var guildId = gatewayEvent.ID; + await _guildData.RemoveGuildId(guildId); + _logger.LogInformation("Left guild {guildId}", guildId); + + return Result.FromSuccess(); + } +} diff --git a/src/Services/Update/GuildUpdateService.cs b/src/Services/Update/GuildUpdateService.cs deleted file mode 100644 index d13423b..0000000 --- a/src/Services/Update/GuildUpdateService.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Remora.Discord.API.Abstractions.Rest; -using Remora.Rest.Core; -using Remora.Results; - -namespace Octobot.Services.Update; - -public sealed class GuildUpdateService : BackgroundService -{ - private readonly IDiscordRestGuildAPI _guildApi; - private readonly GuildDataService _guildData; - private readonly ILogger<GuildUpdateService> _logger; - - public GuildUpdateService(IDiscordRestGuildAPI guildApi, - GuildDataService guildData, ILogger<GuildUpdateService> logger) - { - _guildApi = guildApi; - _guildData = guildData; - _logger = logger; - } - - protected override async Task ExecuteAsync(CancellationToken ct) - { - using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1)); - - while (await timer.WaitForNextTickAsync(ct)) - { - var guildIds = _guildData.GetGuildIds(); - foreach (var id in guildIds) - { - var tickResult = await TickGuildsAsync(id, ct); - _logger.LogResult(tickResult, $"Error in guild update for guild {id}."); - } - } - } - - private async Task<Result> TickGuildsAsync(Snowflake guildId, CancellationToken ct) - { - var getGuildResult = await _guildApi.GetGuildAsync(guildId, ct: ct); - if (getGuildResult.IsSuccess) - { - return Result.FromSuccess(); - } - - await _guildData.RemoveGuildId(guildId); - _logger.LogInformation("Left guild {guildId}", guildId); - - return Result.FromSuccess(); - } -}