From 016fbbcb13bac713f83f6e75010f7e5cf6bd697a Mon Sep 17 00:00:00 2001 From: Voltstro Date: Sat, 2 Nov 2019 22:24:47 +1000 Subject: [PATCH] On Role delete, check each permission roles --- Pootis-Bot/Events/RoleEvents.cs | 16 ++++++++++++ Pootis-Bot/Services/PermissionService.cs | 32 +++++++++++++----------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Pootis-Bot/Events/RoleEvents.cs b/Pootis-Bot/Events/RoleEvents.cs index 71a3d1e1..a9f3d63f 100644 --- a/Pootis-Bot/Events/RoleEvents.cs +++ b/Pootis-Bot/Events/RoleEvents.cs @@ -6,6 +6,7 @@ using Pootis_Bot.Core; using Pootis_Bot.Core.Managers; using Pootis_Bot.Entities; +using Pootis_Bot.Services; using Pootis_Bot.Structs; namespace Pootis_Bot.Events @@ -57,6 +58,21 @@ await dm.SendMessageAsync( ServerListsManager.SaveServerList(); return; } + + //Check all permission roles + List permsToCheck = server.CommandInfos; + foreach (ServerList.CommandInfo command in permsToCheck) + { + List permRolesToRemove = command.Roles.Where(commandRole => role.Id == commandRole).ToList(); + + foreach (ulong permRoleToRemove in permRolesToRemove) + { + server.GetCommandInfo(command.Command).Roles.Remove(permRoleToRemove); + } + } + + PermissionService.CheckAllServerRoles(server); + ServerListsManager.SaveServerList(); } public async Task RoleUpdated(SocketRole before, SocketRole after) diff --git a/Pootis-Bot/Services/PermissionService.cs b/Pootis-Bot/Services/PermissionService.cs index 13e99902..bda36db3 100644 --- a/Pootis-Bot/Services/PermissionService.cs +++ b/Pootis-Bot/Services/PermissionService.cs @@ -144,10 +144,8 @@ public async Task RemovePerm(string command, string[] roles, IMessageChannel cha else { //Check all roles and make sure they are assigned to the command - foreach (IRole role in iRoles) + foreach (IRole role in iRoles.Where(role => server.GetCommandInfo(command).GetRole(role.Id) == 0)) { - if (server.GetCommandInfo(command).GetRole(role.Id) != 0) continue; - await channel.SendMessageAsync( $"The command `{command}` doesn't have the role **{role}** assigned to it!"); return; @@ -160,16 +158,22 @@ await channel.SendMessageAsync( } //There are no more roles assigned to the command so remove it entirely - if (server.GetCommandInfo(command).Roles.Count == 0) - { - server.CommandInfos.Remove(server.GetCommandInfo(command)); - } + CheckAllServerRoles(server); ServerListsManager.SaveServerList(); await channel.SendMessageAsync(RemovePermMessage(roles, command, server)); } } + public static void CheckAllServerRoles(ServerList server) + { + List cmdsToRemove = server.CommandInfos.Where(command => command.Roles.Count == 0).ToList(); + foreach (ServerList.CommandInfo command in cmdsToRemove) + { + server.CommandInfos.Remove(command); + } + } + private bool CanModifyPerm(string command) { bool canModifyPerm = true; @@ -231,16 +235,14 @@ private static string RemovePermMessage(IReadOnlyList roles, string comm { return $"**{roles[0]}** role will not be allowed to use the command `{command}`."; } - else //Multiple roles - { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < roles.Count; i++) - { - sb.Append(i == roles.Count - 1 ? roles[i] : $"{roles[i]}, "); - } - return $"**{sb}** roles will not be allowed to use the command `{command}`."; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < roles.Count; i++) + { + sb.Append(i == roles.Count - 1 ? roles[i] : $"{roles[i]}, "); } + + return $"**{sb}** roles will not be allowed to use the command `{command}`."; } } } \ No newline at end of file