From 557b5be844b66cf92341b5c038885e840515639b Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Tue, 11 Jun 2019 22:39:17 +0300 Subject: [PATCH] [TXT] Render reactions Addresses part of #168 --- .../PlainTextChatLogRenderer.cs | 118 +++++++++++------- 1 file changed, 73 insertions(+), 45 deletions(-) diff --git a/DiscordChatExporter.Core.Rendering/PlainTextChatLogRenderer.cs b/DiscordChatExporter.Core.Rendering/PlainTextChatLogRenderer.cs index 1eb2449..d17f8f7 100644 --- a/DiscordChatExporter.Core.Rendering/PlainTextChatLogRenderer.cs +++ b/DiscordChatExporter.Core.Rendering/PlainTextChatLogRenderer.cs @@ -93,55 +93,88 @@ namespace DiscordChatExporter.Core.Rendering private string FormatMarkdown(string markdown) => FormatMarkdown(MarkdownParser.Parse(markdown)); - private async Task RenderAttachmentAsync(TextWriter writer, Attachment attachment) + private async Task RenderAttachmentsAsync(TextWriter writer, IReadOnlyList attachments) { - await writer.WriteLineAsync("{Attachment}"); - await writer.WriteLineAsync(attachment.Url); + if (attachments.Any()) + { + await writer.WriteLineAsync("{Attachments}"); + + foreach (var attachment in attachments) + await writer.WriteLineAsync(attachment.Url); + + await writer.WriteLineAsync(); + } } - private async Task RenderEmbedAsync(TextWriter writer, Embed embed) + private async Task RenderEmbedsAsync(TextWriter writer, IReadOnlyList embeds) { - await writer.WriteLineAsync("{Embed}"); + foreach (var embed in embeds) + { + await writer.WriteLineAsync("{Embed}"); - // Author name - if (!(embed.Author?.Name).IsNullOrWhiteSpace()) - await writer.WriteLineAsync(embed.Author?.Name); + // Author name + if (!(embed.Author?.Name).IsNullOrWhiteSpace()) + await writer.WriteLineAsync(embed.Author?.Name); - // URL - if (!embed.Url.IsNullOrWhiteSpace()) - await writer.WriteLineAsync(embed.Url); + // URL + if (!embed.Url.IsNullOrWhiteSpace()) + await writer.WriteLineAsync(embed.Url); - // Title - if (!embed.Title.IsNullOrWhiteSpace()) - await writer.WriteLineAsync(FormatMarkdown(embed.Title)); + // Title + if (!embed.Title.IsNullOrWhiteSpace()) + await writer.WriteLineAsync(FormatMarkdown(embed.Title)); - // Description - if (!embed.Description.IsNullOrWhiteSpace()) - await writer.WriteLineAsync(FormatMarkdown(embed.Description)); + // Description + if (!embed.Description.IsNullOrWhiteSpace()) + await writer.WriteLineAsync(FormatMarkdown(embed.Description)); - // Fields - foreach (var field in embed.Fields) - { - // Name - if (!field.Name.IsNullOrWhiteSpace()) - await writer.WriteLineAsync(field.Name); + // Fields + foreach (var field in embed.Fields) + { + // Name + if (!field.Name.IsNullOrWhiteSpace()) + await writer.WriteLineAsync(field.Name); - // Value - if (!field.Value.IsNullOrWhiteSpace()) - await writer.WriteLineAsync(field.Value); + // Value + if (!field.Value.IsNullOrWhiteSpace()) + await writer.WriteLineAsync(field.Value); + } + + // Thumbnail URL + if (!(embed.Thumbnail?.Url).IsNullOrWhiteSpace()) + await writer.WriteLineAsync(embed.Thumbnail?.Url); + + // Image URL + if (!(embed.Image?.Url).IsNullOrWhiteSpace()) + await writer.WriteLineAsync(embed.Image?.Url); + + // Footer text + if (!(embed.Footer?.Text).IsNullOrWhiteSpace()) + await writer.WriteLineAsync(embed.Footer?.Text); + + await writer.WriteLineAsync(); } + } - // Thumbnail URL - if (!(embed.Thumbnail?.Url).IsNullOrWhiteSpace()) - await writer.WriteLineAsync(embed.Thumbnail?.Url); + private async Task RenderReactionsAsync(TextWriter writer, IReadOnlyList reactions) + { + if (reactions.Any()) + { + await writer.WriteLineAsync("{Reactions}"); + + foreach (var reaction in reactions) + { + await writer.WriteAsync(reaction.Emoji.Name); + + if (reaction.Count > 1) + await writer.WriteAsync($" ({reaction.Count})"); - // Image URL - if (!(embed.Image?.Url).IsNullOrWhiteSpace()) - await writer.WriteLineAsync(embed.Image?.Url); + await writer.WriteAsync(" "); + } - // Footer text - if (!(embed.Footer?.Text).IsNullOrWhiteSpace()) - await writer.WriteLineAsync(embed.Footer?.Text); + await writer.WriteLineAsync(); + await writer.WriteLineAsync(); + } } private async Task RenderMessageAsync(TextWriter writer, Message message) @@ -156,18 +189,13 @@ namespace DiscordChatExporter.Core.Rendering await writer.WriteLineAsync(); // Attachments - foreach (var attachment in message.Attachments) - { - await RenderAttachmentAsync(writer, attachment); - await writer.WriteLineAsync(); - } + await RenderAttachmentsAsync(writer, message.Attachments); // Embeds - foreach (var embed in message.Embeds) - { - await RenderEmbedAsync(writer, embed); - await writer.WriteLineAsync(); - } + await RenderEmbedsAsync(writer, message.Embeds); + + // Reactions + await RenderReactionsAsync(writer, message.Reactions); } public async Task RenderAsync(TextWriter writer)