mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-04-19 16:33:36 +03:00
Add GuildUpdateService
Signed-off-by: Macintosh II <mctaylxrs@outlook.com>
This commit is contained in:
parent
7cf200d8de
commit
3e0be7eb93
3 changed files with 58 additions and 0 deletions
|
@ -89,6 +89,7 @@ public sealed class Octobot
|
|||
// Services
|
||||
.AddSingleton<GuildDataService>()
|
||||
.AddSingleton<UtilityService>()
|
||||
.AddHostedService<GuildUpdateService>()
|
||||
.AddHostedService<MemberUpdateService>()
|
||||
.AddHostedService<ScheduledEventUpdateService>()
|
||||
.AddHostedService<SongUpdateService>()
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text.Json.Nodes;
|
|||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Octobot.Data;
|
||||
using Remora.Discord.API.Objects;
|
||||
using Remora.Rest.Core;
|
||||
|
||||
namespace Octobot.Services;
|
||||
|
@ -142,4 +143,9 @@ public sealed class GuildDataService : IHostedService
|
|||
{
|
||||
return _datas.Keys;
|
||||
}
|
||||
|
||||
public Task RemoveGuildId(Snowflake id)
|
||||
{
|
||||
return Task.FromResult(_datas.TryRemove(id, out _));
|
||||
}
|
||||
}
|
||||
|
|
51
src/Services/Update/GuildUpdateService.cs
Normal file
51
src/Services/Update/GuildUpdateService.cs
Normal file
|
@ -0,0 +1,51 @@
|
|||
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<MemberUpdateService> _logger;
|
||||
|
||||
public GuildUpdateService(IDiscordRestGuildAPI guildApi,
|
||||
GuildDataService guildData, ILogger<MemberUpdateService> 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 guildData = await _guildApi.GetGuildAsync(guildId, ct: ct);
|
||||
if (guildData.IsSuccess)
|
||||
{
|
||||
return Result.FromSuccess();
|
||||
}
|
||||
|
||||
await _guildData.RemoveGuildId(guildId);
|
||||
_logger.LogInformation("Left guild \"{guildId}\"", guildId);
|
||||
|
||||
return Result.FromSuccess();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue