Ejemplo n.º 1
0
        protected async Task <IResult> RunAsync(IInteractionContext context, object[] args, IServiceProvider services)
        {
            switch (RunMode)
            {
            case RunMode.Sync:
            {
                if (CommandService._autoServiceScopes)
                {
                    using var scope = services?.CreateScope();
                    return(await ExecuteInternalAsync(context, args, scope?.ServiceProvider ?? EmptyServiceProvider.Instance).ConfigureAwait(false));
                }
                else
                {
                    return(await ExecuteInternalAsync(context, args, services).ConfigureAwait(false));
                }
            }

            case RunMode.Async:
                _ = Task.Run(async() =>
                {
                    if (CommandService._autoServiceScopes)
                    {
                        using var scope = services?.CreateScope();
                        await ExecuteInternalAsync(context, args, scope?.ServiceProvider ?? EmptyServiceProvider.Instance).ConfigureAwait(false);
                    }
                    else
                    {
                        await ExecuteInternalAsync(context, args, services).ConfigureAwait(false);
                    }
                });
                break;

            default:
                throw new InvalidOperationException($"RunMode {RunMode} is not supported.");
            }

            return(ExecuteResult.FromSuccess());
        }
        /// <inheritdoc/>
        public async Task <IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter,
                                                 IServiceProvider services)
        {
            switch (InteractionService._runMode)
            {
            case RunMode.Sync:
            {
                return(await ExecuteInternalAsync(context, autocompleteInteraction, parameter, services).ConfigureAwait(false));
            }

            case RunMode.Async:
                _ = Task.Run(async() =>
                {
                    await ExecuteInternalAsync(context, autocompleteInteraction, parameter, services).ConfigureAwait(false);
                });
                break;

            default:
                throw new InvalidOperationException($"RunMode {InteractionService._runMode} is not supported.");
            }

            return(ExecuteResult.FromSuccess());
        }
Ejemplo n.º 3
0
        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);
            }
        }