From 05cd1ff85bd4e13ea2aad276eebc09295c9621df Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sun, 14 Jan 2018 19:39:26 -0500 Subject: [PATCH] Don't attempt to resolve permissions for invalid roles This resolves #824. Discord seems to have inconsistencies where a role can be deleted, but there will still be a few users who still have it in their `role_ids`. I was able to find this bug appearing in 11 members of a 10,000 member guild, so it would make sense that this is relatively rare, and it's why we hadn't noticed it previously. Since our permission resolution code is implementation agnostic, it operates on the user's RoleIds collection, which is what Discord sends us directly, and is not vaidated against the member's guild. In our permission resolution code, we make the assumption that Discord will always be telling us the truth with regard to a member's `role_ids`. This PR changes the behavior of permissions resolution to instead verify that the guild was able to return a role before attempting to resolve its permissions. --- src/Discord.Net.Core/Utils/Permissions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Core/Utils/Permissions.cs b/src/Discord.Net.Core/Utils/Permissions.cs index 367926dd1..7b92c9d3e 100644 --- a/src/Discord.Net.Core/Utils/Permissions.cs +++ b/src/Discord.Net.Core/Utils/Permissions.cs @@ -133,9 +133,10 @@ namespace Discord ulong deniedPermissions = 0UL, allowedPermissions = 0UL; foreach (var roleId in user.RoleIds) { - if (roleId != guild.EveryoneRole.Id) + IRole role = null; + if (roleId != guild.EveryoneRole.Id && (role = guild.GetRole(roleId)) != null) { - perms = channel.GetPermissionOverwrite(guild.GetRole(roleId)); + perms = channel.GetPermissionOverwrite(role); if (perms != null) { allowedPermissions |= perms.Value.AllowValue;