Browse Source

Added support for custom errors from permission checkers

tags/docs-0.9
RogueException 9 years ago
parent
commit
f06a1d7278
8 changed files with 52 additions and 27 deletions
  1. +3
    -2
      src/Discord.Net.Commands/Command.cs
  2. +7
    -6
      src/Discord.Net.Commands/CommandMap.cs
  3. +24
    -13
      src/Discord.Net.Commands/CommandService.cs
  4. +1
    -1
      src/Discord.Net.Commands/Permissions/IPermissionChecker.cs
  5. +2
    -1
      src/Discord.Net.Commands/Permissions/Levels/PermissionLevelChecker.cs
  6. +2
    -1
      src/Discord.Net.Commands/Permissions/Userlist/BlacklistChecker.cs
  7. +2
    -1
      src/Discord.Net.Commands/Permissions/Userlist/WhitelistChecker.cs
  8. +11
    -2
      src/Discord.Net.Modules/ModuleChecker.cs

+ 3
- 2
src/Discord.Net.Commands/Command.cs 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;
} }




+ 7
- 6
src/Discord.Net.Commands/CommandMap.cs 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))
return true;
if (!cmd.CanRun(user, channel, out error))
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))
return true;
if (!item.Value.CanRun(user, channel, out error))
return false;
} }
} }
return false;
error = null;
return true;
} }
} }
} }

+ 24
- 13
src/Discord.Net.Commands/CommandService.cs 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)
isFirstCmd = false;
/*else
output.AppendLine();*/
ShowCommandHelpInternal(cmd, user, channel, output);
if (!cmd.CanRun(user, channel, out error)) { }
//output.AppendLine(error ?? DefaultPermissionError);
else
{
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))
output.AppendLine("You do not have permission to access this command.");
string error;
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());


+ 1
- 1
src/Discord.Net.Commands/Permissions/IPermissionChecker.cs 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);
} }
} }

+ 2
- 1
src/Discord.Net.Commands/Permissions/Levels/PermissionLevelChecker.cs 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;
} }


+ 2
- 1
src/Discord.Net.Commands/Permissions/Userlist/BlacklistChecker.cs 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);
} }
} }


+ 2
- 1
src/Discord.Net.Commands/Permissions/Userlist/WhitelistChecker.cs 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);
} }
} }


+ 11
- 2
src/Discord.Net.Modules/ModuleChecker.cs 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;
}
} }
} }
} }

Loading…
Cancel
Save