2021-08-25 18:36:13 +00:00
|
|
|
using Humanizer;
|
|
|
|
|
|
|
|
using Myriad.Builders;
|
|
|
|
using Myriad.Cache;
|
|
|
|
using Myriad.Extensions;
|
|
|
|
using Myriad.Rest;
|
|
|
|
using Myriad.Rest.Exceptions;
|
|
|
|
using Myriad.Types;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class Checks
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly BotConfig _botConfig;
|
2022-03-10 01:06:53 +00:00
|
|
|
// this must ONLY be used to get the bot's user ID
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
private readonly ProxyMatcher _matcher;
|
|
|
|
private readonly ProxyService _proxy;
|
|
|
|
private readonly DiscordApiClient _rest;
|
|
|
|
|
|
|
|
private readonly PermissionSet[] requiredPermissions =
|
|
|
|
{
|
|
|
|
PermissionSet.ViewChannel, PermissionSet.SendMessages, PermissionSet.AddReactions,
|
|
|
|
PermissionSet.AttachFiles, PermissionSet.EmbedLinks, PermissionSet.ManageMessages,
|
2022-01-07 19:22:44 +00:00
|
|
|
PermissionSet.ManageWebhooks, PermissionSet.ReadMessageHistory
|
2021-11-27 02:10:56 +00:00
|
|
|
};
|
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
// todo: make sure everything uses the minimum amount of REST calls necessary
|
2022-01-22 08:05:01 +00:00
|
|
|
public Checks(DiscordApiClient rest, IDiscordCache cache, BotConfig botConfig, ProxyService proxy, ProxyMatcher matcher)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
_rest = rest;
|
|
|
|
_cache = cache;
|
|
|
|
_botConfig = botConfig;
|
|
|
|
_proxy = proxy;
|
|
|
|
_matcher = matcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task PermCheckGuild(Context ctx)
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
Guild guild;
|
|
|
|
GuildMemberPartial senderGuildUser = null;
|
|
|
|
|
|
|
|
if (ctx.Guild != null && !ctx.HasNext())
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
guild = ctx.Guild;
|
|
|
|
senderGuildUser = ctx.Member;
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
else
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +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.");
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
guild = await _rest.GetGuild(guildId);
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (ForbiddenException)
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
throw Errors.GuildNotFound(guildId);
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (guild != null)
|
|
|
|
senderGuildUser = await _rest.GetGuildMember(guildId, ctx.Author.Id);
|
|
|
|
if (guild == null || senderGuildUser == null)
|
|
|
|
throw Errors.GuildNotFound(guildId);
|
|
|
|
}
|
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
var guildMember = await _rest.GetGuildMember(guild.Id, await _cache.GetOwnUser());
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Loop through every channel and group them by sets of permissions missing
|
|
|
|
var permissionsMissing = new Dictionary<ulong, List<Channel>>();
|
|
|
|
var hiddenChannels = false;
|
|
|
|
var missingEmojiPermissions = false;
|
|
|
|
foreach (var channel in await _rest.GetGuildChannels(guild.Id))
|
|
|
|
{
|
2022-03-10 01:06:53 +00:00
|
|
|
var botPermissions = PermissionExtensions.PermissionsFor(guild, channel, await _cache.GetOwnUser(), guildMember);
|
|
|
|
var webhookPermissions = PermissionExtensions.EveryonePermissions(guild, channel);
|
|
|
|
var userPermissions = PermissionExtensions.PermissionsFor(guild, channel, ctx.Author.Id, senderGuildUser);
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
if ((userPermissions & PermissionSet.ViewChannel) == 0)
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
// If the user can't see this channel, don't calculate permissions for it
|
|
|
|
// (to prevent info-leaking, mostly)
|
|
|
|
// Instead, show the user that some channels got ignored (so they don't get confused)
|
|
|
|
hiddenChannels = true;
|
|
|
|
continue;
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// We use a bitfield so we can set individual permission bits in the loop
|
|
|
|
// TODO: Rewrite with proper bitfield math
|
|
|
|
ulong missingPermissionField = 0;
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
foreach (var requiredPermission in requiredPermissions)
|
|
|
|
if ((botPermissions & requiredPermission) == 0)
|
|
|
|
missingPermissionField |= (ulong)requiredPermission;
|
|
|
|
|
|
|
|
if ((webhookPermissions & PermissionSet.UseExternalEmojis) == 0)
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
missingPermissionField |= (ulong)PermissionSet.UseExternalEmojis;
|
|
|
|
missingEmojiPermissions = true;
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
// 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)
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
permissionsMissing.TryAdd(missingPermissionField, new List<Channel>());
|
|
|
|
permissionsMissing[missingPermissionField].Add(channel);
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Generate the output embed
|
|
|
|
var eb = new EmbedBuilder()
|
|
|
|
.Title($"Permission check for **{guild.Name}**");
|
|
|
|
|
|
|
|
if (permissionsMissing.Count == 0)
|
|
|
|
eb.Description("No errors found, all channels proxyable :)").Color(DiscordUtils.Green);
|
|
|
|
else
|
|
|
|
foreach (var (missingPermissionField, channels) in permissionsMissing)
|
2021-08-25 19:58:55 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
// Each missing permission field can have multiple missing channels
|
|
|
|
// so we extract them all and generate a comma-separated list
|
|
|
|
var missingPermissionNames = ((PermissionSet)missingPermissionField).ToPermissionString();
|
|
|
|
|
|
|
|
var channelsList = string.Join("\n", channels
|
|
|
|
.OrderBy(c => c.Position)
|
|
|
|
.Select(c => $"#{c.Name}"));
|
|
|
|
eb.Field(new Embed.Field($"Missing *{missingPermissionNames}*", channelsList.Truncate(1000)));
|
|
|
|
eb.Color(DiscordUtils.Red);
|
2021-08-25 19:58:55 +00:00
|
|
|
}
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var footer = "";
|
|
|
|
if (hiddenChannels)
|
|
|
|
footer += "Some channels were ignored as you do not have view access to them.";
|
|
|
|
if (missingEmojiPermissions)
|
|
|
|
{
|
|
|
|
if (hiddenChannels) footer += " | ";
|
|
|
|
footer +=
|
|
|
|
"Use External Emojis permissions must be granted to the @everyone role / Default Permissions.";
|
2021-09-13 08:21:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (footer.Length > 0)
|
|
|
|
eb.Footer(new Embed.EmbedFooter(footer));
|
|
|
|
|
|
|
|
// Send! :)
|
|
|
|
await ctx.Reply(embed: eb.Build());
|
|
|
|
}
|
2021-09-13 12:42:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task PermCheckChannel(Context ctx)
|
|
|
|
{
|
|
|
|
if (!ctx.HasNext())
|
|
|
|
throw new PKSyntaxError("You need to specify a channel.");
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var error = "Channel not found or you do not have permissions to access it.";
|
2022-03-15 03:33:22 +00:00
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
// todo: this breaks if channel is not in cache and bot does not have View Channel permissions
|
2021-11-27 02:10:56 +00:00
|
|
|
var channel = await ctx.MatchChannel();
|
|
|
|
if (channel == null || channel.GuildId == null)
|
|
|
|
throw new PKError(error);
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
var guild = await _rest.GetGuildOrNull(channel.GuildId.Value);
|
|
|
|
if (guild == null)
|
|
|
|
throw new PKError(error);
|
|
|
|
|
|
|
|
var guildMember = await _rest.GetGuildMember(channel.GuildId.Value, await _cache.GetOwnUser());
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!await ctx.CheckPermissionsInGuildChannel(channel, PermissionSet.ViewChannel))
|
|
|
|
throw new PKError(error);
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
var botPermissions = PermissionExtensions.PermissionsFor(guild, channel, await _cache.GetOwnUser(), guildMember);
|
|
|
|
var webhookPermissions = PermissionExtensions.EveryonePermissions(guild, channel);
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// We use a bitfield so we can set individual permission bits
|
|
|
|
ulong missingPermissions = 0;
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
foreach (var requiredPermission in requiredPermissions)
|
|
|
|
if ((botPermissions & requiredPermission) == 0)
|
|
|
|
missingPermissions |= (ulong)requiredPermission;
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if ((webhookPermissions & PermissionSet.UseExternalEmojis) == 0)
|
|
|
|
missingPermissions |= (ulong)PermissionSet.UseExternalEmojis;
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Generate the output embed
|
|
|
|
var eb = new EmbedBuilder()
|
|
|
|
.Title($"Permission check for **{channel.Name}**");
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (missingPermissions == 0)
|
|
|
|
{
|
|
|
|
eb.Description("No issues found, channel is proxyable :)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var missing = "";
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
foreach (var permission in requiredPermissions)
|
|
|
|
if (((ulong)permission & missingPermissions) == (ulong)permission)
|
|
|
|
missing += $"\n- **{permission.ToPermissionString()}**";
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (((ulong)PermissionSet.UseExternalEmojis & missingPermissions) ==
|
|
|
|
(ulong)PermissionSet.UseExternalEmojis)
|
|
|
|
missing += $"\n- **{PermissionSet.UseExternalEmojis.ToPermissionString()}**";
|
2021-09-13 08:21:03 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
eb.Description($"Missing permissions:\n{missing}");
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply(embed: eb.Build());
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task MessageProxyCheck(Context ctx)
|
|
|
|
{
|
|
|
|
if (!ctx.HasNext() && ctx.Message.MessageReference == null)
|
|
|
|
throw new PKSyntaxError("You need to specify a message.");
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var failedToGetMessage =
|
|
|
|
"Could not find a valid message to check, was not able to fetch the message, or the message was not sent by you.";
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var (messageId, channelId) = ctx.MatchMessage(false);
|
|
|
|
if (messageId == null || channelId == null)
|
|
|
|
throw new PKError(failedToGetMessage);
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var proxiedMsg = await ctx.Database.Execute(conn => ctx.Repository.GetMessage(conn, messageId.Value));
|
2021-11-27 02:10:56 +00:00
|
|
|
if (proxiedMsg != null)
|
|
|
|
{
|
|
|
|
await ctx.Reply($"{Emojis.Success} This message was proxied successfully.");
|
|
|
|
return;
|
|
|
|
}
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// get the message info
|
|
|
|
var msg = await _rest.GetMessageOrNull(channelId.Value, messageId.Value);
|
|
|
|
if (msg == null)
|
|
|
|
throw new PKError(failedToGetMessage);
|
|
|
|
|
|
|
|
// if user is fetching a message in a different channel sent by someone else, throw a generic error message
|
|
|
|
if (msg == null || msg.Author.Id != ctx.Author.Id && msg.ChannelId != ctx.Channel.Id)
|
|
|
|
throw new PKError(failedToGetMessage);
|
|
|
|
|
|
|
|
if ((_botConfig.Prefixes ?? BotConfig.DefaultPrefixes).Any(p => msg.Content.StartsWith(p)))
|
2022-01-21 05:00:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply("This message starts with the bot's prefix, and was parsed as a command.");
|
2022-01-21 05:00:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
if (msg.Author.Bot)
|
|
|
|
throw new PKError("You cannot check messages sent by a bot.");
|
|
|
|
if (msg.WebhookId != null)
|
|
|
|
throw new PKError("You cannot check messages sent by a webhook.");
|
|
|
|
if (msg.Author.Id != ctx.Author.Id && !ctx.CheckBotAdmin())
|
|
|
|
throw new PKError("You can only check your own messages.");
|
|
|
|
|
|
|
|
// get the channel info
|
2022-03-10 01:06:53 +00:00
|
|
|
var channel = await _rest.GetChannelOrNull(channelId.Value);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (channel == null)
|
|
|
|
throw new PKError("Unable to get the channel associated with this message.");
|
|
|
|
|
|
|
|
// using channel.GuildId here since _rest.GetMessage() doesn't return the GuildId
|
2022-01-22 08:05:01 +00:00
|
|
|
var context = await ctx.Repository.GetMessageContext(msg.Author.Id, channel.GuildId.Value, msg.ChannelId);
|
|
|
|
var members = (await ctx.Repository.GetProxyMembers(msg.Author.Id, channel.GuildId.Value)).ToList();
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2022-03-22 03:43:33 +00:00
|
|
|
// for now this is just server
|
|
|
|
var autoproxySettings = await ctx.Repository.GetAutoproxySettings(ctx.System.Id, channel.GuildId.Value, null);
|
|
|
|
|
|
|
|
// todo: match unlatch
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Run everything through the checks, catch the ProxyCheckFailedException, and reply with the error message.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_proxy.ShouldProxy(channel, msg, context);
|
2022-06-13 18:52:07 +00:00
|
|
|
_matcher.TryMatch(context, autoproxySettings, members, out var match, msg.Content, msg.Attachments.Length > 0, true);
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
await ctx.Reply("I'm not sure why this message was not proxied, sorry.");
|
|
|
|
}
|
|
|
|
catch (ProxyService.ProxyChecksFailedException e)
|
|
|
|
{
|
|
|
|
await ctx.Reply($"{e.Message}");
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|