Skip to content

Commit 016fbbc

Browse files
committed
On Role delete, check each permission roles
1 parent ce525ab commit 016fbbc

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

Pootis-Bot/Events/RoleEvents.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Pootis_Bot.Core;
77
using Pootis_Bot.Core.Managers;
88
using Pootis_Bot.Entities;
9+
using Pootis_Bot.Services;
910
using Pootis_Bot.Structs;
1011

1112
namespace Pootis_Bot.Events
@@ -57,6 +58,21 @@ await dm.SendMessageAsync(
5758
ServerListsManager.SaveServerList();
5859
return;
5960
}
61+
62+
//Check all permission roles
63+
List<ServerList.CommandInfo> permsToCheck = server.CommandInfos;
64+
foreach (ServerList.CommandInfo command in permsToCheck)
65+
{
66+
List<ulong> permRolesToRemove = command.Roles.Where(commandRole => role.Id == commandRole).ToList();
67+
68+
foreach (ulong permRoleToRemove in permRolesToRemove)
69+
{
70+
server.GetCommandInfo(command.Command).Roles.Remove(permRoleToRemove);
71+
}
72+
}
73+
74+
PermissionService.CheckAllServerRoles(server);
75+
ServerListsManager.SaveServerList();
6076
}
6177

6278
public async Task RoleUpdated(SocketRole before, SocketRole after)

Pootis-Bot/Services/PermissionService.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,8 @@ public async Task RemovePerm(string command, string[] roles, IMessageChannel cha
144144
else
145145
{
146146
//Check all roles and make sure they are assigned to the command
147-
foreach (IRole role in iRoles)
147+
foreach (IRole role in iRoles.Where(role => server.GetCommandInfo(command).GetRole(role.Id) == 0))
148148
{
149-
if (server.GetCommandInfo(command).GetRole(role.Id) != 0) continue;
150-
151149
await channel.SendMessageAsync(
152150
$"The command `{command}` doesn't have the role **{role}** assigned to it!");
153151
return;
@@ -160,16 +158,22 @@ await channel.SendMessageAsync(
160158
}
161159

162160
//There are no more roles assigned to the command so remove it entirely
163-
if (server.GetCommandInfo(command).Roles.Count == 0)
164-
{
165-
server.CommandInfos.Remove(server.GetCommandInfo(command));
166-
}
161+
CheckAllServerRoles(server);
167162

168163
ServerListsManager.SaveServerList();
169164
await channel.SendMessageAsync(RemovePermMessage(roles, command, server));
170165
}
171166
}
172167

168+
public static void CheckAllServerRoles(ServerList server)
169+
{
170+
List<ServerList.CommandInfo> cmdsToRemove = server.CommandInfos.Where(command => command.Roles.Count == 0).ToList();
171+
foreach (ServerList.CommandInfo command in cmdsToRemove)
172+
{
173+
server.CommandInfos.Remove(command);
174+
}
175+
}
176+
173177
private bool CanModifyPerm(string command)
174178
{
175179
bool canModifyPerm = true;
@@ -231,16 +235,14 @@ private static string RemovePermMessage(IReadOnlyList<string> roles, string comm
231235
{
232236
return $"**{roles[0]}** role will not be allowed to use the command `{command}`.";
233237
}
234-
else //Multiple roles
235-
{
236-
StringBuilder sb = new StringBuilder();
237-
for (int i = 0; i < roles.Count; i++)
238-
{
239-
sb.Append(i == roles.Count - 1 ? roles[i] : $"{roles[i]}, ");
240-
}
241238

242-
return $"**{sb}** roles will not be allowed to use the command `{command}`.";
239+
StringBuilder sb = new StringBuilder();
240+
for (int i = 0; i < roles.Count; i++)
241+
{
242+
sb.Append(i == roles.Count - 1 ? roles[i] : $"{roles[i]}, ");
243243
}
244+
245+
return $"**{sb}** roles will not be allowed to use the command `{command}`.";
244246
}
245247
}
246248
}

0 commit comments

Comments
 (0)