Browse Source

Added group support to help

tags/docs-0.9
RogueException 9 years ago
parent
commit
5d548dca33
3 changed files with 41 additions and 23 deletions
  1. +32
    -17
      src/Discord.Net.Commands/CommandMap.cs
  2. +1
    -1
      src/Discord.Net.Commands/CommandParser.cs
  3. +8
    -5
      src/Discord.Net.Commands/CommandService.cs

+ 32
- 17
src/Discord.Net.Commands/CommandMap.cs View File

@@ -4,33 +4,43 @@ using System.Linq;


namespace Discord.Commands namespace Discord.Commands
{ {
internal class CommandMap
//Represents either a single function, command group, or both
internal class CommandMap
{ {
private CommandMap _parent;
private readonly CommandMap _parent;
private readonly string _text;

private Command _command; private Command _command;
private readonly Dictionary<string, CommandMap> _subCommands;
private readonly Dictionary<string, CommandMap> _items;
private int _minPermission;
private bool _isHidden;


public Command Command => _command;
public IEnumerable<Command> SubCommands => _subCommands.Select(x => x.Value.Command).Where(x => x != null);
public string Text => _text;
public int MinPermissions => _minPermission;
public bool IsHidden => _isHidden;
public IEnumerable<Command> SubCommands => _items.Select(x => x.Value._command).Where(x => x != null);
public IEnumerable<CommandMap> SubGroups => _items.Select(x => x.Value).Where(x => x._items.Count > 0);


public CommandMap(CommandMap parent)
public CommandMap(CommandMap parent, string text)
{ {
_parent = parent; _parent = parent;
_subCommands = new Dictionary<string, CommandMap>();
}
_text = text;
_items = new Dictionary<string, CommandMap>();
_isHidden = true;
}
public CommandMap GetMap(string text)
public CommandMap GetItem(string text)
{ {
return GetMap(0, text.Split(' '));
return GetItem(0, text.Split(' '));
} }
public CommandMap GetMap(int index, string[] parts)
public CommandMap GetItem(int index, string[] parts)
{ {
if (index != parts.Length) if (index != parts.Length)
{ {
string nextPart = parts[index]; string nextPart = parts[index];
CommandMap nextGroup; CommandMap nextGroup;
if (_subCommands.TryGetValue(nextPart, out nextGroup))
return nextGroup.GetMap(index + 1, parts);
if (_items.TryGetValue(nextPart, out nextGroup))
return nextGroup.GetItem(index + 1, parts);
else else
return null; return null;
} }
@@ -56,7 +66,7 @@ namespace Discord.Commands
{ {
string nextPart = parts[index]; string nextPart = parts[index];
CommandMap nextGroup; CommandMap nextGroup;
if (_subCommands.TryGetValue(nextPart, out nextGroup))
if (_items.TryGetValue(nextPart, out nextGroup))
{ {
var cmd = nextGroup.GetCommand(index + 1, parts); var cmd = nextGroup.GetCommand(index + 1, parts);
if (cmd != null) if (cmd != null)
@@ -77,12 +87,17 @@ namespace Discord.Commands
{ {
if (index != parts.Length) if (index != parts.Length)
{ {
if (command.MinPermissions < _minPermission)
_minPermission = command.MinPermissions;
if (!command.IsHidden && _isHidden)
_isHidden = false;

string nextPart = parts[index]; string nextPart = parts[index];
CommandMap nextGroup; CommandMap nextGroup;
if (!_subCommands.TryGetValue(nextPart, out nextGroup))
if (!_items.TryGetValue(nextPart, out nextGroup))
{ {
nextGroup = new CommandMap(this);
_subCommands.Add(nextPart, nextGroup);
nextGroup = new CommandMap(this, nextPart);
_items.Add(nextPart, nextGroup);
} }
nextGroup.AddCommand(index + 1, parts, command); nextGroup.AddCommand(index + 1, parts, command);
} }


+ 1
- 1
src/Discord.Net.Commands/CommandParser.cs View File

@@ -40,7 +40,7 @@ namespace Discord.Commands
startPosition = endPosition; startPosition = endPosition;
else else
{ {
var newMap = map.GetMap(temp);
var newMap = map.GetItem(temp);
if (newMap != null) if (newMap != null)
{ {
map = newMap; map = newMap;


+ 8
- 5
src/Discord.Net.Commands/CommandService.cs View File

@@ -22,7 +22,7 @@ namespace Discord.Commands
{ {
Config = config; Config = config;
_commands = new List<Command>(); _commands = new List<Command>();
_map = new CommandMap(null);
_map = new CommandMap(null, null);
} }


void IService.Install(DiscordClient client) void IService.Install(DiscordClient client)
@@ -121,14 +121,17 @@ namespace Discord.Commands
int permissions = Config.PermissionResolver(user); int permissions = Config.PermissionResolver(user);


StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
output.AppendLine("These are the commands you can use:");

output.AppendLine("These are the commands you can use:");
output.Append(string.Join(", ", _map.SubCommands.Distinct() output.Append(string.Join(", ", _map.SubCommands.Distinct()
.Where(x => permissions >= x.MinPermissions && !x.IsHidden) .Where(x => permissions >= x.MinPermissions && !x.IsHidden)
.Select(x => '`' + x.Text + '`' + .Select(x => '`' + x.Text + '`' +
(x.Aliases.Count() > 0 ? ", `" + string.Join("`, `", x.Aliases) + '`' : "")))); (x.Aliases.Count() > 0 ? ", `" + string.Join("`, `", x.Aliases) + '`' : ""))));
output.AppendLine("\nThese are the groups you can access:");
output.Append(string.Join(", ", _map.SubGroups.Distinct()
.Where(x => permissions >= x.MinPermissions && !x.IsHidden)
.Select(x => '`' + x.Text + '`')));


var chars = Config.CommandChars;
var chars = Config.CommandChars;
if (chars.Length > 0) if (chars.Length > 0)
{ {
if (chars.Length == 1) if (chars.Length == 1)
@@ -162,7 +165,7 @@ namespace Discord.Commands


output.AppendLine($": {command.Description ?? "No description set for this command."}"); output.AppendLine($": {command.Description ?? "No description set for this command."}");


var sub = _map.GetMap(command.Text).SubCommands;
var sub = _map.GetItem(command.Text).SubCommands;
if (sub.Count() > 0) if (sub.Count() > 0)
{ {
int permissions = Config.PermissionResolver(user); int permissions = Config.PermissionResolver(user);


Loading…
Cancel
Save