57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
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)]
|
|
);
|
|
}
|
|
}
|