*facepalm* I can't believe I missed this. This commit also improves command attribute buildingpull/394/head
@@ -110,32 +110,35 @@ namespace Discord.Commands | |||||
{ | { | ||||
builder.AddCommand((command) => | builder.AddCommand((command) => | ||||
{ | { | ||||
string firstName = null; | |||||
foreach (var method in overloads) | foreach (var method in overloads) | ||||
{ | { | ||||
if (firstName == null) | |||||
firstName = method.Name; | |||||
command.AddOverload((overload) => | command.AddOverload((overload) => | ||||
{ | { | ||||
BuildOverload(overload, typeInfo, method, service); | BuildOverload(overload, typeInfo, method, service); | ||||
}); | }); | ||||
} | } | ||||
var defaultOverload = overloads.OrderByDescending(x => x.GetCustomAttribute<PriorityAttribute>()?.Priority ?? 0).First(); | |||||
BuildCommand(command, defaultOverload, service); | |||||
var allAttributes = overloads.SelectMany(x => x.GetCustomAttributes()); | |||||
BuildCommand(command, firstName, allAttributes, service); | |||||
}); | }); | ||||
} | } | ||||
} | } | ||||
private static void BuildCommand(CommandBuilder builder, MethodInfo method, CommandService service) | |||||
{ | |||||
var attributes = method.GetCustomAttributes(); | |||||
private static void BuildCommand(CommandBuilder builder, string defaultName, IEnumerable<Attribute> attributes, CommandService service) | |||||
{ | |||||
foreach (var attribute in attributes) | foreach (var attribute in attributes) | ||||
{ | { | ||||
// TODO: C#7 type switch | // TODO: C#7 type switch | ||||
if (attribute is CommandAttribute) | if (attribute is CommandAttribute) | ||||
{ | { | ||||
var cmdAttr = attribute as CommandAttribute; | var cmdAttr = attribute as CommandAttribute; | ||||
builder.AddAliases(cmdAttr.Text); | |||||
if (!builder.Aliases.Contains(cmdAttr.Text)) | |||||
builder.AddAliases(cmdAttr.Text); | |||||
builder.Name = builder.Name ?? cmdAttr.Text; | builder.Name = builder.Name ?? cmdAttr.Text; | ||||
} | } | ||||
else if (attribute is NameAttribute) | else if (attribute is NameAttribute) | ||||
@@ -149,7 +152,7 @@ namespace Discord.Commands | |||||
} | } | ||||
if (builder.Name == null) | if (builder.Name == null) | ||||
builder.Name = method.Name; | |||||
builder.Name = defaultName; | |||||
} | } | ||||
private static void BuildOverload(OverloadBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service) | private static void BuildOverload(OverloadBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service) | ||||
@@ -4,6 +4,8 @@ using System.Threading.Tasks; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Diagnostics; | using System.Diagnostics; | ||||
using CommandCallback = System.Func<Discord.Commands.CommandContext, object[], Discord.Commands.IDependencyMap, System.Threading.Tasks.Task>; | |||||
namespace Discord.Commands.Builders | namespace Discord.Commands.Builders | ||||
{ | { | ||||
public class OverloadBuilder | public class OverloadBuilder | ||||
@@ -12,7 +14,7 @@ namespace Discord.Commands.Builders | |||||
private readonly List<ParameterBuilder> _parameters; | private readonly List<ParameterBuilder> _parameters; | ||||
public CommandBuilder Command { get; } | public CommandBuilder Command { get; } | ||||
internal Func<CommandContext, object[], IDependencyMap, Task> Callback { get; set; } | |||||
public CommandCallback Callback { get; set; } | |||||
public RunMode RunMode { get; set; } | public RunMode RunMode { get; set; } | ||||
public int Priority { get; set; } | public int Priority { get; set; } | ||||
@@ -40,6 +42,12 @@ namespace Discord.Commands.Builders | |||||
return this; | return this; | ||||
} | } | ||||
public OverloadBuilder WithCallback(CommandCallback callback) | |||||
{ | |||||
Callback = callback; | |||||
return this; | |||||
} | |||||
public OverloadBuilder AddPrecondition(PreconditionAttribute precondition) | public OverloadBuilder AddPrecondition(PreconditionAttribute precondition) | ||||
{ | { | ||||
_preconditions.Add(precondition); | _preconditions.Add(precondition); | ||||
@@ -228,7 +228,7 @@ namespace Discord.Commands | |||||
public SearchResult Search(CommandContext context, string input) | public SearchResult Search(CommandContext context, string input) | ||||
{ | { | ||||
string searchInput = _caseSensitive ? input : input.ToLowerInvariant(); | string searchInput = _caseSensitive ? input : input.ToLowerInvariant(); | ||||
var matches = _map.GetCommands(input).OrderBy(x => x.Overloads.Average(y => y.Priority)).ToImmutableArray(); | |||||
var matches = _map.GetCommands(searchInput).OrderBy(x => x.Overloads.Average(y => y.Priority)).ToImmutableArray(); | |||||
if (matches.Length > 0) | if (matches.Length > 0) | ||||
return SearchResult.FromSuccess(input, matches); | return SearchResult.FromSuccess(input, matches); | ||||