Browse Source

Added custom error to AddCheck(lambda)

pull/22/merge
RogueException 9 years ago
parent
commit
f61b9febfb
2 changed files with 8 additions and 6 deletions
  1. +4
    -4
      src/Discord.Net.Commands/CommandBuilder.cs
  2. +4
    -2
      src/Discord.Net.Commands/Permissions/GenericPermissionChecker.cs

+ 4
- 4
src/Discord.Net.Commands/CommandBuilder.cs View File

@@ -75,9 +75,9 @@ namespace Discord.Commands
_checks.Add(check); _checks.Add(check);
return this; return this;
} }
public CommandBuilder AddCheck(Func<Command, User, Channel, bool> checkFunc)
public CommandBuilder AddCheck(Func<Command, User, Channel, bool> checkFunc, string errorMsg = null)
{ {
_checks.Add(new GenericPermissionChecker(checkFunc));
_checks.Add(new GenericPermissionChecker(checkFunc, errorMsg));
return this; return this;
} }


@@ -140,9 +140,9 @@ namespace Discord.Commands
{ {
_checks.Add(checker); _checks.Add(checker);
} }
public void AddCheck(Func<Command, User, Channel, bool> checkFunc)
public void AddCheck(Func<Command, User, Channel, bool> checkFunc, string errorMsg = null)
{ {
_checks.Add(new GenericPermissionChecker(checkFunc));
_checks.Add(new GenericPermissionChecker(checkFunc, errorMsg));
} }


public CommandGroupBuilder CreateGroup(string cmd, Action<CommandGroupBuilder> config = null) public CommandGroupBuilder CreateGroup(string cmd, Action<CommandGroupBuilder> config = null)


+ 4
- 2
src/Discord.Net.Commands/Permissions/GenericPermissionChecker.cs View File

@@ -5,15 +5,17 @@ namespace Discord.Commands.Permissions
internal class GenericPermissionChecker : IPermissionChecker internal class GenericPermissionChecker : IPermissionChecker
{ {
private readonly Func<Command, User, Channel, bool> _checkFunc; private readonly Func<Command, User, Channel, bool> _checkFunc;
private readonly string _error;


public GenericPermissionChecker(Func<Command, User, Channel, bool> checkFunc)
public GenericPermissionChecker(Func<Command, User, Channel, bool> checkFunc, string error = null)
{ {
_checkFunc = checkFunc; _checkFunc = checkFunc;
_error = error;
} }


public bool CanRun(Command command, User user, Channel channel, out string error) public bool CanRun(Command command, User user, Channel channel, out string error)
{ {
error = null; //Use default error text.
error = _error;
return _checkFunc(command, user, channel); return _checkFunc(command, user, channel);
} }
} }


Loading…
Cancel
Save