Browse Source

Make overload builders actually useful

*facepalm* I can't believe I missed this.
This commit also improves command attribute building
pull/394/head
FiniteReality 8 years ago
parent
commit
c9b892cffb
3 changed files with 22 additions and 11 deletions
  1. +12
    -9
      src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
  2. +9
    -1
      src/Discord.Net.Commands/Builders/OverloadBuilder.cs
  3. +1
    -1
      src/Discord.Net.Commands/CommandService.cs

+ 12
- 9
src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs View File

@@ -110,32 +110,35 @@ namespace Discord.Commands
{
builder.AddCommand((command) =>
{
string firstName = null;

foreach (var method in overloads)
{
if (firstName == null)
firstName = method.Name;

command.AddOverload((overload) =>
{
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)
{
// TODO: C#7 type switch
if (attribute is 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;
}
else if (attribute is NameAttribute)
@@ -149,7 +152,7 @@ namespace Discord.Commands
}

if (builder.Name == null)
builder.Name = method.Name;
builder.Name = defaultName;
}

private static void BuildOverload(OverloadBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service)


+ 9
- 1
src/Discord.Net.Commands/Builders/OverloadBuilder.cs View File

@@ -4,6 +4,8 @@ using System.Threading.Tasks;
using System.Collections.Generic;
using System.Diagnostics;

using CommandCallback = System.Func<Discord.Commands.CommandContext, object[], Discord.Commands.IDependencyMap, System.Threading.Tasks.Task>;

namespace Discord.Commands.Builders
{
public class OverloadBuilder
@@ -12,7 +14,7 @@ namespace Discord.Commands.Builders
private readonly List<ParameterBuilder> _parameters;

public CommandBuilder Command { get; }
internal Func<CommandContext, object[], IDependencyMap, Task> Callback { get; set; }
public CommandCallback Callback { get; set; }

public RunMode RunMode { get; set; }
public int Priority { get; set; }
@@ -40,6 +42,12 @@ namespace Discord.Commands.Builders
return this;
}

public OverloadBuilder WithCallback(CommandCallback callback)
{
Callback = callback;
return this;
}

public OverloadBuilder AddPrecondition(PreconditionAttribute precondition)
{
_preconditions.Add(precondition);


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

@@ -228,7 +228,7 @@ namespace Discord.Commands
public SearchResult Search(CommandContext context, string input)
{
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)
return SearchResult.FromSuccess(input, matches);


Loading…
Cancel
Save