Catalogger.NET/Catalogger.Backend/Bot/Commands/MetaCommands.cs

89 lines
3.5 KiB
C#
Raw Normal View History

2024-08-13 13:08:50 +02:00
using System.ComponentModel;
2024-08-13 16:48:54 +02:00
using System.Diagnostics;
2024-08-13 17:02:11 +02:00
using System.Runtime.InteropServices;
using System.Text.Json;
using App.Metrics;
2024-08-13 16:48:54 +02:00
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
2024-08-13 16:48:54 +02:00
using Catalogger.Backend.Database;
2024-08-13 13:08:50 +02:00
using Catalogger.Backend.Extensions;
2024-08-13 16:48:54 +02:00
using Humanizer;
using Microsoft.EntityFrameworkCore;
2024-08-13 13:08:50 +02:00
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
2024-08-13 17:02:11 +02:00
using Remora.Discord.API.Abstractions.Objects;
2024-08-13 13:08:50 +02:00
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Objects;
using Remora.Discord.Commands.Contexts;
using Remora.Discord.Commands.Extensions;
2024-08-13 13:08:50 +02:00
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
2024-08-13 16:48:54 +02:00
using Remora.Discord.Extensions.Embeds;
2024-08-13 13:08:50 +02:00
using Remora.Discord.Gateway;
using Remora.Results;
using IClock = NodaTime.IClock;
2024-08-13 13:08:50 +02:00
using IResult = Remora.Results.IResult;
namespace Catalogger.Backend.Bot.Commands;
[Group("catalogger")]
public class MetaCommands(
IClock clock,
IMetrics metrics,
2024-08-13 13:08:50 +02:00
DiscordGatewayClient client,
IFeedbackService feedbackService,
ContextInjectionService contextInjection,
IInviteCache inviteCache,
GuildCache guildCache,
ChannelCache channelCache,
2024-08-13 13:08:50 +02:00
IDiscordRestChannelAPI channelApi) : CommandGroup
{
[Command("ping")]
[Description("Ping pong! See the bot's latency")]
public async Task<IResult> PingAsync()
{
var t1 = clock.GetCurrentInstant();
var msg = await feedbackService.SendContextualAsync("...").GetOrThrow();
var elapsed = clock.GetCurrentInstant() - t1;
2024-08-13 16:48:54 +02:00
var process = Process.GetCurrentProcess();
var memoryUsage = process.WorkingSet64;
var embed = new EmbedBuilder()
.WithColour(DiscordUtils.Purple)
2024-08-13 17:02:11 +02:00
.WithFooter($"{RuntimeInformation.FrameworkDescription} on {RuntimeInformation.RuntimeIdentifier}")
2024-08-13 16:48:54 +02:00
.WithCurrentTimestamp();
embed.AddField("Ping", $"Gateway: {client.Latency.Humanize()}\nAPI: {elapsed.ToTimeSpan().Humanize()}",
inline: true);
2024-08-13 17:02:11 +02:00
embed.AddField("Memory usage", memoryUsage.Bytes().Humanize(), inline: true);
2024-08-13 16:48:54 +02:00
var messagesReceived = metrics.Snapshot.GetForContext("Bot").Meters
.FirstOrDefault(m => m.MultidimensionalName == CataloggerMetrics.MessagesReceived.Name)?.Value;
if (messagesReceived != null)
embed.AddField("Messages received", $"{messagesReceived.OneMinuteRate * 60:F1}/m", true);
var messageCount = metrics.Snapshot.GetForContext("Bot").Gauges
.FirstOrDefault(m => m.MultidimensionalName == CataloggerMetrics.MessagesStored.Name)?.Value ?? 0;
2024-08-13 16:48:54 +02:00
embed.AddField("Numbers",
$"{messageCount:N0} messages from {guildCache.Size:N0} servers\nCached {channelCache.Size:N0} channels",
inline: false);
IEmbed[] embeds = [embed.Build().GetOrThrow()];
2024-08-13 17:02:11 +02:00
return (Result)await channelApi.EditMessageAsync(msg.ChannelID, msg.ID, content: "", embeds: embeds);
2024-08-13 13:08:50 +02:00
}
[Command("debug-invites")]
[Description("Show a representation of this server's invites")]
public async Task<IResult> DebugInvitesAsync()
{
if (contextInjection.Context is not IInteractionCommandContext ctx) throw new CataloggerError("No context");
if (!ctx.TryGetGuildID(out var guildId)) throw new CataloggerError("No guild ID in context");
var invites = await inviteCache.TryGetAsync(guildId);
var text = invites.Select(i => $"{i.Code} in {i.Channel?.ID.Value}");
return await feedbackService.SendContextualAsync(string.Join("\n", text));
}
2024-08-13 13:08:50 +02:00
}