| @@ -61,6 +61,9 @@ | |||||
| <Compile Include="..\Discord.Net.Commands\CommandServiceConfig.cs"> | <Compile Include="..\Discord.Net.Commands\CommandServiceConfig.cs"> | ||||
| <Link>CommandServiceConfig.cs</Link> | <Link>CommandServiceConfig.cs</Link> | ||||
| </Compile> | </Compile> | ||||
| <Compile Include="..\Discord.Net.Commands\Permissions\GenericPermissionChecker.cs"> | |||||
| <Link>Permissions\GenericPermissionChecker.cs</Link> | |||||
| </Compile> | |||||
| <Compile Include="..\Discord.Net.Commands\Permissions\IPermissionChecker.cs"> | <Compile Include="..\Discord.Net.Commands\Permissions\IPermissionChecker.cs"> | ||||
| <Link>Permissions\IPermissionChecker.cs</Link> | <Link>Permissions\IPermissionChecker.cs</Link> | ||||
| </Compile> | </Compile> | ||||
| @@ -94,6 +97,18 @@ | |||||
| <Compile Include="..\Discord.Net.Commands\Permissions\Userlist\WhitelistService.cs"> | <Compile Include="..\Discord.Net.Commands\Permissions\Userlist\WhitelistService.cs"> | ||||
| <Link>Permissions\Userlist\WhitelistService.cs</Link> | <Link>Permissions\Userlist\WhitelistService.cs</Link> | ||||
| </Compile> | </Compile> | ||||
| <Compile Include="..\Discord.Net.Commands\Permissions\Visibility\PrivateChecker.cs"> | |||||
| <Link>Permissions\Visibility\PrivateChecker.cs</Link> | |||||
| </Compile> | |||||
| <Compile Include="..\Discord.Net.Commands\Permissions\Visibility\PrivateExtensions.cs"> | |||||
| <Link>Permissions\Visibility\PrivateExtensions.cs</Link> | |||||
| </Compile> | |||||
| <Compile Include="..\Discord.Net.Commands\Permissions\Visibility\PublicChecker.cs"> | |||||
| <Link>Permissions\Visibility\PublicChecker.cs</Link> | |||||
| </Compile> | |||||
| <Compile Include="..\Discord.Net.Commands\Permissions\Visibility\PublicExtensions.cs"> | |||||
| <Link>Permissions\Visibility\PublicExtensions.cs</Link> | |||||
| </Compile> | |||||
| <Compile Include="Properties\AssemblyInfo.cs" /> | <Compile Include="Properties\AssemblyInfo.cs" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| @@ -74,6 +74,11 @@ namespace Discord.Commands | |||||
| _checks.Add(check); | _checks.Add(check); | ||||
| return this; | return this; | ||||
| } | } | ||||
| public CommandBuilder AddCheck(Func<Command, User, Channel, bool> checkFunc) | |||||
| { | |||||
| _checks.Add(new GenericPermissionChecker(checkFunc)); | |||||
| return this; | |||||
| } | |||||
| public void Do(Func<CommandEventArgs, Task> func) | public void Do(Func<CommandEventArgs, Task> func) | ||||
| { | { | ||||
| @@ -129,9 +134,13 @@ namespace Discord.Commands | |||||
| _category = category; | _category = category; | ||||
| return this; | return this; | ||||
| } | } | ||||
| public void AddCheck(IPermissionChecker check) | |||||
| public void AddCheck(IPermissionChecker checker) | |||||
| { | { | ||||
| _checks.Add(check); | |||||
| _checks.Add(checker); | |||||
| } | |||||
| public void AddCheck(Func<Command, User, Channel, bool> checkFunc) | |||||
| { | |||||
| _checks.Add(new GenericPermissionChecker(checkFunc)); | |||||
| } | } | ||||
| public CommandGroupBuilder CreateGroup(string cmd, Action<CommandGroupBuilder> config = null) | public CommandGroupBuilder CreateGroup(string cmd, Action<CommandGroupBuilder> config = null) | ||||
| @@ -0,0 +1,20 @@ | |||||
| using System; | |||||
| namespace Discord.Commands.Permissions | |||||
| { | |||||
| internal class GenericPermissionChecker : IPermissionChecker | |||||
| { | |||||
| private readonly Func<Command, User, Channel, bool> _checkFunc; | |||||
| public GenericPermissionChecker(Func<Command, User, Channel, bool> checkFunc) | |||||
| { | |||||
| _checkFunc = checkFunc; | |||||
| } | |||||
| public bool CanRun(Command command, User user, Channel channel, out string error) | |||||
| { | |||||
| error = null; //Use default error text. | |||||
| return _checkFunc(command, user, channel); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,21 @@ | |||||
| namespace Discord.Commands.Permissions.Visibility | |||||
| { | |||||
| public class PrivateChecker : IPermissionChecker | |||||
| { | |||||
| internal PrivateChecker() { } | |||||
| public bool CanRun(Command command, User user, Channel channel, out string error) | |||||
| { | |||||
| if (!user.IsPrivate) | |||||
| { | |||||
| error = "This command may only be run in a private chat."; | |||||
| return false; | |||||
| } | |||||
| else | |||||
| { | |||||
| error = null; | |||||
| return true; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,21 @@ | |||||
| namespace Discord.Commands.Permissions.Visibility | |||||
| { | |||||
| public static class PrivateExtensions | |||||
| { | |||||
| public static CommandBuilder PrivateOnly(this CommandBuilder builder) | |||||
| { | |||||
| builder.AddCheck(new PrivateChecker()); | |||||
| return builder; | |||||
| } | |||||
| public static CommandGroupBuilder PrivateOnly(this CommandGroupBuilder builder) | |||||
| { | |||||
| builder.AddCheck(new PrivateChecker()); | |||||
| return builder; | |||||
| } | |||||
| public static CommandService PrivateOnly(this CommandService service) | |||||
| { | |||||
| service.Root.AddCheck(new PrivateChecker()); | |||||
| return service; | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,21 @@ | |||||
| namespace Discord.Commands.Permissions.Visibility | |||||
| { | |||||
| public class PublicChecker : IPermissionChecker | |||||
| { | |||||
| internal PublicChecker() { } | |||||
| public bool CanRun(Command command, User user, Channel channel, out string error) | |||||
| { | |||||
| if (user.IsPrivate) | |||||
| { | |||||
| error = "This command can't be run in n a private chat."; | |||||
| return false; | |||||
| } | |||||
| else | |||||
| { | |||||
| error = null; | |||||
| return true; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,21 @@ | |||||
| namespace Discord.Commands.Permissions.Visibility | |||||
| { | |||||
| public static class PublicExtensions | |||||
| { | |||||
| public static CommandBuilder PublicOnly(this CommandBuilder builder) | |||||
| { | |||||
| builder.AddCheck(new PublicChecker()); | |||||
| return builder; | |||||
| } | |||||
| public static CommandGroupBuilder PublicOnly(this CommandGroupBuilder builder) | |||||
| { | |||||
| builder.AddCheck(new PublicChecker()); | |||||
| return builder; | |||||
| } | |||||
| public static CommandService PublicOnly(this CommandService service) | |||||
| { | |||||
| service.Root.AddCheck(new PublicChecker()); | |||||
| return service; | |||||
| } | |||||
| } | |||||
| } | |||||