@@ -1,5 +1,4 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Collections.Generic; | |||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
@@ -11,15 +10,15 @@ namespace Discord.Commands | |||||
private readonly List<Command> _commands; | private readonly List<Command> _commands; | ||||
private readonly Dictionary<string, CommandMap> _items; | private readonly Dictionary<string, CommandMap> _items; | ||||
private bool _isHidden; | |||||
private bool _isVisible, _hasNonAliases, _hasSubGroups; | |||||
public string Name => _name; | public string Name => _name; | ||||
public string FullName => _fullName; | public string FullName => _fullName; | ||||
public bool IsHidden => _isHidden; | |||||
public bool IsVisible => _isVisible; | |||||
public bool HasNonAliases => _hasNonAliases; | |||||
public bool HasSubGroups => _hasSubGroups; | |||||
public IEnumerable<Command> Commands => _commands; | public IEnumerable<Command> Commands => _commands; | ||||
public IEnumerable<CommandMap> SubGroups => _items.Values; | public IEnumerable<CommandMap> SubGroups => _items.Values; | ||||
/*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, string name, string fullName) | public CommandMap(CommandMap parent, string name, string fullName) | ||||
{ | { | ||||
@@ -28,7 +27,9 @@ namespace Discord.Commands | |||||
_fullName = fullName; | _fullName = fullName; | ||||
_items = new Dictionary<string, CommandMap>(); | _items = new Dictionary<string, CommandMap>(); | ||||
_commands = new List<Command>(); | _commands = new List<Command>(); | ||||
_isHidden = true; | |||||
_isVisible = false; | |||||
_hasNonAliases = false; | |||||
_hasSubGroups = false; | |||||
} | } | ||||
public CommandMap GetItem(string text) | public CommandMap GetItem(string text) | ||||
@@ -81,29 +82,34 @@ namespace Discord.Commands | |||||
return null; | return null; | ||||
} | } | ||||
public void AddCommand(string text, Command command) | |||||
public void AddCommand(string text, Command command, bool isAlias) | |||||
{ | { | ||||
AddCommand(0, text.Split(' '), command); | |||||
AddCommand(0, text.Split(' '), command, isAlias); | |||||
} | } | ||||
private void AddCommand(int index, string[] parts, Command command) | |||||
private void AddCommand(int index, string[] parts, Command command, bool isAlias) | |||||
{ | { | ||||
if (!command.IsHidden && _isHidden) | |||||
_isHidden = false; | |||||
if (!command.IsHidden) | |||||
_isVisible = true; | |||||
if (index != parts.Length) | if (index != parts.Length) | ||||
{ | { | ||||
CommandMap nextGroup; | CommandMap nextGroup; | ||||
string name = parts[index].ToLowerInvariant(); | string name = parts[index].ToLowerInvariant(); | ||||
string fullName = string.Join(" ", parts, 0, index + 1); | string fullName = string.Join(" ", parts, 0, index + 1); | ||||
if (!_items.TryGetValue(name, out nextGroup)) | |||||
if (!_items.TryGetValue(name, out nextGroup)) | |||||
{ | { | ||||
nextGroup = new CommandMap(this, name, fullName); | nextGroup = new CommandMap(this, name, fullName); | ||||
_items.Add(name, nextGroup); | _items.Add(name, nextGroup); | ||||
_hasSubGroups = true; | |||||
} | } | ||||
nextGroup.AddCommand(index + 1, parts, command); | |||||
} | |||||
nextGroup.AddCommand(index + 1, parts, command, isAlias); | |||||
} | |||||
else | else | ||||
{ | |||||
_commands.Add(command); | _commands.Add(command); | ||||
if (!isAlias) | |||||
_hasNonAliases = true; | |||||
} | |||||
} | } | ||||
public bool CanRun(User user, Channel channel, out string error) | public bool CanRun(User user, Channel channel, out string error) | ||||
@@ -60,8 +60,7 @@ namespace Discord.Commands | |||||
else | else | ||||
await client.SendMessage(replyChannel, "Unable to display help: Unknown command."); | await client.SendMessage(replyChannel, "Unable to display help: Unknown command."); | ||||
} | } | ||||
else //Show general help | |||||
else //Show general help | |||||
await ShowGeneralHelp(e.User, e.Channel, replyChannel); | await ShowGeneralHelp(e.User, e.Channel, replyChannel); | ||||
})); | })); | ||||
} | } | ||||
@@ -143,16 +142,6 @@ namespace Discord.Commands | |||||
public Task ShowGeneralHelp(User user, Channel channel, Channel replyChannel = null) | public Task ShowGeneralHelp(User user, Channel channel, Channel replyChannel = null) | ||||
{ | { | ||||
StringBuilder output = new StringBuilder(); | StringBuilder output = new StringBuilder(); | ||||
/*output.AppendLine("These are the commands you can use:"); | |||||
output.Append(string.Join(", ", _map.SubCommands | |||||
.Where(x => x.CanRun(user, channel) && !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 | |||||
.Where(x => /*x.CanRun(user, channel)*//* && !x.IsHidden) | |||||
.Select(x => '`' + x.Text + '`')));*/ | |||||
bool isFirstCategory = true; | bool isFirstCategory = true; | ||||
foreach (var category in _categories) | foreach (var category in _categories) | ||||
{ | { | ||||
@@ -160,9 +149,9 @@ namespace Discord.Commands | |||||
foreach (var group in category.Value.SubGroups) | foreach (var group in category.Value.SubGroups) | ||||
{ | { | ||||
string error; | string error; | ||||
if (!group.IsHidden && group.CanRun(user, channel, out error)) | |||||
if (group.IsVisible && (group.HasSubGroups || group.HasNonAliases) && group.CanRun(user, channel, out error)) | |||||
{ | { | ||||
if (isFirstItem) | |||||
if (isFirstItem) | |||||
{ | { | ||||
isFirstItem = false; | isFirstItem = false; | ||||
//This is called for the first item in each category. If we never get here, we dont bother writing the header for a category type (since it's empty) | //This is called for the first item in each category. If we never get here, we dont bother writing the header for a category type (since it's empty) | ||||
@@ -184,7 +173,7 @@ namespace Discord.Commands | |||||
output.Append(", "); | output.Append(", "); | ||||
output.Append('`'); | output.Append('`'); | ||||
output.Append(group.Name); | output.Append(group.Name); | ||||
if (group.SubGroups.Any()) | |||||
if (group.HasSubGroups) | |||||
output.Append("*"); | output.Append("*"); | ||||
output.Append('`'); | output.Append('`'); | ||||
} | } | ||||
@@ -244,7 +233,7 @@ namespace Discord.Commands | |||||
} | } | ||||
bool isFirstSubCmd = true; | bool isFirstSubCmd = true; | ||||
foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel, out error) && !x.IsHidden)) | |||||
foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel, out error) && !x.IsVisible)) | |||||
{ | { | ||||
if (isFirstSubCmd) | if (isFirstSubCmd) | ||||
{ | { | ||||
@@ -324,14 +313,14 @@ namespace Discord.Commands | |||||
} | } | ||||
//Add main command | //Add main command | ||||
category.AddCommand(command.Text, command); | |||||
_map.AddCommand(command.Text, command); | |||||
category.AddCommand(command.Text, command, false); | |||||
_map.AddCommand(command.Text, command, false); | |||||
//Add aliases | //Add aliases | ||||
foreach (var alias in command.Aliases) | foreach (var alias in command.Aliases) | ||||
{ | { | ||||
category.AddCommand(alias, command); | |||||
_map.AddCommand(alias, command); | |||||
category.AddCommand(alias, command, true); | |||||
_map.AddCommand(alias, command, true); | |||||
} | } | ||||
} | } | ||||
} | } | ||||