Merge pull request #19 from cuken/master

Added PermissionHelper.cs, Permissons.cs
This commit is contained in:
Master Kwoth 2016-02-11 10:16:05 +01:00
commit 7274a5d80f
3 changed files with 271 additions and 0 deletions

View File

@ -0,0 +1,44 @@
using NadekoBot.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Classes
{
static class PermissionHelper
{
public static bool ValidateBool(string passedArg)
{
if (string.IsNullOrEmpty(passedArg.Trim()))
{
throw new System.ArgumentException("No value supplied! Missing argument");
}
switch (passedArg.ToLower())
{
case "t":
case "true":
case "enabled":
return true;
case "f":
case "false":
case "disabled":
return false;
default:
throw new System.ArgumentException("Did not receive a valid boolean value");
}
}
internal static void ValidateModule(string v)
{
return;
}
internal static void ValidateCommand(string v)
{
return;
}
}
}

View File

@ -0,0 +1,226 @@
using System;
using System.Threading.Tasks;
using Discord.Modules;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Discord.Commands;
using NadekoBot.Extensions;
using System.Collections.Generic;
using NadekoBot.Classes.PermissionCheckers;
using NadekoBot.Classes;
namespace NadekoBot.Modules
{
class PermissionModule : DiscordModule
{
string trigger = "*";
public PermissionModule() : base()
{
//Empty for now
}
public override void Install(ModuleManager manager)
{
var client = NadekoBot.client;
manager.CreateCommands("", cgb => {
commands.ForEach(cmd => cmd.Init(cgb));
cgb.CreateCommand(trigger + "ssm").Alias(trigger + "setservermodule")
.Parameter("module", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets a module's permission at the server level.")
// .AddCheck() -> fix this
.Do(async e =>
{
try
{
PermissionHelper.ValidateModule(e.GetArg("module"));
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
await e.Send("I'm setting " + e.GetArg("module") + " to " + state);
}
catch (ArgumentException exArg)
{
await e.Send(exArg.Message);
}
catch (Exception ex)
{
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(trigger + "ssc").Alias(trigger + "setservercommand")
.Parameter("command", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets a command's permission at the server level.")
// .AddCheck() -> fix this
.Do(async e =>
{
try
{
PermissionHelper.ValidateCommand(e.GetArg("command"));
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
await e.Send("I'm setting " + e.GetArg("command") + " to " + state);
}
catch (ArgumentException exArg)
{
await e.Send(exArg.Message);
}
catch (Exception ex)
{
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(trigger + "srm").Alias(trigger + "setrollmodule")
.Parameter("module", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets a module's permission at the roll level.")
// .AddCheck() -> fix this
.Do(async e =>
{
try
{
PermissionHelper.ValidateModule(e.GetArg("module"));
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
await e.Send("I'm setting " + e.GetArg("module") + " to " + state);
}
catch (ArgumentException exArg)
{
await e.Send(exArg.Message);
}
catch (Exception ex)
{
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(trigger + "src").Alias(trigger + "setrollcommand")
.Parameter("command", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets a command's permission at the roll level.")
// .AddCheck() -> fix this
.Do(async e =>
{
try
{
PermissionHelper.ValidateCommand(e.GetArg("command"));
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
await e.Send("I'm setting " + e.GetArg("command") + " to " + state);
}
catch (ArgumentException exArg)
{
await e.Send(exArg.Message);
}
catch (Exception ex)
{
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(trigger + "scm").Alias(trigger + "setchannelmodule")
.Parameter("module", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets a module's permission at the channel level.")
// .AddCheck() -> fix this
.Do(async e =>
{
try
{
PermissionHelper.ValidateModule(e.GetArg("module"));
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
await e.Send("I'm setting " + e.GetArg("module") + " to " + state);
}
catch (ArgumentException exArg)
{
await e.Send(exArg.Message);
}
catch (Exception ex)
{
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(trigger + "scc").Alias(trigger + "setchannelcommand")
.Parameter("command", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets a command's permission at the channel level.")
// .AddCheck() -> fix this
.Do(async e =>
{
try
{
PermissionHelper.ValidateCommand(e.GetArg("command"));
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
await e.Send("I'm setting " + e.GetArg("command") + " to " + state);
}
catch (ArgumentException exArg)
{
await e.Send(exArg.Message);
}
catch (Exception ex)
{
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(trigger + "sum").Alias(trigger + "setusermodule")
.Parameter("module", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets a module's permission at the user level.")
// .AddCheck() -> fix this
.Do(async e =>
{
try
{
PermissionHelper.ValidateModule(e.GetArg("module"));
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
await e.Send("I'm setting " + e.GetArg("module") + " to " + state);
}
catch (ArgumentException exArg)
{
await e.Send(exArg.Message);
}
catch (Exception ex)
{
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
cgb.CreateCommand(trigger + "suc").Alias(trigger + "setuserchannel")
.Parameter("command", ParameterType.Required)
.Parameter("bool", ParameterType.Required)
.Description("Sets a command's permission at the user level.")
// .AddCheck() -> fix this
.Do(async e =>
{
try
{
PermissionHelper.ValidateModule(e.GetArg("command"));
bool state = PermissionHelper.ValidateBool(e.GetArg("bool"));
await e.Send("I'm setting " + e.GetArg("command") + " to " + state);
}
catch (ArgumentException exArg)
{
await e.Send(exArg.Message);
}
catch (Exception ex)
{
await e.Send("Something went terribly wrong - " + ex.Message);
}
});
});
}
}
}

View File

@ -90,6 +90,7 @@ namespace NadekoBot {
//install modules //install modules
modules.Add(new Administration(), "Administration", ModuleFilter.None); modules.Add(new Administration(), "Administration", ModuleFilter.None);
modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None);
modules.Add(new Conversations(), "Conversations", ModuleFilter.None); modules.Add(new Conversations(), "Conversations", ModuleFilter.None);
modules.Add(new Gambling(), "Gambling", ModuleFilter.None); modules.Add(new Gambling(), "Gambling", ModuleFilter.None);
modules.Add(new Games(), "Games", ModuleFilter.None); modules.Add(new Games(), "Games", ModuleFilter.None);