2019-12-22 11:50:47 +00:00
|
|
|
using System.Diagnostics;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2019-07-16 19:59:06 +00:00
|
|
|
using App.Metrics;
|
2019-12-22 11:50:47 +00:00
|
|
|
|
2020-12-24 13:52:44 +00:00
|
|
|
using Myriad.Builders;
|
|
|
|
using Myriad.Cache;
|
|
|
|
using Myriad.Gateway;
|
|
|
|
using Myriad.Rest;
|
|
|
|
using Myriad.Rest.Types.Requests;
|
|
|
|
using Myriad.Types;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class Misc
|
2021-08-27 15:03:47 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly BotConfig _botConfig;
|
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
private readonly CpuStatService _cpu;
|
|
|
|
private readonly IMetrics _metrics;
|
|
|
|
private readonly ModelRepository _repo;
|
|
|
|
private readonly ShardInfoService _shards;
|
|
|
|
|
|
|
|
public Misc(BotConfig botConfig, IMetrics metrics, CpuStatService cpu, ShardInfoService shards,
|
2021-11-27 03:02:58 +00:00
|
|
|
ModelRepository repo, IDiscordCache cache)
|
2019-10-05 05:41:00 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_botConfig = botConfig;
|
|
|
|
_metrics = metrics;
|
|
|
|
_cpu = cpu;
|
|
|
|
_shards = shards;
|
|
|
|
_repo = repo;
|
|
|
|
_cache = cache;
|
|
|
|
}
|
2020-04-24 19:50:28 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task Invite(Context ctx)
|
|
|
|
{
|
|
|
|
var clientId = _botConfig.ClientId ?? await _cache.GetOwnUser();
|
|
|
|
|
|
|
|
var permissions =
|
|
|
|
PermissionSet.AddReactions |
|
|
|
|
PermissionSet.AttachFiles |
|
|
|
|
PermissionSet.EmbedLinks |
|
|
|
|
PermissionSet.ManageMessages |
|
|
|
|
PermissionSet.ManageWebhooks |
|
|
|
|
PermissionSet.ReadMessageHistory |
|
|
|
|
PermissionSet.SendMessages;
|
|
|
|
|
|
|
|
var invite =
|
|
|
|
$"https://discord.com/oauth2/authorize?client_id={clientId}&scope=bot%20applications.commands&permissions={(ulong)permissions}";
|
2021-11-27 16:09:08 +00:00
|
|
|
|
|
|
|
var botName = _botConfig.IsBetaBot ? "PluralKit Beta" : "PluralKit";
|
|
|
|
await ctx.Reply($"{Emojis.Success} Use this link to add {botName} to your server:\n<{invite}>");
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Stats(Context ctx)
|
|
|
|
{
|
|
|
|
var timeBefore = SystemClock.Instance.GetCurrentInstant();
|
|
|
|
var msg = await ctx.Reply("...");
|
|
|
|
var timeAfter = SystemClock.Instance.GetCurrentInstant();
|
|
|
|
var apiLatency = timeAfter - timeBefore;
|
|
|
|
|
2022-01-19 22:18:31 +00:00
|
|
|
var embed = new EmbedBuilder();
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var messagesReceived = _metrics.Snapshot.GetForContext("Bot").Meters
|
|
|
|
.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.MessagesReceived.Name)?.Value;
|
2022-01-19 22:18:31 +00:00
|
|
|
if (messagesReceived != null)
|
|
|
|
embed.Field(new Embed.Field("Messages processed",
|
|
|
|
$"{messagesReceived.OneMinuteRate * 60:F1}/m ({messagesReceived.FifteenMinuteRate * 60:F1}/m over 15m)",
|
|
|
|
true));
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var messagesProxied = _metrics.Snapshot.GetForContext("Bot").Meters
|
|
|
|
.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.MessagesProxied.Name)?.Value;
|
2022-01-19 22:18:31 +00:00
|
|
|
if (messagesProxied != null)
|
|
|
|
embed.Field(new Embed.Field("Messages proxied",
|
|
|
|
$"{messagesProxied.OneMinuteRate * 60:F1}/m ({messagesProxied.FifteenMinuteRate * 60:F1}/m over 15m)",
|
|
|
|
true));
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var commandsRun = _metrics.Snapshot.GetForContext("Bot").Meters
|
|
|
|
.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.CommandsRun.Name)?.Value;
|
2022-01-19 22:18:31 +00:00
|
|
|
if (commandsRun != null)
|
|
|
|
embed.Field(new Embed.Field("Commands executed",
|
|
|
|
$"{commandsRun.OneMinuteRate * 60:F1}/m ({commandsRun.FifteenMinuteRate * 60:F1}/m over 15m)",
|
|
|
|
true));
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
var counts = await _repo.GetStats();
|
|
|
|
|
2022-01-15 02:51:43 +00:00
|
|
|
var shardId = ctx.ShardId;
|
2021-11-27 02:10:56 +00:00
|
|
|
var shardTotal = ctx.Cluster.Shards.Count;
|
|
|
|
var shardUpTotal = _shards.Shards.Where(x => x.Connected).Count();
|
2022-01-15 02:51:43 +00:00
|
|
|
var shardInfo = _shards.GetShardInfo(ctx.ShardId);
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
var process = Process.GetCurrentProcess();
|
|
|
|
var memoryUsage = process.WorkingSet64;
|
|
|
|
|
|
|
|
var now = SystemClock.Instance.GetCurrentInstant();
|
|
|
|
var shardUptime = now - shardInfo.LastConnectionTime;
|
|
|
|
|
|
|
|
embed
|
|
|
|
.Field(new Embed.Field("Current shard",
|
|
|
|
$"Shard #{shardId} (of {shardTotal} total, {shardUpTotal} are up)", true))
|
|
|
|
.Field(new Embed.Field("Shard uptime",
|
|
|
|
$"{shardUptime.FormatDuration()} ({shardInfo.DisconnectionCount} disconnections)", true))
|
|
|
|
.Field(new Embed.Field("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true))
|
|
|
|
.Field(new Embed.Field("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true))
|
|
|
|
.Field(new Embed.Field("Latency",
|
|
|
|
$"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms",
|
|
|
|
true))
|
|
|
|
.Field(new Embed.Field("Total numbers", $" {counts.SystemCount:N0} systems,"
|
|
|
|
+ $" {counts.MemberCount:N0} members,"
|
|
|
|
+ $" {counts.GroupCount:N0} groups,"
|
|
|
|
+ $" {counts.SwitchCount:N0} switches,"
|
|
|
|
+ $" {counts.MessageCount:N0} messages"))
|
|
|
|
.Timestamp(process.StartTime.ToString("O"))
|
|
|
|
.Footer(new Embed.EmbedFooter(
|
|
|
|
$"PluralKit {BuildInfoService.Version} • https://github.com/xSke/PluralKit • Last restarted: "));
|
|
|
|
;
|
|
|
|
await ctx.Rest.EditMessage(msg.ChannelId, msg.Id,
|
|
|
|
new MessageEditRequest { Content = "", Embed = embed.Build() });
|
2019-04-29 18:21:16 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
}
|