Browse Source

feature: consolidate all results into CommandExecuted

This resolves #694.

This is a breaking change!
- changes the signature of CommandExecuted from (CommandInfo, ...) to
(Optional<CommandInfo>, ...), since we are now including Search result
failures in the event (and a command isn't accessible yet).
pull/1164/head
Christopher F 7 years ago
parent
commit
73d9abb87e
2 changed files with 17 additions and 3 deletions
  1. +14
    -3
      src/Discord.Net.Commands/CommandService.cs
  2. +3
    -0
      src/Discord.Net.Commands/Info/CommandInfo.cs

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

@@ -49,8 +49,8 @@ namespace Discord.Commands
/// Should the command encounter any of the aforementioned error, this event will not be raised. /// Should the command encounter any of the aforementioned error, this event will not be raised.
/// </para> /// </para>
/// </remarks> /// </remarks>
public event Func<CommandInfo, ICommandContext, IResult, Task> CommandExecuted { add { _commandExecutedEvent.Add(value); } remove { _commandExecutedEvent.Remove(value); } }
internal readonly AsyncEvent<Func<CommandInfo, ICommandContext, IResult, Task>> _commandExecutedEvent = new AsyncEvent<Func<CommandInfo, ICommandContext, IResult, Task>>();
public event Func<Optional<CommandInfo>, ICommandContext, IResult, Task> CommandExecuted { add { _commandExecutedEvent.Add(value); } remove { _commandExecutedEvent.Remove(value); } }
internal readonly AsyncEvent<Func<Optional<CommandInfo>, ICommandContext, IResult, Task>> _commandExecutedEvent = new AsyncEvent<Func<Optional<CommandInfo>, ICommandContext, IResult, Task>>();


private readonly SemaphoreSlim _moduleLock; private readonly SemaphoreSlim _moduleLock;
private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs; private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs;
@@ -512,7 +512,11 @@ namespace Discord.Commands


var searchResult = Search(input); var searchResult = Search(input);
if (!searchResult.IsSuccess) if (!searchResult.IsSuccess)
{
await _commandExecutedEvent.InvokeAsync(Optional.Create<CommandInfo>(), context, searchResult).ConfigureAwait(false);
return searchResult; return searchResult;
}


var commands = searchResult.Commands; var commands = searchResult.Commands;
var preconditionResults = new Dictionary<CommandMatch, PreconditionResult>(); var preconditionResults = new Dictionary<CommandMatch, PreconditionResult>();
@@ -532,6 +536,8 @@ namespace Discord.Commands
var bestCandidate = preconditionResults var bestCandidate = preconditionResults
.OrderByDescending(x => x.Key.Command.Priority) .OrderByDescending(x => x.Key.Command.Priority)
.FirstOrDefault(x => !x.Value.IsSuccess); .FirstOrDefault(x => !x.Value.IsSuccess);

await _commandExecutedEvent.InvokeAsync(bestCandidate.Key.Command, context, bestCandidate.Value).ConfigureAwait(false);
return bestCandidate.Value; return bestCandidate.Value;
} }


@@ -589,12 +595,17 @@ namespace Discord.Commands
//All parses failed, return the one from the highest priority command, using score as a tie breaker //All parses failed, return the one from the highest priority command, using score as a tie breaker
var bestMatch = parseResults var bestMatch = parseResults
.FirstOrDefault(x => !x.Value.IsSuccess); .FirstOrDefault(x => !x.Value.IsSuccess);

await _commandExecutedEvent.InvokeAsync(bestMatch.Key.Command, context, bestMatch.Value).ConfigureAwait(false);
return bestMatch.Value; return bestMatch.Value;
} }


//If we get this far, at least one parse was successful. Execute the most likely overload. //If we get this far, at least one parse was successful. Execute the most likely overload.
var chosenOverload = successfulParses[0]; var chosenOverload = successfulParses[0];
return await chosenOverload.Key.ExecuteAsync(context, chosenOverload.Value, services).ConfigureAwait(false);
var result = await chosenOverload.Key.ExecuteAsync(context, chosenOverload.Value, services).ConfigureAwait(false);
if (!result.IsSuccess) // succesful results raise the event in CommandInfo#ExecuteInternalAsync (have to raise it there b/c deffered execution)
await _commandExecutedEvent.InvokeAsync(chosenOverload.Key.Command, context, result);
return result;
} }
} }
} }

+ 3
- 0
src/Discord.Net.Commands/Info/CommandInfo.cs View File

@@ -179,7 +179,10 @@ namespace Discord.Commands
public Task<IResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services) public Task<IResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)
{ {
if (!parseResult.IsSuccess) if (!parseResult.IsSuccess)
{

return Task.FromResult((IResult)ExecuteResult.FromError(parseResult)); return Task.FromResult((IResult)ExecuteResult.FromError(parseResult));
}


var argList = new object[parseResult.ArgValues.Count]; var argList = new object[parseResult.ArgValues.Count];
for (int i = 0; i < parseResult.ArgValues.Count; i++) for (int i = 0; i < parseResult.ArgValues.Count; i++)


Loading…
Cancel
Save