Browse Source

Fixed a few command bugs and cleaned up several parts.

tags/docs-0.9
RogueException 9 years ago
parent
commit
7cd1119ab7
3 changed files with 94 additions and 50 deletions
  1. +0
    -2
      src/Discord.Net.Commands/CommandBuilder.cs
  2. +7
    -7
      src/Discord.Net.Commands/CommandMap.cs
  3. +87
    -41
      src/Discord.Net.Commands/CommandService.cs

+ 0
- 2
src/Discord.Net.Commands/CommandBuilder.cs View File

@@ -75,8 +75,6 @@ namespace Discord.Commands
{ {
_command.SetParameters(_params.ToArray()); _command.SetParameters(_params.ToArray());
_service.AddCommand(_command); _service.AddCommand(_command);
foreach (var alias in _command.Aliases)
_service.Map.AddCommand(alias, _command);
} }


internal static string AppendPrefix(string prefix, string cmd) internal static string AppendPrefix(string prefix, string cmd)


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

@@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;


namespace Discord.Commands namespace Discord.Commands
{ {
@@ -16,9 +15,10 @@ namespace Discord.Commands


public string Text => _text; public string Text => _text;
public bool IsHidden => _isHidden; public bool IsHidden => _isHidden;
public IEnumerable<KeyValuePair<string, CommandMap>> Items => _items;
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 Command Command => _command;
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 text) public CommandMap(CommandMap parent, string text)
{ {
@@ -84,11 +84,11 @@ namespace Discord.Commands
} }
public void AddCommand(int index, string[] parts, Command command) public void AddCommand(int index, string[] parts, Command command)
{ {
if (!command.IsHidden && _isHidden)
_isHidden = false;

if (index != parts.Length) if (index != parts.Length)
{ {
if (!command.IsHidden && _isHidden)
_isHidden = false;

string nextPart = parts[index]; string nextPart = parts[index];
CommandMap nextGroup; CommandMap nextGroup;
if (!_items.TryGetValue(nextPart, out nextGroup)) if (!_items.TryGetValue(nextPart, out nextGroup))


+ 87
- 41
src/Discord.Net.Commands/CommandService.cs View File

@@ -17,7 +17,6 @@ namespace Discord.Commands
private readonly List<Command> _allCommands; private readonly List<Command> _allCommands;


//Command map stores all commands by their input text, used for fast resolving and parsing //Command map stores all commands by their input text, used for fast resolving and parsing
internal CommandMap Map => _map;
private readonly CommandMap _map; private readonly CommandMap _map;


//Groups store all commands by their module, used for more informative help //Groups store all commands by their module, used for more informative help
@@ -49,11 +48,11 @@ namespace Discord.Commands
Channel channel = Config.HelpMode == HelpMode.Public ? e.Channel : await client.CreatePMChannel(e.User); Channel channel = Config.HelpMode == HelpMode.Public ? e.Channel : await client.CreatePMChannel(e.User);
if (e.Args.Length > 0) //Show command help if (e.Args.Length > 0) //Show command help
{ {
var cmd = _map.GetCommand(string.Join(" ", e.Args));
if (cmd != null)
await ShowHelp(cmd, e.User, channel);
var map = _map.GetItem(string.Join(" ", e.Args));
if (map != null)
await ShowHelp(map, e.User, channel);
else else
await client.SendMessage(channel, "Unable to display help: unknown command.");
await client.SendMessage(channel, "Unable to display help: Unknown command.");
} }
else //Show general help else //Show general help
await ShowHelp(e.User, channel); await ShowHelp(e.User, channel);
@@ -139,10 +138,9 @@ namespace Discord.Commands
foreach (var category in _categories) foreach (var category in _categories)
{ {
bool isFirstItem = true; bool isFirstItem = true;
foreach (var item in category.Value.Items)
foreach (var group in category.Value.SubGroups)
{ {
var map = item.Value;
if (!map.IsHidden && map.CanRun(user, channel))
if (!group.IsHidden && group.CanRun(user, channel))
{ {
if (isFirstItem) if (isFirstItem)
{ {
@@ -165,9 +163,9 @@ namespace Discord.Commands
else else
output.Append(", "); output.Append(", ");
output.Append('`'); output.Append('`');
output.Append(map.Text);
if (map.Items.Any())
output.Append(@"\*");
output.Append(group.Text);
if (group.SubGroups.Any())
output.Append("*");
output.Append('`'); output.Append('`');
} }
} }
@@ -177,7 +175,7 @@ namespace Discord.Commands
output.Append("There are no commands you have permission to run."); output.Append("There are no commands you have permission to run.");
else else
{ {
output.AppendLine();
output.Append("\n\n");


var chars = Config.CommandChars; var chars = Config.CommandChars;
if (chars.Length > 0) if (chars.Length > 0)
@@ -186,45 +184,81 @@ namespace Discord.Commands
output.AppendLine($"You can use `{chars[0]}` to call a command."); output.AppendLine($"You can use `{chars[0]}` to call a command.");
else else
output.AppendLine($"You can use `{string.Join(" ", chars.Take(chars.Length - 1))}` or `{chars.Last()}` to call a command."); output.AppendLine($"You can use `{string.Join(" ", chars.Take(chars.Length - 1))}` or `{chars.Last()}` to call a command.");
output.AppendLine($"`{chars[0]}help <command>` can tell you more about how to use a command.");
} }
output.AppendLine("`help <command>` can tell you more about how to use a command.");
else
output.AppendLine($"`help <command>` can tell you more about how to use a command.");
} }


return _client.SendMessage(channel, output.ToString()); return _client.SendMessage(channel, output.ToString());
} }


public Task ShowHelp(Command command, User user, Channel channel)
private Task ShowHelp(CommandMap map, User user, Channel channel)
{ {
StringBuilder output = new StringBuilder();
output.Append($"`{command.Text}`");

foreach (string s in command.Parameters.Where(x => x.Type == ParameterType.Required)
.Select(x => x.Name))
output.Append($" <`{s}`>");
foreach (string s in command.Parameters.Where(x => x.Type == ParameterType.Optional)
.Select(x => x.Name))
output.Append($" [`{s}`]");
StringBuilder output = new StringBuilder();


if (command.Parameters.LastOrDefault(x => x.Type == ParameterType.Multiple) != null)
output.Append(" [`...`]");

if (command.Parameters.LastOrDefault(x => x.Type == ParameterType.Unparsed) != null)
output.Append(" [`--`]");

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

var sub = _map.GetItem(command.Text).SubCommands;
if (sub.Count() > 0)
{
output.AppendLine("Sub Commands: `" + string.Join("`, `", sub.Where(x => x.CanRun(user, channel) && !x.IsHidden)
.Select(x => x.Text.Substring(command.Text.Length + 1))) + '`');
}
Command cmd = map.Command;
if (cmd != null)
ShowHelpInternal(cmd, user, channel, output);
else
{
output.Append('`');
output.Append(map.Text);
output.Append("`\n");
}


if (command.Aliases.Count() > 0)
output.Append($"Aliases: `" + string.Join("`, `", command.Aliases) + '`');
bool isFirst = true;
foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel) && !x.IsHidden))
{
if (isFirst)
{
isFirst = false;
output.AppendLine("Sub Commands: ");
}
else
output.Append(", ");
output.Append('`');
output.Append(subCmd.Text);
if (subCmd.SubGroups.Any())
output.Append("*");
output.Append('`');
}


return _client.SendMessage(channel, output.ToString());
}
public Task ShowHelp(Command command, User user, Channel channel)
{
StringBuilder output = new StringBuilder();
ShowHelpInternal(command, user, channel, output);
return _client.SendMessage(channel, output.ToString()); return _client.SendMessage(channel, output.ToString());
}
private void ShowHelpInternal(Command command, User user, Channel channel, StringBuilder output)
{
output.Append('`');
output.Append(command.Text);
foreach (var param in command.Parameters)
{
switch (param.Type)
{
case ParameterType.Required:
output.Append($" <{param.Name}>");
break;
case ParameterType.Optional:
output.Append($" [{param.Name}]");
break;
case ParameterType.Multiple:
output.Append(" [...]");
break;
case ParameterType.Unparsed:
output.Append(" [--]");
break;
}
}
output.Append('`');
output.AppendLine($": {command.Description ?? "No description set for this command."}");

if (command.Aliases.Any())
output.AppendLine($"Aliases: `" + string.Join("`, `", command.Aliases) + '`');
} }


public void CreateGroup(string cmd, Action<CommandGroupBuilder> config = null) public void CreateGroup(string cmd, Action<CommandGroupBuilder> config = null)
@@ -242,6 +276,8 @@ namespace Discord.Commands
internal void AddCommand(Command command) internal void AddCommand(Command command)
{ {
_allCommands.Add(command); _allCommands.Add(command);

//Get category
CommandMap category; CommandMap category;
string categoryName = command.Category ?? ""; string categoryName = command.Category ?? "";
if (!_categories.TryGetValue(categoryName, out category)) if (!_categories.TryGetValue(categoryName, out category))
@@ -249,7 +285,17 @@ namespace Discord.Commands
category = new CommandMap(null, ""); category = new CommandMap(null, "");
_categories.Add(categoryName, category); _categories.Add(categoryName, category);
} }
_map.AddCommand(command.Text, command);

//Add main command
category.AddCommand(command.Text, command);
_map.AddCommand(command.Text, command);

//Add aliases
foreach (var alias in command.Aliases)
{
category.AddCommand(alias, command);
_map.AddCommand(alias, command);
}
} }
} }
} }

Loading…
Cancel
Save