permissions pretty much finished, bugfixes and 1 improvement left

This commit is contained in:
Master Kwoth 2016-02-13 02:11:30 +01:00
parent d589ef7449
commit cd2cfebfa3
5 changed files with 253 additions and 140 deletions

View File

@ -17,25 +17,60 @@ namespace NadekoBot.Classes.Permissions {
public bool CanRun(Command command, User user, Channel channel, out string error) {
error = null;
if (channel.IsPrivate)
return true;
try {
//is it a permission command?
if (command.Text == "Permissions")
// if it is, check if the user has the correct role
// if yes return true, if no return false
if (command.Category == "Permissions")
if (user.Server.IsOwner || user.HasRole(PermissionHelper.ValidateRole(user.Server, PermissionsHandler.GetServerPermissionsRoleName(user.Server))))
return true;
else
throw new Exception("You do not have necessary role to change permissions.");
throw new Exception($"You don't have the necessary role (**{PermissionsHandler._permissionsDict[user.Server].PermissionsControllerRole}**) to change permissions.");
var permissionType = PermissionsHandler.GetPermissionBanType(command, user, channel);
if (permissionType == PermissionsHandler.PermissionBanType.None)
return true;
string msg;
throw new InvalidOperationException($"Cannot run this command: {permissionType}");
switch (permissionType) {
case PermissionsHandler.PermissionBanType.None:
return true;
case PermissionsHandler.PermissionBanType.ServerBanCommand:
msg = $"**{command.Text}** command has been banned from use on this **server**.";
break;
case PermissionsHandler.PermissionBanType.ServerBanModule:
msg = $"**{command.Category}** module has been banned from use on this **server**.";
break;
case PermissionsHandler.PermissionBanType.ChannelBanCommand:
msg = $"**{command.Text}** command has been banned from use on this **channel**.";
break;
case PermissionsHandler.PermissionBanType.ChannelBanModule:
msg = $"**{command.Category}** module has been banned from use on this **channel**.";
break;
case PermissionsHandler.PermissionBanType.RoleBanCommand:
msg = $"You do not have a **role** which permits you the usage of **{command.Text}** command.";
break;
case PermissionsHandler.PermissionBanType.RoleBanModule:
msg = $"You do not have a **role** which permits you the usage of **{command.Category}** module.";
break;
case PermissionsHandler.PermissionBanType.UserBanCommand:
msg = $"{user.Mention}, You have been banned from using **{command.Text}** command.";
break;
case PermissionsHandler.PermissionBanType.UserBanModule:
msg = $"{user.Mention}, You have been banned from using **{command.Category}** module.";
break;
default:
return true;
}
if (PermissionsHandler._permissionsDict[user.Server].Verbose) //if verbose - print errors
Task.Run(() => channel.SendMessage(msg));
return false;
} catch (Exception ex) {
if (PermissionsHandler._permissionsDict[user.Server].Verbose) //if verbose - print errors
channel.SendMessage(ex.Message);
Task.Run(() => channel.SendMessage(ex.Message));
return false;
}
}

View File

@ -19,6 +19,7 @@ namespace NadekoBot.Classes {
case "t":
case "true":
case "enable":
case "enabled":
case "allow":
case "unban":
return true;
@ -26,6 +27,7 @@ namespace NadekoBot.Classes {
case "f":
case "false":
case "disable":
case "disabled":
case "disallow":
case "ban":
return false;

View File

@ -141,9 +141,18 @@ namespace NadekoBot.Modules {
});
cgb.CreateCommand(".roles")
.Description("List all roles on this server")
.Description("List all roles on this server or a single user if specified.")
.Parameter("user", ParameterType.Unparsed)
.Do(async e => {
await e.Send("`List of roles:` \n• " + string.Join("\n• ", e.Server.Roles).Replace("@everyone", "[everyone]"));
if (!string.IsNullOrWhiteSpace(e.GetArg("user"))) {
var usr = e.Server.FindUsers(e.GetArg("user")).FirstOrDefault();
if (usr != null) {
await e.Send($"`List of roles for **{usr.Name}**:` \n• " + string.Join("\n• ", usr.Roles).Replace("@everyone", "මeveryone"));
return;
}
}
await e.Send("`List of roles:` \n• " + string.Join("\n• ", e.Server.Roles).Replace("@everyone", "මeveryone"));
});
cgb.CreateCommand(".modules")
@ -395,22 +404,6 @@ namespace NadekoBot.Modules {
if (!e.User.ServerPermissions.ManageMessages) return;
await e.Send("This feature is being reconstructed.");
/*
if (pruneDict.ContainsKey(e.Server))
return;
int num;
if (!Int32.TryParse(e.GetArg("num"), out num) || num < 1) {
await e.Send("Incorrect amount.");
return;
}
if (num > 10)
num = 10;
var msgs = await e.Channel.DownloadMessages(num);
foreach (var m in msgs) {
await m.Delete();
await Task.Delay(500);
}
*/
});
cgb.CreateCommand(".die")
@ -435,23 +428,6 @@ namespace NadekoBot.Modules {
foreach (var m in msgs)
await m.Delete();
});
/*
try {
if (clearDictionary.ContainsKey(e.Server))
return;
clearDictionary.TryAdd(e.Server, true);
var msgs = await e.Channel.DownloadMessages(100);
await Task.Run(async () => {
var ms = msgs.Where(msg => msg.User.Id == client.CurrentUser.Id);
foreach (var m in ms) {
try { await m.Delete(); } catch (Exception) { }
await Task.Delay(500);
}
});
} catch (Exception) { }
bool throwaway;
clearDictionary.TryRemove(e.Server, out throwaway);
*/
});
cgb.CreateCommand(".newname")
.Alias(".setname")
@ -552,7 +528,7 @@ namespace NadekoBot.Modules {
cgb.CreateCommand(".menrole")
.Alias(".mentionrole")
.Description("Mentions every person from the provided role or roles (separated by a ',') on this server. Requires you to have mention @everyone permission.")
.Description("Mentions every person from the provided role or roles (separated by a ',') on this server. Requires you to have mention everyone permission.")
.Parameter("roles", ParameterType.Unparsed)
.Do(async e => {
if (!e.User.ServerPermissions.MentionEveryone) return;
@ -603,10 +579,8 @@ namespace NadekoBot.Modules {
var rows = Classes.DBHandler.Instance.GetAllRows<Donator>();
var donatorsOrdered = rows.OrderBy(d => d.Amount);
string str = $"`Total number of people who donated is {donatorsOrdered.Count()}`\n";
foreach (var don in donatorsOrdered) {
str += don.UserName;
}
await e.Channel.SendMessage(str);
await e.Channel.SendMessage(string.Join(", ", donatorsOrdered));
});
});
@ -633,47 +607,6 @@ namespace NadekoBot.Modules {
Console.WriteLine("---------------\nInner error:\n" + ex.InnerException);
}
});
/*
cgb.CreateCommand(".no")
.Description("desc")
.Parameter("arg", ParameterType.Required)
.Do(async e => {
var arg = e.GetArg("arg");
});
cgb.CreateCommand(".voicetext")
.Description("Enabled or disabled voice to text channel connection. Only people in a certain voice channel will see ")
cgb.CreateCommand(".jsontype")
.Do(async e => {
Newtonsoft.Json.Linq.JArray data = Newtonsoft.Json.Linq.JArray.Parse(File.ReadAllText("data.json"));
if (data == null || data.Count == 0) return;
var wer = data.Where(jt => jt["Description"].ToString().Length > 120);
var list = wer.Select(jt => {
var obj = new Parse.ParseObject("TypingArticles");
obj["text"] = jt["Description"].ToString();
return obj;
});
await Parse.ParseObject.SaveAllAsync(list);
await e.Send("saved to parse");
});
cgb.CreateCommand(".repeat")
.Do(async e => {
if (e.User.Id != NadekoBot.OwnerID) return;
string[] notifs = { "Admin use .bye .greet", "Unstable - fixing", "fixing ~ani, ~mang", "join NadekoLog server", "-h is help, .stats",};
int i = notifs.Length;
while (true) {
await e.Channel.SendMessage($".setgame {notifs[--i]}");
await Task.Delay(20000);
if (i == 0) i = notifs.Length;
}
});
*/
});
}

