feat: shard status update, delete old messages when they expire

This commit is contained in:
sam 2024-10-13 17:08:32 +02:00
parent 8e030acaf3
commit 32732d74d0
9 changed files with 302 additions and 17 deletions

View file

@ -0,0 +1,57 @@
using Catalogger.Backend.Bot;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Gateway.Commands;
using Remora.Discord.API.Objects;
namespace Catalogger.Backend.Services;
public class StatusUpdateService(ILogger logger, ShardedGatewayClient shardedClient)
: BackgroundService
{
private readonly ILogger _logger = logger.ForContext<StatusUpdateService>();
protected override async Task ExecuteAsync(CancellationToken ct)
{
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(3));
while (await timer.WaitForNextTickAsync(ct))
UpdateShardStatuses(ct);
}
private void UpdateShardStatuses(CancellationToken ct = default)
{
_logger.Information(
"Updating status for {TotalShards} shards. Guild count is {GuildCount}",
shardedClient.TotalShards,
CataloggerMetrics.GuildsCached.Value
);
foreach (var (shardId, client) in shardedClient.Shards)
{
if (!ShardedGatewayClient.IsConnected(client))
{
_logger.Warning(
"Cannot update status for shard {ShardId} as it is not connected",
shardId
);
continue;
}
client.SubmitCommand(PresenceFor(shardId));
}
}
private UpdatePresence PresenceFor(int shardId)
{
var status = $"/catalogger help | in {CataloggerMetrics.GuildsCached.Value} servers";
if (shardedClient.TotalShards != 1)
status += $" | shard {shardId + 1}/{shardedClient.TotalShards}";
return new UpdatePresence(
Status: UserStatus.Online,
IsAFK: false,
Since: null,
Activities: [new Activity(Name: "Beep", Type: ActivityType.Custom, State: status)]
);
}
}