feat: respect Discord permissions in pk;message and pk;edit

This commit is contained in:
spiral
2021-11-10 23:46:16 -05:00
parent 48d4009c69
commit 343fafe9f0
5 changed files with 64 additions and 24 deletions

View File

@@ -1,7 +1,9 @@
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using Myriad.Extensions;
using Myriad.Types;
using PluralKit.Core;
@@ -57,6 +59,28 @@ namespace PluralKit.Bot
return ctx;
}
public static async Task<bool> CheckPermissionsInGuildChannel(this Context ctx, Channel channel, PermissionSet neededPerms)
{
var guild = ctx.Cache.GetGuild(channel.GuildId.Value);
if (guild == null)
return false;
var guildMember = ctx.Member;
if (ctx.Guild?.Id != channel.GuildId)
{
guildMember = await ctx.Rest.GetGuildMember(channel.GuildId.Value, ctx.Author.Id);
if (guildMember == null)
return false;
}
var userPermissions = PermissionExtensions.PermissionsFor(guild, channel, ctx.Author.Id, guildMember);
if ((userPermissions & neededPerms) == 0)
return false;
return true;
}
public static bool CheckBotAdmin(this Context ctx)
{
var botConfig = ctx.Services.Resolve<BotConfig>();