PluralKit/PluralKit.Bot/Commands/Misc.cs

209 lines
10 KiB
C#
Raw Normal View History

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;
2019-07-16 19:59:06 +00:00
using App.Metrics;
2019-12-22 11:50:47 +00:00
2020-04-24 19:50:28 +00:00
using DSharpPlus;
2019-12-22 11:50:47 +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-04-24 19:50:28 +00:00
using DSharpPlus.Entities;
2019-10-05 05:41:00 +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;
public Misc(BotConfig botConfig, IMetrics metrics, CpuStatService cpu, ShardInfoService shards, EmbedService embeds, ModelRepository repo, IDatabase db)
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;
2019-10-05 05:41:00 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task Invite(Context ctx)
{
2020-04-24 19:50:28 +00:00
var clientId = _botConfig.ClientId ?? ctx.Client.CurrentApplication.Id;
var permissions = new Permissions()
.Grant(Permissions.AddReactions)
.Grant(Permissions.AttachFiles)
.Grant(Permissions.EmbedLinks)
.Grant(Permissions.ManageMessages)
.Grant(Permissions.ManageWebhooks)
.Grant(Permissions.ReadMessageHistory)
.Grant(Permissions.SendMessages);
var invite = $"https://discord.com/oauth2/authorize?client_id={clientId}&scope=bot&permissions={(long)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
}
2019-10-05 05:41:00 +00:00
public async Task Stats(Context ctx)
2019-07-16 19:59:06 +00:00
{
var timeBefore = SystemClock.Instance.GetCurrentInstant();
2019-12-22 11:50:47 +00:00
var msg = await ctx.Reply($"...");
var timeAfter = SystemClock.Instance.GetCurrentInstant();
var apiLatency = timeAfter - timeBefore;
2019-12-22 11:50:47 +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
var totalSystems = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SystemCount.Name)?.Value ?? 0;
var totalMembers = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MemberCount.Name)?.Value ?? 0;
var totalSwitches = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SwitchCount.Name)?.Value ?? 0;
var totalMessages = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MessageCount.Name)?.Value ?? 0;
2019-12-22 11:50:47 +00:00
var shardId = ctx.Shard.ShardId;
2020-04-24 19:50:28 +00:00
var shardTotal = ctx.Client.ShardClients.Count;
var shardUpTotal = _shards.Shards.Where(x => x.Connected).Count();
2019-12-22 11:50:47 +00:00
var shardInfo = _shards.GetShardInfo(ctx.Shard);
2019-07-16 19:59:06 +00:00
2019-12-22 11:50:47 +00:00
var process = Process.GetCurrentProcess();
var memoryUsage = process.WorkingSet64;
var shardUptime = SystemClock.Instance.GetCurrentInstant() - shardInfo.LastConnectionTime;
var embed = new DiscordEmbedBuilder();
if (messagesReceived != null) embed.AddField("Messages processed",$"{messagesReceived.OneMinuteRate * 60:F1}/m ({messagesReceived.FifteenMinuteRate * 60:F1}/m over 15m)", true);
if (messagesProxied != null) embed.AddField("Messages proxied", $"{messagesProxied.OneMinuteRate * 60:F1}/m ({messagesProxied.FifteenMinuteRate * 60:F1}/m over 15m)", true);
if (commandsRun != null) embed.AddField("Commands executed", $"{commandsRun.OneMinuteRate * 60:F1}/m ({commandsRun.FifteenMinuteRate * 60:F1}/m over 15m)", true);
embed
2019-12-22 11:50:47 +00:00
.AddField("Current shard", $"Shard #{shardId} (of {shardTotal} total, {shardUpTotal} are up)", true)
.AddField("Shard uptime", $"{shardUptime.FormatDuration()} ({shardInfo.DisconnectionCount} disconnections)", true)
2019-12-22 13:37:55 +00:00
.AddField("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true)
2019-12-22 11:50:47 +00:00
.AddField("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true)
.AddField("Latency", $"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms", true)
2019-12-22 13:37:55 +00:00
.AddField("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalSwitches:N0} switches, {totalMessages:N0} messages");
2020-04-24 19:50:28 +00:00
await msg.ModifyAsync("", embed.Build());
2019-07-16 19:59:06 +00:00
}
2020-04-24 19:50:28 +00:00
2019-10-05 05:41:00 +00:00
public async Task PermCheckGuild(Context ctx)
{
2020-04-24 19:50:28 +00:00
DiscordGuild guild;
2020-07-25 11:25:10 +00:00
DiscordMember senderGuildUser = null;
if (ctx.Guild != null && !ctx.HasNext())
{
guild = ctx.Guild;
2020-07-25 11:17:34 +00:00
senderGuildUser = (DiscordMember)ctx.Author;
}
else
{
2020-07-25 10:25:45 +00:00
var guildIdStr = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a server ID or run this command in a server.");
if (!ulong.TryParse(guildIdStr, out var guildId))
throw new PKSyntaxError($"Could not parse {guildIdStr.AsCode()} as an ID.");
guild = ctx.Client.GetGuild(guildId);
2020-07-25 11:25:10 +00:00
if (guild != null) senderGuildUser = await guild.GetMember(ctx.Author.Id);
if (guild == null || senderGuildUser == null) throw Errors.GuildNotFound(guildId);
}
var requiredPermissions = new []
{
2020-04-24 19:50:28 +00:00
Permissions.AccessChannels,
Permissions.SendMessages,
Permissions.AddReactions,
Permissions.AttachFiles,
Permissions.EmbedLinks,
Permissions.ManageMessages,
Permissions.ManageWebhooks
};
// Loop through every channel and group them by sets of permissions missing
2020-04-24 19:50:28 +00:00
var permissionsMissing = new Dictionary<ulong, List<DiscordChannel>>();
var hiddenChannels = 0;
foreach (var channel in await guild.GetChannelsAsync())
{
var botPermissions = channel.BotPermissions();
var userPermissions = senderGuildUser.PermissionsIn(channel);
if ((userPermissions & Permissions.AccessChannels) == 0)
{
// If the user can't see this channel, don't calculate permissions for it
// (to prevent info-leaking, mostly)
// Instead, count how many hidden channels and show the user (so they don't get confused)
hiddenChannels++;
continue;
}
// We use a bitfield so we can set individual permission bits in the loop
// TODO: Rewrite with proper bitfield math
ulong missingPermissionField = 0;
foreach (var requiredPermission in requiredPermissions)
if ((botPermissions & requiredPermission) == 0)
missingPermissionField |= (ulong) requiredPermission;
// If we're not missing any permissions, don't bother adding it to the dict
// This means we can check if the dict is empty to see if all channels are proxyable
if (missingPermissionField != 0)
{
2020-04-24 19:50:28 +00:00
permissionsMissing.TryAdd(missingPermissionField, new List<DiscordChannel>());
permissionsMissing[missingPermissionField].Add(channel);
}
}
// Generate the output embed
2020-04-24 19:50:28 +00:00
var eb = new DiscordEmbedBuilder()
.WithTitle($"Permission check for **{guild.Name}**");
if (permissionsMissing.Count == 0)
{
2020-04-28 22:25:01 +00:00
eb.WithDescription($"No errors found, all channels proxyable :)").WithColor(DiscordUtils.Green);
}
else
{
foreach (var (missingPermissionField, channels) in permissionsMissing)
{
// Each missing permission field can have multiple missing channels
// so we extract them all and generate a comma-separated list
2020-04-24 19:50:28 +00:00
var missingPermissionNames = ((Permissions)missingPermissionField).ToPermissionString();
var channelsList = string.Join("\n", channels
.OrderBy(c => c.Position)
.Select(c => $"#{c.Name}"));
eb.AddField($"Missing *{missingPermissionNames}*", channelsList.Truncate(1000));
2020-04-28 22:25:01 +00:00
eb.WithColor(DiscordUtils.Red);
}
}
if (hiddenChannels > 0)
eb.WithFooter($"{"channel".ToQuantity(hiddenChannels)} were ignored as you do not have view access to them.");
// Send! :)
2019-10-05 05:41:00 +00:00
await ctx.Reply(embed: eb.Build());
}
2020-02-01 12:03:02 +00:00
public async Task GetMessage(Context ctx)
{
var word = ctx.PopArgument() ?? throw new PKSyntaxError("You must pass a message ID or link.");
ulong messageId;
if (ulong.TryParse(word, out var id))
messageId = id;
else if (Regex.Match(word, "https://discord(?:app)?.com/channels/\\d+/\\d+/(\\d+)") is Match match && match.Success)
2020-02-01 12:03:02 +00:00
messageId = ulong.Parse(match.Groups[1].Value);
else throw new PKSyntaxError($"Could not parse {word.AsCode()} as a message ID or link.");
2020-02-01 12:03:02 +00:00
2020-08-29 11:46:27 +00:00
var message = await _db.Execute(c => _repo.GetMessage(c, messageId));
2020-02-01 12:03:02 +00:00
if (message == null) throw Errors.MessageNotFound(messageId);
2020-04-24 21:48:10 +00:00
await ctx.Reply(embed: await _embeds.CreateMessageInfoEmbed(ctx.Shard, message));
2020-02-01 12:03:02 +00:00
}
2019-04-29 18:21:16 +00:00
}
}