2024-10-14 14:56:40 +02:00
|
|
|
// Copyright (C) 2021-present sam (starshines.gay)
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-10-13 17:08:32 +02:00
|
|
|
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;
|
|
|
|
|
|
2024-10-29 20:19:49 +01:00
|
|
|
public class StatusUpdateService(ILogger logger, ShardedGatewayClient shardedClient, Config config)
|
2024-10-13 17:08:32 +02:00
|
|
|
: 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
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-29 20:19:49 +01:00
|
|
|
if (config.Discord.TestMode)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Not updating shard statuses because test mode is enabled.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-13 17:08:32 +02:00
|
|
|
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)]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|