Return best command candidate on CommandService.ExecuteAsync
See original GitHub issueCurrently CommandService.ExecuteAsync
does only return IResult
, which contains an error category and an error message, but not the command which may fail because of preconditions or missing arguments before executing it. This makes it hard to add an error handler for commands that fail before they are executed.
For example, I added a SyntaxAttribute
allowing to specify the correct command syntax and a short description for each argument and I want to display this command syntax if result.Error == CommandError.BadArgCount
. As the command fails before executing it, neither CommandService.CommandExecuted
nor CommandService.Log
is fired.
The only way to check for such errors is awaiting CommandService.ExecuteAsync
and check for its result.
Although this allows determining the error, it doesn’t return to which command match the result applies to. Currently, I use the following as a workaround:
var context = new SocketCommandContext(_client, message);
var result = await _service.ExecuteAsync(context, position, _provider);
if(!result.IsSuccess) {
if(result.Error != CommandError.UnknownCommand) {
var search = _service.Search(context, position);
if(search.IsSuccess && search.Commands.Count == 1) {
var command = search.Commands.First().Command;
// handle command failure here
}
}
}
This works fine as long as the result of CommandService.Search
returns exactly one command, but what if there are more than one matches? The CommandService itself determines the best candidate when returning early, but returns its result only:
CommandService L374 CommandService L432
I think it would be useful to return the candidate as well (adding a property to the IResult
interface or an additional event for pre-command failures.) or is there already a way to achieve this?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (6 by maintainers)
Top GitHub Comments
Why not look for the candidate yourself first? The methods are there for you to use.
Also, please use the issue tracker only for actual bugs. Questions about usage should go in our channel on the DAPI server.
I’m going to close this, since you can now use
CommandExecuted
to receive the command a result failed on (with the exception of search results - but you can apply your own logic for that)