Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Develop #63

Merged
merged 13 commits into from
May 29, 2024
36 changes: 36 additions & 0 deletions Boolean/Modules/BotInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using Boolean.Util;
using Discord;
using Discord.Interactions;
Expand Down Expand Up @@ -57,4 +58,39 @@ public async Task Contribute()

await RespondAsync(embed: embed.Build(), ephemeral: true);
}

// We might later want to consider making this maintainers only (as people could use this to exploit the bot)
[SlashCommand("status", "Shows the bot's compute usage (CPU, RAM, etc)")]
public async Task Status()
{
var botProcess = Process.GetCurrentProcess();
var ramUsageGb = botProcess.WorkingSet64 / (float) Math.Pow(1024, 3);

// Calculate the CPU usage
var startTime = DateTime.UtcNow;
var startCpuUsage = botProcess.TotalProcessorTime;

await Task.Delay(500);

var endTime = DateTime.UtcNow;
var endCpuUsage = botProcess.TotalProcessorTime;

var cpuUsage = (float) (endCpuUsage - startCpuUsage).TotalMilliseconds
/ (float) (Environment.ProcessorCount * (endTime - startTime).TotalMilliseconds);

var embed = new EmbedBuilder
{
Title = "Bot Status",
Color = EmbedColors.Normal,
};

var uptime = DateTime.Now - botProcess.StartTime;

embed
.AddField("RAM", $"`{Math.Round(ramUsageGb, 2)} GB`", true)
.AddField("CPU Usage", $"`{Math.Round(cpuUsage * 100, 2)}%`", true)
.AddField("Up Time", $"{uptime.Days} days, {uptime.Hours} hours, {uptime.Minutes} minutes, {uptime.Seconds} seconds", false);

await RespondAsync(embed: embed.Build(), ephemeral: true);
}
}
Loading