From 9216952bdb35491bb0d4d554768f1169591153fe Mon Sep 17 00:00:00 2001 From: Jasper Christiansen Date: Tue, 5 Nov 2024 17:35:48 +0100 Subject: [PATCH] Added simple stats command --- KanbanCord/Commands/HelpCommand.cs | 1 + KanbanCord/Commands/StatsCommand.cs | 39 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 KanbanCord/Commands/StatsCommand.cs diff --git a/KanbanCord/Commands/HelpCommand.cs b/KanbanCord/Commands/HelpCommand.cs index a9c1734..8278225 100644 --- a/KanbanCord/Commands/HelpCommand.cs +++ b/KanbanCord/Commands/HelpCommand.cs @@ -25,6 +25,7 @@ public async ValueTask ExecuteAsync(SlashCommandContext context) (commands.GetMention(["clear"]), commands.GetDescription(["clear"])), (commands.GetMention(["reset"]), commands.GetDescription(["reset"])), (commands.GetMention(["repository"]), commands.GetDescription(["repository"])), + (commands.GetMention(["stats"]), commands.GetDescription(["stats"])), (commands.GetMention(["task", "add"]), commands.GetDescription(["task", "add"])), (commands.GetMention(["task", "edit"]), commands.GetDescription(["task", "edit"])), (commands.GetMention(["task", "delete"]), commands.GetDescription(["task", "delete"])), diff --git a/KanbanCord/Commands/StatsCommand.cs b/KanbanCord/Commands/StatsCommand.cs new file mode 100644 index 0000000..81d2e32 --- /dev/null +++ b/KanbanCord/Commands/StatsCommand.cs @@ -0,0 +1,39 @@ +using System.ComponentModel; +using System.Diagnostics; +using DSharpPlus.Commands; +using DSharpPlus.Commands.Processors.SlashCommands; +using DSharpPlus.Entities; +using KanbanCord.Extensions; +using KanbanCord.Helpers; + +namespace KanbanCord.Commands; + +public class StatsCommand +{ + [Command("stats")] + [Description("Displays the stats of the bot.")] + public async ValueTask ExecuteAsync(SlashCommandContext context) + { + var botVersion = EnvironmentHelpers.GetApplicationVersion(); + + GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true); + var heapMemory = $"{GC.GetTotalMemory(true) / 1024 / 1024:n0} MB"; + + using var process = Process.GetCurrentProcess(); + var uptime = DateTimeOffset.UtcNow.Subtract(process.StartTime); + var uptimeDays = uptime.Days; + var remainingHours = uptime.Hours % 24; + + var latency = context.Client.GetConnectionLatency(context.Guild!.Id).Milliseconds; + + var embed = new DiscordEmbedBuilder() + .WithDefaultColor() + .WithThumbnail(context.Client.CurrentUser.AvatarUrl) + .AddField("Latency:", latency == 0 ? "waiting for heartbeat..." : $"{latency} ms") + .AddField("Memory Usage:", heapMemory) + .AddField("Uptime:", $"{uptimeDays} days, {remainingHours} hours") + .AddField("Bot Version:", botVersion); + + await context.RespondAsync(embed); + } +} \ No newline at end of file