Пример #1
0
        /// <summary>
        /// Send the error that occured in <see cref="ExecuteAsync"/>.
        /// </summary>
        internal static async Task SendErrorAsync(
            IContext e,
            string key,
            object error,
            ICodeProvider codeProvider,
            object rangeObj = null)
        {
            var sb = new StringBuilder();

            switch (rangeObj)
            {
            case SourceRange range:
            {
                var code = await codeProvider.GetAsync();

                sb.AppendLine();
                sb.AppendLine();
                sb.Append(error);
                sb.Append(" at ");
                sb.Append(range.StartLine);
                sb.Append(':');
                sb.Append(range.StartColumn);
                sb.AppendLine();
                sb.AppendLine("```");
                sb.Append(code.GetPeek(range.Index, range.Length));
                sb.AppendLine("```");
                break;
            }

            case CompiledSourceRange range:
            {
                var code = await codeProvider.GetAsync();

                sb.AppendLine();
                sb.AppendLine();
                sb.Append(error);
                sb.AppendLine();
                sb.AppendLine("```");
                sb.Append(code.GetPeek(range.Index, range.Length));
                sb.AppendLine("```");
                break;
            }

            default:
                sb.AppendLine("```");
                sb.Append(error);
                sb.AppendLine();
                sb.AppendLine("```");
                break;
            }

            await e.ErrorEmbedResource(key)
            .AppendDescription(sb.ToString())
            .ToEmbed().QueueAsync(e, e.GetChannel());
        }