Added support for custom errors from permission checkers

This commit is contained in:
RogueException
2015-11-10 15:37:32 -04:00
parent d9759abf4c
commit f06a1d7278
8 changed files with 52 additions and 27 deletions

View File

@@ -95,13 +95,14 @@ namespace Discord.Commands
_checks = checks; _checks = checks;
} }
internal bool CanRun(User user, Channel channel) internal bool CanRun(User user, Channel channel, out string error)
{ {
for (int i = 0; i < _checks.Length; i++) for (int i = 0; i < _checks.Length; i++)
{ {
if (!_checks[i].CanRun(this, user, channel)) if (!_checks[i].CanRun(this, user, channel, out error))
return false; return false;
} }
error = null;
return true; return true;
} }

View File

@@ -106,25 +106,26 @@ namespace Discord.Commands
_commands.Add(command); _commands.Add(command);
} }
public bool CanRun(User user, Channel channel) public bool CanRun(User user, Channel channel, out string error)
{ {
if (_commands.Count > 0) if (_commands.Count > 0)
{ {
foreach (var cmd in _commands) foreach (var cmd in _commands)
{ {
if (cmd.CanRun(user, channel)) if (!cmd.CanRun(user, channel, out error))
return true; return false;
} }
} }
if (_items.Count > 0) if (_items.Count > 0)
{ {
foreach (var item in _items) foreach (var item in _items)
{ {
if (item.Value.CanRun(user, channel)) if (!item.Value.CanRun(user, channel, out error))
return true; return false;
} }
} }
return false; error = null;
return true;
} }
} }
} }

View File

@@ -9,7 +9,9 @@ namespace Discord.Commands
/// <summary> A Discord.Net client with extensions for handling common bot operations like text commands. </summary> /// <summary> A Discord.Net client with extensions for handling common bot operations like text commands. </summary>
public partial class CommandService : IService public partial class CommandService : IService
{ {
private readonly CommandServiceConfig _config; private const string DefaultPermissionError = "You do not have permission to access this command.";
private readonly CommandServiceConfig _config;
private readonly CommandGroupBuilder _root; private readonly CommandGroupBuilder _root;
private DiscordClient _client; private DiscordClient _client;
@@ -119,9 +121,10 @@ After:
var eventArgs = new CommandEventArgs(e.Message, command, args); var eventArgs = new CommandEventArgs(e.Message, command, args);
// Check permissions // Check permissions
if (!command.CanRun(eventArgs.User, eventArgs.Channel)) string errorText;
if (!command.CanRun(eventArgs.User, eventArgs.Channel, out errorText))
{ {
RaiseCommandError(CommandErrorType.BadPermissions, eventArgs); RaiseCommandError(CommandErrorType.BadPermissions, eventArgs, new Exception(errorText ?? DefaultPermissionError));
return; return;
} }
@@ -162,7 +165,8 @@ After:
bool isFirstItem = true; bool isFirstItem = true;
foreach (var group in category.Value.SubGroups) foreach (var group in category.Value.SubGroups)
{ {
if (!group.IsHidden && group.CanRun(user, channel)) string error;
if (!group.IsHidden && group.CanRun(user, channel, out error))
{ {
if (isFirstItem) if (isFirstItem)
{ {
@@ -221,15 +225,21 @@ After:
IEnumerable<Command> cmds = map.Commands; IEnumerable<Command> cmds = map.Commands;
bool isFirstCmd = true; bool isFirstCmd = true;
string error;
if (cmds != null) if (cmds != null)
{ {
foreach (var cmd in cmds) foreach (var cmd in cmds)
{ {
if (isFirstCmd) if (!cmd.CanRun(user, channel, out error)) { }
isFirstCmd = false; //output.AppendLine(error ?? DefaultPermissionError);
/*else else
output.AppendLine();*/ {
ShowCommandHelpInternal(cmd, user, channel, output); if (isFirstCmd)
isFirstCmd = false;
else
output.AppendLine();
ShowCommandHelpInternal(cmd, user, channel, output);
}
} }
} }
else else
@@ -240,7 +250,7 @@ After:
} }
bool isFirstSubCmd = true; bool isFirstSubCmd = true;
foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel) && !x.IsHidden)) foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel, out error) && !x.IsHidden))
{ {
if (isFirstSubCmd) if (isFirstSubCmd)
{ {
@@ -259,7 +269,7 @@ After:
if (isFirstCmd && isFirstSubCmd) //Had no commands and no subcommands if (isFirstCmd && isFirstSubCmd) //Had no commands and no subcommands
{ {
output.Clear(); output.Clear();
output.AppendLine("You do not have permission to access this command."); output.AppendLine("There are no commands you have permission to run.");
} }
return _client.SendMessage(replyChannel ?? channel, output.ToString()); return _client.SendMessage(replyChannel ?? channel, output.ToString());
@@ -267,8 +277,9 @@ After:
public Task ShowCommandHelp(Command command, User user, Channel channel, Channel replyChannel = null) public Task ShowCommandHelp(Command command, User user, Channel channel, Channel replyChannel = null)
{ {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
if (!command.CanRun(user, channel)) string error;
output.AppendLine("You do not have permission to access this command."); if (!command.CanRun(user, channel, out error))
output.AppendLine(error ?? DefaultPermissionError);
else else
ShowCommandHelpInternal(command, user, channel, output); ShowCommandHelpInternal(command, user, channel, output);
return _client.SendMessage(replyChannel ?? channel, output.ToString()); return _client.SendMessage(replyChannel ?? channel, output.ToString());

View File

@@ -2,6 +2,6 @@
{ {
public interface IPermissionChecker public interface IPermissionChecker
{ {
bool CanRun(Command command, User user, Channel channel); bool CanRun(Command command, User user, Channel channel, out string error);
} }
} }

View File

@@ -14,8 +14,9 @@
_minPermissions = minPermissions; _minPermissions = minPermissions;
} }
public bool CanRun(Command command, User user, Channel channel) public bool CanRun(Command command, User user, Channel channel, out string error)
{ {
error = null; //Use default error text.
int permissions = _service.GetPermissionLevel(user, channel); int permissions = _service.GetPermissionLevel(user, channel);
return permissions >= _minPermissions; return permissions >= _minPermissions;
} }

View File

@@ -9,8 +9,9 @@
_service = client.GetService<BlacklistService>(true); _service = client.GetService<BlacklistService>(true);
} }
public bool CanRun(Command command, User user, Channel channel) public bool CanRun(Command command, User user, Channel channel, out string error)
{ {
error = null; //Use default error text.
return _service.CanRun(user); return _service.CanRun(user);
} }
} }

View File

@@ -9,8 +9,9 @@
_service = client.GetService<WhitelistService>(true); _service = client.GetService<WhitelistService>(true);
} }
public bool CanRun(Command command, User user, Channel channel) public bool CanRun(Command command, User user, Channel channel, out string error)
{ {
error = null; //Use default error text.
return _service.CanRun(user); return _service.CanRun(user);
} }
} }

View File

@@ -14,9 +14,18 @@ namespace Discord.Modules
_filterType = manager.FilterType; _filterType = manager.FilterType;
} }
public bool CanRun(Command command, User user, Channel channel) public bool CanRun(Command command, User user, Channel channel, out string error)
{ {
return _filterType == FilterType.Unrestricted || _filterType == FilterType.AllowPrivate || _manager.HasChannel(channel); if (_filterType == FilterType.Unrestricted || _filterType == FilterType.AllowPrivate || _manager.HasChannel(channel))
{
error = null;
return true;
}
else
{
error = "This module is currently disabled.";
return false;
}
} }
} }
} }