feat: add sharding support

This commit is contained in:
sam 2024-08-24 19:02:19 +02:00
parent 8f39d85486
commit c42ca3f888
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
7 changed files with 167 additions and 9 deletions

View file

@ -11,7 +11,9 @@ using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
using Remora.Discord.Extensions.Embeds;
using Remora.Discord.Gateway;
using Remora.Results;
@ -25,8 +27,9 @@ public class MetaCommands(
ILogger logger,
IClock clock,
Config config,
DiscordGatewayClient client,
ShardedGatewayClient client,
IFeedbackService feedbackService,
ContextInjectionService contextInjection,
GuildCache guildCache,
ChannelCache channelCache,
IDiscordRestChannelAPI channelApi) : CommandGroup
@ -38,6 +41,13 @@ public class MetaCommands(
[Description("Ping pong! See the bot's latency")]
public async Task<IResult> PingAsync()
{
var shardId = contextInjection.Context?.TryGetGuildID(out var guildId) == true
? client.ShardIdFor(guildId.Value)
: 0;
var averageLatency = client.Shards.Values.Select(x => x.Latency.TotalMilliseconds).Sum() /
client.Shards.Count;
var t1 = clock.GetCurrentInstant();
var msg = await feedbackService.SendContextualAsync("...").GetOrThrow();
var elapsed = clock.GetCurrentInstant() - t1;
@ -49,7 +59,9 @@ public class MetaCommands(
.WithColour(DiscordUtils.Purple)
.WithFooter($"{RuntimeInformation.FrameworkDescription} on {RuntimeInformation.RuntimeIdentifier}")
.WithCurrentTimestamp();
embed.AddField("Ping", $"Gateway: {client.Latency.Humanize()}\nAPI: {elapsed.ToTimeSpan().Humanize()}",
embed.AddField("Ping",
$"Gateway: {client.Shards[shardId].Latency.TotalMilliseconds:N0}ms (average: {averageLatency:N0}ms)\n" +
$"API: {elapsed.TotalMilliseconds:N0}ms",
inline: true);
embed.AddField("Memory usage", memoryUsage.Bytes().Humanize(), inline: true);
@ -60,16 +72,18 @@ public class MetaCommands(
: $"{CataloggerMetrics.MessagesReceived.Value:N0} since last restart",
true);
embed.AddField("Numbers",
$"{CataloggerMetrics.MessagesStored.Value:N0} messages " +
$"from {guildCache.Size:N0} servers\nCached {channelCache.Size:N0} channels",
true);
embed.AddField("Shard", $"{shardId + 1} of {client.Shards.Count}", true);
embed.AddField("Uptime",
$"{(CataloggerMetrics.Startup - clock.GetCurrentInstant()).Prettify(TimeUnit.Second)}\n" +
$"since <t:{CataloggerMetrics.Startup.ToUnixTimeSeconds()}:F>",
true);
embed.AddField("Numbers",
$"{CataloggerMetrics.MessagesStored.Value:N0} messages " +
$"from {guildCache.Size:N0} servers\nCached {channelCache.Size:N0} channels",
false);
IEmbed[] embeds = [embed.Build().GetOrThrow()];
return (Result)await channelApi.EditMessageAsync(msg.ChannelID, msg.ID, content: "", embeds: embeds);