Added ;rpc ;upc and ;cpc - they copy permission from one thing to another (user/role/channel) closes #258

For example
;cpc #general ~ #new-channel

Copies all permissions set for general to new-channel
This commit is contained in:
Master Kwoth 2016-05-04 07:29:58 +02:00
parent 562e561dba
commit 5fbe36e5da
2 changed files with 151 additions and 0 deletions

View File

@ -192,6 +192,60 @@ namespace NadekoBot.Modules.Permissions.Classes
Task.Run(() => WriteServerToJson(serverPerms)); Task.Run(() => WriteServerToJson(serverPerms));
} }
internal static void CopyRolePermissions(Role fromRole, Role toRole)
{
var server = fromRole.Server;
var serverPerms = PermissionsDict.GetOrAdd(server.Id,
new ServerPermissions(server.Id, server.Name));
var from = GetRolePermissionsById(server, fromRole.Id);
if (from == null)
serverPerms.RolePermissions.Add(fromRole.Id, from = new Permissions(fromRole.Name));
var to = GetRolePermissionsById(server, toRole.Id);
if (to == null)
serverPerms.RolePermissions.Add(toRole.Id, to = new Permissions(toRole.Name));
to.CopyFrom(from);
Task.Run(() => WriteServerToJson(serverPerms));
}
internal static void CopyChannelPermissions(Channel fromChannel, Channel toChannel)
{
var server = fromChannel.Server;
var serverPerms = PermissionsDict.GetOrAdd(server.Id,
new ServerPermissions(server.Id, server.Name));
var from = GetChannelPermissionsById(server, fromChannel.Id);
if (from == null)
serverPerms.ChannelPermissions.Add(fromChannel.Id, from = new Permissions(fromChannel.Name));
var to = GetChannelPermissionsById(server, toChannel.Id);
if (to == null)
serverPerms.ChannelPermissions.Add(toChannel.Id, to = new Permissions(toChannel.Name));
to.CopyFrom(from);
Task.Run(() => WriteServerToJson(serverPerms));
}
internal static void CopyUserPermissions(User fromUser, User toUser)
{
var server = fromUser.Server;
var serverPerms = PermissionsDict.GetOrAdd(server.Id,
new ServerPermissions(server.Id, server.Name));
var from = GetUserPermissionsById(server, fromUser.Id);
if (from == null)
serverPerms.UserPermissions.Add(fromUser.Id, from = new Permissions(fromUser.Name));
var to = GetUserPermissionsById(server, toUser.Id);
if (to == null)
serverPerms.UserPermissions.Add(toUser.Id, to = new Permissions(toUser.Name));
to.CopyFrom(from);
Task.Run(() => WriteServerToJson(serverPerms));
}
public static void SetServerModulePermission(Server server, string moduleName, bool value) public static void SetServerModulePermission(Server server, string moduleName, bool value)
{ {
var serverPerms = PermissionsDict.GetOrAdd(server.Id, var serverPerms = PermissionsDict.GetOrAdd(server.Id,
@ -424,6 +478,18 @@ namespace NadekoBot.Modules.Permissions.Classes
FilterWords = false; FilterWords = false;
} }
public void CopyFrom(Permissions other)
{
Modules.Clear();
foreach (var mp in other.Modules)
Modules.AddOrUpdate(mp.Key, mp.Value, (s, b) => mp.Value);
Commands.Clear();
foreach (var cp in other.Commands)
Commands.AddOrUpdate(cp.Key, cp.Value, (s, b) => cp.Value);
FilterInvites = other.FilterInvites;
FilterWords = other.FilterWords;
}
public override string ToString() public override string ToString()
{ {
var toReturn = ""; var toReturn = "";

View File

@ -58,6 +58,91 @@ namespace NadekoBot.Modules.Permissions
await e.Channel.SendMessage($"Role `{role.Name}` is now required in order to change permissions.").ConfigureAwait(false); await e.Channel.SendMessage($"Role `{role.Name}` is now required in order to change permissions.").ConfigureAwait(false);
}); });
cgb.CreateCommand(Prefix + "rpc")
.Alias(Prefix + "rolepermissionscopy")
.Description($"Copies BOT PERMISSIONS (not discord permissions) from one role to another.\n**Usage**:`{Prefix}rpc Some Role ~ Some other role`")
.Parameter("from_to", ParameterType.Unparsed)
.Do(async e =>
{
var arg = e.GetArg("from_to")?.Trim();
if (string.IsNullOrWhiteSpace(arg) || !arg.Contains('~'))
return;
var args = arg.Split('~').Select(a => a.Trim()).ToArray();
if (args.Length > 2)
{
await e.Channel.SendMessage("💢Invalid number of '~'s in the argument.");
return;
}
try
{
var fromRole = PermissionHelper.ValidateRole(e.Server, args[0]);
var toRole = PermissionHelper.ValidateRole(e.Server, args[1]);
PermissionsHandler.CopyRolePermissions(fromRole, toRole);
await e.Channel.SendMessage($"Copied permission settings from **{fromRole.Name}** to **{toRole.Name}**.");
}
catch (Exception ex)
{
await e.Channel.SendMessage($"💢{ex.Message}");
}
});
cgb.CreateCommand(Prefix + "cpc")
.Alias(Prefix + "channelpermissionscopy")
.Description($"Copies BOT PERMISSIONS (not discord permissions) from one channel to another.\n**Usage**:`{Prefix}cpc Some Channel ~ Some other channel`")
.Parameter("from_to", ParameterType.Unparsed)
.Do(async e =>
{
var arg = e.GetArg("from_to")?.Trim();
if (string.IsNullOrWhiteSpace(arg) || !arg.Contains('~'))
return;
var args = arg.Split('~').Select(a => a.Trim()).ToArray();
if (args.Length > 2)
{
await e.Channel.SendMessage("💢Invalid number of '~'s in the argument.");
return;
}
try
{
var fromChannel = PermissionHelper.ValidateChannel(e.Server, args[0]);
var toChannel = PermissionHelper.ValidateChannel(e.Server, args[1]);
PermissionsHandler.CopyChannelPermissions(fromChannel, toChannel);
await e.Channel.SendMessage($"Copied permission settings from **{fromChannel.Name}** to **{toChannel.Name}**.");
}
catch (Exception ex)
{
await e.Channel.SendMessage($"💢{ex.Message}");
}
});
cgb.CreateCommand(Prefix + "upc")
.Alias(Prefix + "userpermissionscopy")
.Description($"Copies BOT PERMISSIONS (not discord permissions) from one role to another.\n**Usage**:`{Prefix}upc @SomeUser ~ @SomeOtherUser`")
.Parameter("from_to", ParameterType.Unparsed)
.Do(async e =>
{
var arg = e.GetArg("from_to")?.Trim();
if (string.IsNullOrWhiteSpace(arg) || !arg.Contains('~'))
return;
var args = arg.Split('~').Select(a => a.Trim()).ToArray();
if (args.Length > 2)
{
await e.Channel.SendMessage("💢Invalid number of '~'s in the argument.");
return;
}
try
{
var fromUser = PermissionHelper.ValidateUser(e.Server, args[0]);
var toUser = PermissionHelper.ValidateUser(e.Server, args[1]);
PermissionsHandler.CopyUserPermissions(fromUser, toUser);
await e.Channel.SendMessage($"Copied permission settings from **{fromUser.ToString()}**to * *{toUser.ToString()}**.");
}
catch (Exception ex)
{
await e.Channel.SendMessage($"💢{ex.Message}");
}
});
cgb.CreateCommand(Prefix + "verbose") cgb.CreateCommand(Prefix + "verbose")
.Alias(Prefix + "v") .Alias(Prefix + "v")
.Description("Sets whether to show when a command/module is blocked.\n**Usage**: ;verbose true") .Description("Sets whether to show when a command/module is blocked.\n**Usage**: ;verbose true")