PluralKit/Myriad/Extensions/PermissionExtensions.cs

176 lines
6.7 KiB
C#
Raw Normal View History

2021-08-27 15:03:47 +00:00
using System;
2020-12-22 12:15:26 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2020-12-22 12:15:26 +00:00
using Myriad.Cache;
2020-12-22 12:15:26 +00:00
using Myriad.Gateway;
using Myriad.Types;
namespace Myriad.Extensions
{
public static class PermissionExtensions
{
public static Task<PermissionSet> PermissionsFor(this IDiscordCache cache, MessageCreateEvent message) =>
2021-08-02 20:18:39 +00:00
PermissionsFor(cache, message.ChannelId, message.Author.Id, message.Member, isWebhook: message.WebhookId != null);
public static Task<PermissionSet> PermissionsFor(this IDiscordCache cache, ulong channelId, GuildMember member) =>
2021-08-02 20:18:39 +00:00
PermissionsFor(cache, channelId, member.User.Id, member);
public static async Task<PermissionSet> PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId, GuildMemberPartial? member, bool isWebhook = false)
{
if (!await cache.TryGetChannel(channelId, out var channel))
// todo: handle channel not found better
return PermissionSet.Dm;
2021-08-27 15:03:47 +00:00
if (channel.GuildId == null)
return PermissionSet.Dm;
var rootChannel = await cache.GetRootChannel(channelId);
2021-08-27 15:03:47 +00:00
var guild = await cache.GetGuild(channel.GuildId.Value);
if (isWebhook)
return EveryonePermissions(guild);
2021-08-02 20:18:39 +00:00
return PermissionsFor(guild, rootChannel, userId, member);
}
2021-08-27 15:03:47 +00:00
2020-12-22 12:15:26 +00:00
public static PermissionSet EveryonePermissions(this Guild guild) =>
guild.Roles.FirstOrDefault(r => r.Id == guild.Id)?.Permissions ?? PermissionSet.Dm;
2021-08-27 15:03:47 +00:00
public static async Task<PermissionSet> EveryonePermissions(this IDiscordCache cache, Channel channel)
{
if (channel.Type == Channel.ChannelType.Dm)
return PermissionSet.Dm;
var defaultPermissions = (await cache.GetGuild(channel.GuildId!.Value)).EveryonePermissions();
var overwrite = channel.PermissionOverwrites?.FirstOrDefault(r => r.Id == channel.GuildId);
if (overwrite == null)
return defaultPermissions;
var perms = defaultPermissions;
perms &= ~overwrite.Deny;
perms |= overwrite.Allow;
return perms;
}
2020-12-22 12:15:26 +00:00
public static PermissionSet PermissionsFor(Guild guild, Channel channel, MessageCreateEvent msg) =>
2021-08-02 20:18:39 +00:00
PermissionsFor(guild, channel, msg.Author.Id, msg.Member);
2020-12-22 12:15:26 +00:00
2021-08-02 20:18:39 +00:00
public static PermissionSet PermissionsFor(Guild guild, Channel channel, ulong userId, GuildMemberPartial? member)
2020-12-22 12:15:26 +00:00
{
if (channel.Type == Channel.ChannelType.Dm)
return PermissionSet.Dm;
2021-08-27 15:03:47 +00:00
2021-08-02 20:18:39 +00:00
if (member == null)
// this happens with system (Discord platform-owned) users - they're not actually in the guild, so there is no member object.
return EveryonePermissions(guild);
2020-12-22 12:15:26 +00:00
2021-08-02 20:18:39 +00:00
var perms = GuildPermissions(guild, userId, member.Roles);
perms = ApplyChannelOverwrites(perms, channel, userId, member.Roles);
2020-12-22 12:15:26 +00:00
if ((perms & PermissionSet.Administrator) == PermissionSet.Administrator)
return PermissionSet.All;
if ((perms & PermissionSet.ViewChannel) == 0)
perms &= ~NeedsViewChannel;
if ((perms & PermissionSet.SendMessages) == 0)
perms &= ~NeedsSendMessages;
return perms;
}
public static PermissionSet GuildPermissions(this Guild guild, ulong userId, ICollection<ulong> roleIds)
{
if (guild.OwnerId == userId)
return PermissionSet.All;
var perms = PermissionSet.None;
foreach (var role in guild.Roles)
{
if (role.Id == guild.Id || roleIds.Contains(role.Id))
perms |= role.Permissions;
}
if (perms.HasFlag(PermissionSet.Administrator))
2020-12-22 12:15:26 +00:00
return PermissionSet.All;
return perms;
}
public static PermissionSet ApplyChannelOverwrites(PermissionSet perms, Channel channel, ulong userId,
ICollection<ulong> roleIds)
{
if (channel.PermissionOverwrites == null)
return perms;
var everyoneDeny = PermissionSet.None;
var everyoneAllow = PermissionSet.None;
var roleDeny = PermissionSet.None;
var roleAllow = PermissionSet.None;
var userDeny = PermissionSet.None;
var userAllow = PermissionSet.None;
foreach (var overwrite in channel.PermissionOverwrites)
{
switch (overwrite.Type)
{
case Channel.OverwriteType.Role when overwrite.Id == channel.GuildId:
everyoneDeny |= overwrite.Deny;
everyoneAllow |= overwrite.Allow;
break;
case Channel.OverwriteType.Role when roleIds.Contains(overwrite.Id):
roleDeny |= overwrite.Deny;
roleAllow |= overwrite.Allow;
break;
case Channel.OverwriteType.Member when overwrite.Id == userId:
userDeny |= overwrite.Deny;
userAllow |= overwrite.Allow;
break;
}
}
perms &= ~everyoneDeny;
perms |= everyoneAllow;
perms &= ~roleDeny;
perms |= roleAllow;
perms &= ~userDeny;
perms |= userAllow;
return perms;
}
private const PermissionSet NeedsViewChannel =
PermissionSet.SendMessages |
PermissionSet.SendTtsMessages |
PermissionSet.ManageMessages |
PermissionSet.EmbedLinks |
PermissionSet.AttachFiles |
PermissionSet.ReadMessageHistory |
PermissionSet.MentionEveryone |
PermissionSet.UseExternalEmojis |
PermissionSet.AddReactions |
PermissionSet.Connect |
PermissionSet.Speak |
PermissionSet.MuteMembers |
PermissionSet.DeafenMembers |
PermissionSet.MoveMembers |
PermissionSet.UseVad |
PermissionSet.Stream |
PermissionSet.PrioritySpeaker;
private const PermissionSet NeedsSendMessages =
PermissionSet.MentionEveryone |
PermissionSet.SendTtsMessages |
PermissionSet.AttachFiles |
PermissionSet.EmbedLinks;
2021-01-15 10:29:43 +00:00
public static string ToPermissionString(this PermissionSet perms)
{
// TODO: clean string
return perms.ToString();
}
2020-12-22 12:15:26 +00:00
}
}