Exemplo n.º 1
0
        /// <summary>
        ///     Post-command execution handling.
        /// </summary>
        private async Task OnCommandExecutedAsync(Optional <CommandInfo> commandInfo, ICommandContext context, IResult result)
        {
            // Bails if the generic result doesn't have an error reason, or if it's an unknown command error.
            if (string.IsNullOrEmpty(result.ErrorReason) || result.Error == CommandError.UnknownCommand)
            {
                return;
            }
            var embed = new EmbedBuilder();

            switch (result)
            {
            case CommandRuntimeResult customResult:
                switch (customResult.Type)
                {
                case ResultType.Unknown:
                    break;

                case ResultType.Info:
                    embed = EmbedHelper.FromInfo(description: customResult.Reason);
                    break;

                case ResultType.Warning:
                    embed = EmbedHelper.FromWarning(description: customResult.Reason);
                    break;

                case ResultType.Error:
                    embed = EmbedHelper.FromError(description: customResult.Reason);
                    break;

                case ResultType.Success:
                    embed = EmbedHelper.FromSuccess(description: customResult.Reason);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                break;

            default:
                embed = EmbedHelper.FromError(description: result.ErrorReason);
                break;
            }
            await context.Channel.SendMessageAsync("", embed : embed.Build()).ConfigureAwait(false);

            if (commandInfo.IsSpecified)
            {
                _loggingService.Log(
                    $"{context.User} executed \"{commandInfo.Value.Aliases.FirstOrDefault()}\" in {context.Message.GetPostedAt()}." +
                    Environment.NewLine +
                    $"Result: {result.ErrorReason}" +
                    Environment.NewLine +
                    $"Result Type: {result.GetType().Name}",
                    LogSeverity.Verbose);
            }
        }
Exemplo n.º 2
0
        private async Task HandlePostCommandExecutionAsync(Optional <CommandInfo> commandInfo, ICommandContext context,
                                                           IResult result)
        {
            if (result.Error == CommandError.UnknownCommand)
            {
                return;
            }
            if (!string.IsNullOrEmpty(result.ErrorReason))
            {
                Embed embed;
                switch (result)
                {
                case CommonResult commonResult:
                    switch (commonResult.CommonResultType)
                    {
                    default:
                        embed = EmbedHelper.FromInfo(description: commonResult.Reason);
                        break;

                    case CommonResultType.Warning:
                        embed = EmbedHelper.FromWarning(description: commonResult.Reason);
                        break;

                    case CommonResultType.Error:
                        embed = EmbedHelper.FromError(description: commonResult.Reason);
                        break;

                    case CommonResultType.Success:
                        embed = EmbedHelper.FromSuccess(description: commonResult.Reason);
                        break;
                    }

                    break;

                default:
                    embed = EmbedHelper.FromError(description: result.ErrorReason);
                    break;
                }

                var message = await context.Channel.SendMessageAsync("", embed : embed).ConfigureAwait(false);

                if (result.Error.HasValue)
                {
                    _ = Task.Delay(TimeSpan.FromSeconds(5))
                        .ContinueWith(_ => message?.DeleteAsync().ConfigureAwait(false));
                }
            }
        }
Exemplo n.º 3
0
        private Task LogCommandAsync(LogMessage message)
        {
            var sb = new StringBuilder().AppendLine(message.ToString(prependTimestamp: false));

            // Error messages for RunMode.Async commands
            if (message.Exception is CommandException command)
            {
                long errorId = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                sb.Append($"ID: {errorId}");
                var _ = command.Context.Channel.SendMessageAsync("", embed:
                                                                 EmbedHelper
                                                                 .FromError("Uh oh...",
                                                                            "We ran into a problem when executing this command!\n\nDon't worry, this error has been reported.")
                                                                 .AddField("Error ID", errorId, true)
                                                                 .WithThumbnailUrl("https://cdn.discordapp.com/emojis/288727789241237504.png").Build());
            }

            _commandsLogger.Log(GetLogLevel(message.Severity), 0,
                                message,
                                null,
                                (msg, ex) => sb.ToString());
            return(Task.CompletedTask);
        }