View File

@ -4,10 +4,11 @@ using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Classes;
using PermsHandler = NadekoBot.Classes.Permissions.PermissionsHandler;
using System.Linq;
namespace NadekoBot.Modules {
class PermissionModule : DiscordModule {
string prefix = "*";
string prefix = ";";
public PermissionModule() : base() {
//Empty for now
}
@ -21,55 +22,76 @@ namespace NadekoBot.Modules {
cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance);
cgb.CreateCommand(prefix + "serverperms")
.Alias(prefix + "sp")
.Description("Shows banned permissions for this server.")
.Do(async e => {
var perms = PermsHandler.GetServerPermissions(e.Server);
if (perms == null)
await e.Send("No permissions set.");
if (string.IsNullOrWhiteSpace(perms?.ToString()))
await e.Send("No permissions set for this server.");
await e.Send(perms.ToString());
});
cgb.CreateCommand(prefix + "roleperms")
.Alias(prefix + "rp")
.Description("Shows banned permissions for a certain role. No argument means for everyone.")
.Parameter("role", ParameterType.Unparsed)
.Do(async e => {
var arg = e.GetArg("role");
var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role"));
Discord.Role role = e.Server.EveryoneRole;
if (!string.IsNullOrWhiteSpace(arg))
try {
role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role"));
} catch (Exception ex) {
await e.Send("💢 Error: " + ex.Message);
return;
}
var perms = PermsHandler.GetRolePermissionsById(e.Server, role.Id);
if (perms == null)
await e.Send("No permissions set.");
if (string.IsNullOrWhiteSpace(perms?.ToString()))
await e.Send($"No permissions set for **{role.Name}** role.");
await e.Send(perms.ToString());
});
cgb.CreateCommand(prefix + "channelperms")
.Alias(prefix + "cp")
.Description("Shows banned permissions for a certain channel. No argument means for this channel.")
.Parameter("channel", ParameterType.Unparsed)
.Do(async e => {
var arg = e.GetArg("channel");
var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel"));
Discord.Channel channel = e.Channel;
if (!string.IsNullOrWhiteSpace(arg))
try {
channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel"));
} catch (Exception ex) {
await e.Send("💢 Error: " + ex.Message);
return;
}
var perms = PermsHandler.GetChannelPermissionsById(e.Server, channel.Id);
if (perms == null)
await e.Send("No permissions set.");
if (string.IsNullOrWhiteSpace(perms?.ToString()))
await e.Send($"No permissions set for **{channel.Name}** channel.");
await e.Send(perms.ToString());
});
cgb.CreateCommand(prefix + "userperms")
.Alias(prefix + "up")
.Description("Shows banned permissions for a certain user. No argument means for yourself.")
.Parameter("user", ParameterType.Unparsed)
.Do(async e => {
var arg = e.GetArg("user");
Discord.User user;
if (string.IsNullOrWhiteSpace(e.GetArg("user")))
user = e.User;
else
Discord.User user = e.User;
if (!string.IsNullOrWhiteSpace(e.GetArg("user")))
try {
user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user"));
} catch (Exception ex) {
await e.Send("💢 Error: " + ex.Message);
return;
}
var perms = PermsHandler.GetUserPermissionsById(e.Server, user.Id);
if (perms == null)
await e.Send("No permissions set.");
if (string.IsNullOrWhiteSpace(perms?.ToString()))
await e.Send($"No permissions set for user **{user.Name}**.");
await e.Send(perms.ToString());
});
@ -83,7 +105,7 @@ namespace NadekoBot.Modules {
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
PermsHandler.SetServerModulePermission(e.Server, module, state);
await e.Send("I'm setting " + e.GetArg("module") + " to " + state);
await e.Send($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** on this server.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
@ -101,7 +123,7 @@ namespace NadekoBot.Modules {
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
PermsHandler.SetServerCommandPermission(e.Server, command, state);
await e.Send("I'm setting " + e.GetArg("command") + " to " + state);
await e.Send($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** on this server.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
@ -121,7 +143,7 @@ namespace NadekoBot.Modules {
Discord.Role role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role"));
PermsHandler.SetRoleModulePermission(role, module, state);
await e.Send("I'm setting " + e.GetArg("module") + " to " + state);
await e.Send($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
@ -141,7 +163,7 @@ namespace NadekoBot.Modules {
Discord.Role role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role"));
PermsHandler.SetRoleCommandPermission(role, command, state);
await e.Send("I'm setting " + e.GetArg("command") + " to " + state);
await e.Send($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
@ -161,7 +183,7 @@ namespace NadekoBot.Modules {
Discord.Channel channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel"));
PermsHandler.SetChannelModulePermission(channel, module, state);
await e.Send("I'm setting " + e.GetArg("module") + " to " + state);
await e.Send($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
@ -181,7 +203,7 @@ namespace NadekoBot.Modules {
Discord.Channel channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel"));
PermsHandler.SetChannelCommandPermission(channel, command, state);
await e.Send("I'm setting " + e.GetArg("command") + " to " + state);
await e.Send($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
@ -201,7 +223,7 @@ namespace NadekoBot.Modules {
Discord.User user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user"));
PermsHandler.SetUserModulePermission(user, module, state);
await e.Send("I'm setting " + e.GetArg("module") + " to " + state);
await e.Send($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for user **{user.Name}**.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
@ -221,7 +243,127 @@ namespace NadekoBot.Modules {
Discord.User user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user"));
PermsHandler.SetUserCommandPermission(user, command, state);
await e.Send("I'm setting " + e.GetArg("command") + " to " + state);
await e.Send($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for user **{user.Name}**.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(prefix + "asm").Alias(prefix + "allservermodules")
.Parameter("bool", ParameterType.Required)
.Description("Sets permissions for all modules at the server level.")
.Do(async e => {
try {
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
foreach (var module in NadekoBot.client.Modules().Modules) {
PermsHandler.SetServerModulePermission(e.Server, module.Name, state);
}
await e.Send($"All modules have been **{(state ? "enabled" : "disabled")}** on this server.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(prefix + "asc").Alias(prefix + "allservercommands")
.Parameter("module", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets permissions for all commands from a module at the server level.")
.Do(async e => {
try {
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
string module = PermissionHelper.ValidateModule(e.GetArg("module"));
foreach (var command in NadekoBot.client.Commands().AllCommands.Where(c => c.Category == module)) {
PermsHandler.SetServerCommandPermission(e.Server, command.Text, state);
}
await e.Send($"All commands from {module} module have been **{(state ? "enabled" : "disabled")}** on this server.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(prefix + "acm").Alias(prefix + "allchannelmodules")
.Parameter("bool", ParameterType.Required)
.Parameter("channel", ParameterType.Unparsed)
.Description("Sets permissions for all modules at the server level.")
.Do(async e => {
try {
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
Discord.Channel channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel"));
foreach (var module in NadekoBot.client.Modules().Modules) {
PermsHandler.SetChannelModulePermission(channel, module.Name, state);
}
await e.Send($"All modules have been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(prefix + "acc").Alias(prefix + "allchannelcommands")
.Parameter("module", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Parameter("channel", ParameterType.Unparsed)
.Description("Sets permissions for all commands from a module at the server level.")
.Do(async e => {
try {
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
string module = PermissionHelper.ValidateModule(e.GetArg("module"));
Discord.Channel channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel"));
foreach (var command in NadekoBot.client.Commands().AllCommands.Where(c => c.Category == module)) {
PermsHandler.SetChannelCommandPermission(channel, command.Text, state);
}
await e.Send($"All commands have been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(prefix + "arm").Alias(prefix + "allrolemodules")
.Parameter("bool", ParameterType.Required)
.Parameter("role", ParameterType.Unparsed)
.Description("Sets permissions for all modules at the role level.")
.Do(async e => {
try {
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
Discord.Role role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role"));
foreach (var module in NadekoBot.client.Modules().Modules) {
PermsHandler.SetRoleModulePermission(role, module.Name, state);
}
await e.Send($"All modules have been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(prefix + "arc").Alias(prefix + "allrolecommands")
.Parameter("module", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Parameter("channel", ParameterType.Unparsed)
.Description("Sets permissions for all commands from a certain module at the role level.")
.Do(async e => {
try {
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
string module = PermissionHelper.ValidateModule(e.GetArg("module"));
Discord.Role role = PermissionHelper.ValidateRole(e.Server, e.GetArg("channel"));
foreach (var command in NadekoBot.client.Commands().AllCommands.Where(c => c.Category == module)) {
PermsHandler.SetRoleCommandPermission(role, command.Text, state);
}
await e.Send($"All commands have been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role.");
} catch (ArgumentException exArg) {
await e.Send(exArg.Message);
} catch (Exception ex) {

View File

@ -121,6 +121,7 @@ namespace NadekoBot {
if (request != null) {
if (string.IsNullOrWhiteSpace(request.Content))
e.Cancel = true;
request.Content = request.Content.Replace("@everyone", "@everyοne");
}
};
});