1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-20 00:43:36 +03:00

Use IGuildDelete

Signed-off-by: Macintosh II <mctaylxrs@outlook.com>
This commit is contained in:
Macintxsh 2023-10-04 15:03:26 +03:00
parent 5cfb5e428f
commit a1408e88e2
Signed by: mctaylors
GPG key ID: 361D326747B61E65
3 changed files with 37 additions and 52 deletions

View file

@ -89,7 +89,6 @@ public sealed class Octobot
// Services // Services
.AddSingleton<GuildDataService>() .AddSingleton<GuildDataService>()
.AddSingleton<UtilityService>() .AddSingleton<UtilityService>()
.AddHostedService<GuildUpdateService>()
.AddHostedService<MemberUpdateService>() .AddHostedService<MemberUpdateService>()
.AddHostedService<ScheduledEventUpdateService>() .AddHostedService<ScheduledEventUpdateService>()
.AddHostedService<SongUpdateService>() .AddHostedService<SongUpdateService>()

View file

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

View file

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