fix(bot): make EmbedLinks permission check work with threads

This commit is contained in:
Iris System 2023-05-28 14:42:18 +12:00
parent 71c61e93b5
commit 97666a62df
3 changed files with 9 additions and 7 deletions

View File

@ -39,7 +39,8 @@ public static class PermissionExtensions
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)
GuildMemberPartial? member, bool isWebhook = false,
bool isThread = false)
{
if (!(await cache.TryGetChannel(channelId) is Channel channel))
// todo: handle channel not found better
@ -55,7 +56,7 @@ public static class PermissionExtensions
if (isWebhook)
return EveryonePermissions(guild);
return PermissionsFor(guild, rootChannel, userId, member);
return PermissionsFor(guild, rootChannel, userId, member, isThread: isThread);
}
public static PermissionSet EveryonePermissions(this Guild guild) =>
@ -78,11 +79,11 @@ public static class PermissionExtensions
return perms;
}
public static PermissionSet PermissionsFor(Guild guild, Channel channel, MessageCreateEvent msg) =>
PermissionsFor(guild, channel, msg.Author.Id, msg.Member);
public static PermissionSet PermissionsFor(Guild guild, Channel channel, MessageCreateEvent msg, bool isThread = false) =>
PermissionsFor(guild, channel, msg.Author.Id, msg.Member, isThread: isThread);
public static PermissionSet PermissionsFor(Guild guild, Channel channel, ulong userId,
GuildMemberPartial? member)
GuildMemberPartial? member, bool isThread = false)
{
if (channel.Type == Channel.ChannelType.Dm)
return PermissionSet.Dm;
@ -100,7 +101,7 @@ public static class PermissionExtensions
if ((perms & PermissionSet.ViewChannel) == 0)
perms &= ~NeedsViewChannel;
if ((perms & PermissionSet.SendMessages) == 0)
if ((perms & PermissionSet.SendMessages) == 0 && (!isThread || (perms & PermissionSet.SendMessagesInThreads) == 0))
perms &= ~NeedsSendMessages;
return perms;

View File

@ -34,6 +34,7 @@ public enum PermissionSet: ulong
ManageRoles = 0x10000000,
ManageWebhooks = 0x20000000,
ManageEmojis = 0x40000000,
SendMessagesInThreads = 0x04000000000,
// Special:
None = 0,

View File

@ -100,7 +100,7 @@ public class ProxyService
// Check if the sender account can mention everyone/here + embed links
// we need to "mirror" these permissions when proxying to prevent exploits
var senderPermissions = PermissionExtensions.PermissionsFor(guild, rootChannel, message);
var senderPermissions = PermissionExtensions.PermissionsFor(guild, rootChannel, message, isThread: rootChannel.Id != channel.Id);
var allowEveryone = senderPermissions.HasFlag(PermissionSet.MentionEveryone);
var allowEmbeds = senderPermissions.HasFlag(PermissionSet.EmbedLinks);