1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-20 00:43:36 +03:00
Octobot/Services/GuildUpdateService.cs
Octol1ttle f4738e5dd4
Do not clear BannedUntil if automatic unbanning wasn't successful
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
2023-06-11 17:01:13 +05:00

42 lines
1.5 KiB
C#

using Boyfriend.Services.Data;
using Microsoft.Extensions.Hosting;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Rest.Core;
namespace Boyfriend.Services;
public class GuildUpdateService : BackgroundService {
private readonly GuildDataService _dataService;
private readonly IDiscordRestGuildAPI _guildApi;
public GuildUpdateService(GuildDataService dataService, IDiscordRestGuildAPI guildApi) {
_dataService = dataService;
_guildApi = guildApi;
}
protected override async Task ExecuteAsync(CancellationToken ct) {
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
var tasks = new List<Task>();
while (await timer.WaitForNextTickAsync(ct)) {
foreach (var id in _dataService.GetGuildIds())
tasks.Add(TickGuildAsync(id, ct));
await Task.WhenAll(tasks);
tasks.Clear();
}
}
private async Task TickGuildAsync(Snowflake guildId, CancellationToken ct = default) {
var data = await _dataService.GetData(guildId, ct);
Messages.Culture = data.Culture;
foreach (var memberData in data.MemberData.Values)
if (DateTimeOffset.UtcNow > memberData.BannedUntil) {
var unbanResult = await _guildApi.RemoveGuildBanAsync(
guildId, memberData.Id.ToDiscordSnowflake(), Messages.PunishmentExpired.EncodeHeader(), ct);
if (unbanResult.IsSuccess)
memberData.BannedUntil = null;
}
}
}