diff --git a/src/Discord.Net.Commands/CommandMap.cs b/src/Discord.Net.Commands/CommandMap.cs index d066a8910..b528dd49c 100644 --- a/src/Discord.Net.Commands/CommandMap.cs +++ b/src/Discord.Net.Commands/CommandMap.cs @@ -4,33 +4,43 @@ using System.Linq; 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 readonly Dictionary _subCommands; + private readonly Dictionary _items; + private int _minPermission; + private bool _isHidden; - public Command Command => _command; - public IEnumerable 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 SubCommands => _items.Select(x => x.Value._command).Where(x => x != null); + public IEnumerable SubGroups => _items.Select(x => x.Value).Where(x => x._items.Count > 0); - public CommandMap(CommandMap parent) + public CommandMap(CommandMap parent, string text) { _parent = parent; - _subCommands = new Dictionary(); - } + _text = text; + _items = new Dictionary(); + _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) { string nextPart = parts[index]; 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 return null; } @@ -56,7 +66,7 @@ namespace Discord.Commands { string nextPart = parts[index]; CommandMap nextGroup; - if (_subCommands.TryGetValue(nextPart, out nextGroup)) + if (_items.TryGetValue(nextPart, out nextGroup)) { var cmd = nextGroup.GetCommand(index + 1, parts); if (cmd != null) @@ -77,12 +87,17 @@ namespace Discord.Commands { if (index != parts.Length) { + if (command.MinPermissions < _minPermission) + _minPermission = command.MinPermissions; + if (!command.IsHidden && _isHidden) + _isHidden = false; + string nextPart = parts[index]; 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); } diff --git a/src/Discord.Net.Commands/CommandParser.cs b/src/Discord.Net.Commands/CommandParser.cs index 90b00e977..52d1ba114 100644 --- a/src/Discord.Net.Commands/CommandParser.cs +++ b/src/Discord.Net.Commands/CommandParser.cs @@ -40,7 +40,7 @@ namespace Discord.Commands startPosition = endPosition; else { - var newMap = map.GetMap(temp); + var newMap = map.GetItem(temp); if (newMap != null) { map = newMap; diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index ba51d7479..2b1794b4d 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -22,7 +22,7 @@ namespace Discord.Commands { Config = config; _commands = new List(); - _map = new CommandMap(null); + _map = new CommandMap(null, null); } void IService.Install(DiscordClient client) @@ -121,14 +121,17 @@ namespace Discord.Commands int permissions = Config.PermissionResolver(user); 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() .Where(x => permissions >= x.MinPermissions && !x.IsHidden) .Select(x => '`' + x.Text + '`' + (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 == 1) @@ -162,7 +165,7 @@ namespace Discord.Commands 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) { int permissions = Config.PermissionResolver(user);