Finish implementation of command builders
This commit is contained in:
@@ -15,7 +15,7 @@ namespace Discord.Commands
|
||||
private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList));
|
||||
private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();
|
||||
|
||||
private readonly Func<CommandContext, object[], Task> _action;
|
||||
private readonly Func<CommandContext, object[], IDependencyMap, Task> _action;
|
||||
|
||||
public ModuleInfo Module { get; }
|
||||
public string Name { get; }
|
||||
@@ -89,7 +89,7 @@ namespace Discord.Commands
|
||||
return await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public Task<ExecuteResult> Execute(CommandContext context, ParseResult parseResult)
|
||||
public Task<ExecuteResult> Execute(CommandContext context, ParseResult parseResult, IDependencyMap map)
|
||||
{
|
||||
if (!parseResult.IsSuccess)
|
||||
return Task.FromResult(ExecuteResult.FromError(parseResult));
|
||||
@@ -110,23 +110,26 @@ namespace Discord.Commands
|
||||
paramList[i] = parseResult.ParamValues[i].Values.First().Value;
|
||||
}
|
||||
|
||||
return Execute(context, argList, paramList);
|
||||
return Execute(context, argList, paramList, map);
|
||||
}
|
||||
public async Task<ExecuteResult> Execute(CommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList)
|
||||
public async Task<ExecuteResult> Execute(CommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IDependencyMap map)
|
||||
{
|
||||
if (map == null)
|
||||
map = DependencyMap.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
var args = GenerateArgs(argList, paramList);
|
||||
switch (RunMode)
|
||||
{
|
||||
case RunMode.Sync: //Always sync
|
||||
await _action(context, args).ConfigureAwait(false);
|
||||
await _action(context, args, map).ConfigureAwait(false);
|
||||
break;
|
||||
case RunMode.Mixed: //Sync until first await statement
|
||||
var t1 = _action(context, args);
|
||||
var t1 = _action(context, args, map);
|
||||
break;
|
||||
case RunMode.Async: //Always async
|
||||
var t2 = Task.Run(() => _action(context, args));
|
||||
var t2 = Task.Run(() => _action(context, args, map));
|
||||
break;
|
||||
}
|
||||
return ExecuteResult.FromSuccess();
|
||||
|
||||
@@ -31,14 +31,15 @@ namespace Discord.Commands
|
||||
Preconditions = BuildPreconditions(builder).ToImmutableArray();
|
||||
}
|
||||
|
||||
private static List<string> BuildAliases(ModuleBuilder builder)
|
||||
private static IEnumerable<string> BuildAliases(ModuleBuilder builder)
|
||||
{
|
||||
IEnumerable<string> result = null;
|
||||
|
||||
Stack<ModuleBuilder> builderStack = new Stack<ModuleBuilder>();
|
||||
builderStack.Push(builder);
|
||||
|
||||
ModuleBuilder parent = builder;
|
||||
while (parent.ParentModule != null)
|
||||
ModuleBuilder parent = builder.ParentModule;
|
||||
while (parent != null)
|
||||
{
|
||||
builderStack.Push(parent);
|
||||
parent = parent.ParentModule;
|
||||
@@ -49,11 +50,13 @@ namespace Discord.Commands
|
||||
ModuleBuilder level = builderStack.Pop(); // get the topmost builder
|
||||
if (result == null)
|
||||
result = level.Aliases.ToList(); // create a shallow copy so we don't overwrite the builder unexpectedly
|
||||
else
|
||||
else if (result.Count() > level.Aliases.Count)
|
||||
result = result.Permutate(level.Aliases, (first, second) => first + " " + second);
|
||||
else
|
||||
result = level.Aliases.Permutate(result, (second, first) => first + " " + second);
|
||||
}
|
||||
|
||||
return result.ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<PreconditionAttribute> BuildPreconditions(ModuleBuilder builder)
|
||||
|
||||
Reference in New Issue
Block a user