예제 #1
0
        /// <inheritdoc />
        protected override async Task OnCommandFailedAsync
        (
            SocketCommandContext context,
            int commandStart,
            IResult result
        )
        {
            switch (result)
            {
            case ExecuteResult executeResult when executeResult.Error == CommandError.Exception:
            {
                await HandleInternalErrorAsync(context, executeResult);

                break;
            }

            case RuntimeCommandResult runtimeResult:
            {
                await _feedback.SendErrorAsync(context, runtimeResult.Reason);

                return;
            }
            }

            switch (result.Error)
            {
            case CommandError.UnknownCommand:
            {
                break;
            }

            case CommandError.ObjectNotFound:
            case CommandError.MultipleMatches:
            case CommandError.Unsuccessful:
            case CommandError.UnmetPrecondition:
            case CommandError.ParseFailed:
            case CommandError.BadArgCount:
            {
                var userDMChannel = await context.Message.Author.GetOrCreateDMChannelAsync();

                await SendPrivateMessageAsync(context, result.ErrorReason);

                var searchResult = this.Commands.Search(context, commandStart);
                if (searchResult.Commands.Any())
                {
                    await userDMChannel.SendMessageAsync
                    (
                        string.Empty,
                        false,
                        _help.CreateCommandUsageEmbed(searchResult.Commands)
                    );
                }

                break;
            }
            }
        }
예제 #2
0
        /// <inheritdoc />
        protected override async Task AfterCommandAsync(SocketCommandContext context, int commandStart, IResult result)
        {
            switch (result.Error)
            {
            case CommandError.UnknownCommand:
            {
                break;
            }

            case CommandError.ObjectNotFound:
            case CommandError.MultipleMatches:
            case CommandError.Unsuccessful:
            case CommandError.UnmetPrecondition:
            case CommandError.ParseFailed:
            case CommandError.BadArgCount:
            {
                var userDMChannel = await context.Message.Author.GetOrCreateDMChannelAsync();

                try
                {
                    var errorEmbed = _feedback.CreateFeedbackEmbed
                                     (
                        context.User,
                        Color.Red,
                        result.ErrorReason
                                     );

                    await userDMChannel.SendMessageAsync(string.Empty, false, errorEmbed);

                    var searchResult = this.Commands.Search(context, commandStart);
                    if (searchResult.Commands.Any())
                    {
                        await userDMChannel.SendMessageAsync
                        (
                            string.Empty,
                            false,
                            _help.CreateCommandUsageEmbed(searchResult.Commands)
                        );
                    }
                }
                catch (HttpException hex) when(hex.WasCausedByDMsNotAccepted())
                {
                }
                finally
                {
                    await userDMChannel.CloseAsync();
                }

                break;
            }
            }
        }
        /// <summary>
        /// Handles the result of a command, alerting the user if errors occurred.
        /// </summary>
        /// <param name="context">The context of the command.</param>
        /// <param name="result">The result of the command.</param>
        /// <param name="argumentPos">The position in the message string where the command starts.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task HandleCommandResultAsync
        (
            [NotNull] ICommandContext context,
            [NotNull] IResult result,
            int argumentPos
        )
        {
            if (result.IsSuccess)
            {
                return;
            }

            switch (result.Error)
            {
            case CommandError.Exception:
            {
                await HandleInternalErrorAsync(context, result);

                break;
            }

            case CommandError.UnknownCommand:
            {
                break;
            }

            case CommandError.ObjectNotFound:
            case CommandError.MultipleMatches:
            case CommandError.Unsuccessful:
            case CommandError.UnmetPrecondition:
            case CommandError.ParseFailed:
            case CommandError.BadArgCount:
            {
                var userDMChannel = await context.Message.Author.GetOrCreateDMChannelAsync();

                try
                {
                    var errorEmbed = _feedback.CreateFeedbackEmbed
                                     (
                        context.User,
                        Color.Red,
                        result.ErrorReason
                                     );

                    await userDMChannel.SendMessageAsync(string.Empty, false, errorEmbed);

                    var searchResult = _commands.Search(context, argumentPos);
                    if (searchResult.Commands.Any())
                    {
                        await userDMChannel.SendMessageAsync
                        (
                            string.Empty,
                            false,
                            _help.CreateCommandUsageEmbed(searchResult.Commands)
                        );
                    }
                }
                catch (HttpException hex) when(hex.WasCausedByDMsNotAccepted())
                {
                }
                finally
                {
                    await userDMChannel.CloseAsync();
                }

                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException();
            }
            }
        }