Skip to content

Commit

Permalink
On Role delete, check each permission roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltstro committed Nov 2, 2019
1 parent ce525ab commit 016fbbc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
16 changes: 16 additions & 0 deletions Pootis-Bot/Events/RoleEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,6 +58,21 @@ await dm.SendMessageAsync(
ServerListsManager.SaveServerList();
return;
}

//Check all permission roles
List<ServerList.CommandInfo> permsToCheck = server.CommandInfos;
foreach (ServerList.CommandInfo command in permsToCheck)
{
List<ulong> 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)
Expand Down
32 changes: 17 additions & 15 deletions Pootis-Bot/Services/PermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ServerList.CommandInfo> 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;
Expand Down Expand Up @@ -231,16 +235,14 @@ private static string RemovePermMessage(IReadOnlyList<string> 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}`.";
}
}
}

0 comments on commit 016fbbc

Please sign in to comment.