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