private async Task <IResult> ExecuteInternalAsync(IInteractionContext context, object[] args, IServiceProvider services) { await CommandService._cmdLogger.DebugAsync($"Executing {GetLogString(context)}").ConfigureAwait(false); try { var preconditionResult = await CheckPreconditionsAsync(context, services).ConfigureAwait(false); if (!preconditionResult.IsSuccess) { await InvokeModuleEvent(context, preconditionResult).ConfigureAwait(false); return(preconditionResult); } var index = 0; foreach (var parameter in Parameters) { var result = await parameter.CheckPreconditionsAsync(context, args[index++], services).ConfigureAwait(false); if (!result.IsSuccess) { await InvokeModuleEvent(context, result).ConfigureAwait(false); return(result); } } var task = _action(context, args, services, this); if (task is Task <IResult> resultTask) { var result = await resultTask.ConfigureAwait(false); await InvokeModuleEvent(context, result).ConfigureAwait(false); if (result is RuntimeResult || result is ExecuteResult) { return(result); } } else { await task.ConfigureAwait(false); var result = ExecuteResult.FromSuccess(); await InvokeModuleEvent(context, result).ConfigureAwait(false); return(result); } var failResult = ExecuteResult.FromError(InteractionCommandError.Unsuccessful, "Command execution failed for an unknown reason"); await InvokeModuleEvent(context, failResult).ConfigureAwait(false); return(failResult); } catch (Exception ex) { var originalEx = ex; while (ex is TargetInvocationException) { ex = ex.InnerException; } await Module.CommandService._cmdLogger.ErrorAsync(ex).ConfigureAwait(false); var result = ExecuteResult.FromError(ex); await InvokeModuleEvent(context, result).ConfigureAwait(false); if (Module.CommandService._throwOnError) { if (ex == originalEx) { throw; } else { ExceptionDispatchInfo.Capture(ex).Throw(); } } return(result); } finally { await CommandService._cmdLogger.VerboseAsync($"Executed {GetLogString(context)}").ConfigureAwait(false); } }