From ae42554621eed897070ca4062acfd8d23fce3ff9 Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Sun, 18 Jul 2021 02:19:50 +0300 Subject: [PATCH] Render voice channel mentions Closes #636 --- DiscordChatExporter.Cli/Commands/ExportAllCommand.cs | 4 ++++ .../Commands/ExportDirectMessagesCommand.cs | 6 ++++-- DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs | 6 ++++-- DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs | 8 +++++++- .../Commands/GetDirectMessageChannelsCommand.cs | 8 +++++++- DiscordChatExporter.Core/Discord/Data/Channel.cs | 2 ++ DiscordChatExporter.Core/Discord/DiscordClient.cs | 4 ---- .../Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs | 3 ++- .../Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs | 4 ++++ .../Markdown/Parsing/MarkdownParser.cs | 2 +- DiscordChatExporter.Gui/ViewModels/RootViewModel.cs | 3 ++- 11 files changed, 37 insertions(+), 13 deletions(-) diff --git a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs index ed196da..7e81f3a 100644 --- a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs @@ -31,6 +31,10 @@ namespace DiscordChatExporter.Cli.Commands await foreach (var channel in Discord.GetGuildChannelsAsync(guild.Id)) { + // Skip non-text channels + if (!channel.IsTextChannel) + continue; + channels.Add(channel); } } diff --git a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs index b2db56a..4b1fee5 100644 --- a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; @@ -17,9 +18,10 @@ namespace DiscordChatExporter.Cli.Commands // Get channel metadata await console.Output.WriteLineAsync("Fetching channels..."); var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id); + var textChannels = channels.Where(c => c.IsTextChannel).ToArray(); // Export - await ExportAsync(console, channels); + await ExportAsync(console, textChannels); } } } \ No newline at end of file diff --git a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs index fb6230d..8a4daf6 100644 --- a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; @@ -20,9 +21,10 @@ namespace DiscordChatExporter.Cli.Commands // Get channel metadata await console.Output.WriteLineAsync("Fetching channels..."); var channels = await Discord.GetGuildChannelsAsync(GuildId); + var textChannels = channels.Where(c => c.IsTextChannel).ToArray(); // Export - await ExportAsync(console, channels); + await ExportAsync(console, textChannels); } } } \ No newline at end of file diff --git a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs index c0db564..dc691c3 100644 --- a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs @@ -19,7 +19,13 @@ namespace DiscordChatExporter.Cli.Commands { var channels = await Discord.GetGuildChannelsAsync(GuildId); - foreach (var channel in channels.OrderBy(c => c.Category.Position).ThenBy(c => c.Name)) + var textChannels = channels + .Where(c => c.IsTextChannel) + .OrderBy(c => c.Category.Position) + .ThenBy(c => c.Name) + .ToArray(); + + foreach (var channel in textChannels) { // Channel ID await console.Output.WriteAsync(channel.Id.ToString()); diff --git a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs index e76dfac..4845be5 100644 --- a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs @@ -16,7 +16,13 @@ namespace DiscordChatExporter.Cli.Commands { var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id); - foreach (var channel in channels.OrderBy(c => c.Name)) + var textChannels = channels + .Where(c => c.IsTextChannel) + .OrderBy(c => c.Category.Position) + .ThenBy(c => c.Name) + .ToArray(); + + foreach (var channel in textChannels) { // Channel ID await console.Output.WriteAsync(channel.Id.ToString()); diff --git a/DiscordChatExporter.Core/Discord/Data/Channel.cs b/DiscordChatExporter.Core/Discord/Data/Channel.cs index d0cd592..bb75ac8 100644 --- a/DiscordChatExporter.Core/Discord/Data/Channel.cs +++ b/DiscordChatExporter.Core/Discord/Data/Channel.cs @@ -21,6 +21,8 @@ namespace DiscordChatExporter.Core.Discord.Data ChannelType.GuildNews or ChannelType.GuildStore; + public bool IsVoiceChannel => !IsTextChannel; + public Snowflake GuildId { get; } public ChannelCategory Category { get; } diff --git a/DiscordChatExporter.Core/Discord/DiscordClient.cs b/DiscordChatExporter.Core/Discord/DiscordClient.cs index 3a09070..7fbb11f 100644 --- a/DiscordChatExporter.Core/Discord/DiscordClient.cs +++ b/DiscordChatExporter.Core/Discord/DiscordClient.cs @@ -142,10 +142,6 @@ namespace DiscordChatExporter.Core.Discord var channel = Channel.Parse(channelJson, category, position); - // We are only interested in text channels - if (!channel.IsTextChannel) - continue; - position++; yield return channel; diff --git a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs index 4b565d4..973c778 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs @@ -99,11 +99,12 @@ namespace DiscordChatExporter.Core.Exporting.Writers.MarkdownVisitors else if (mention.Kind == MentionKind.Channel) { var channel = mentionId?.Pipe(_context.TryGetChannel); + var symbol = channel?.IsVoiceChannel == true ? "🔊" : "#"; var name = channel?.Name ?? "deleted-channel"; _buffer .Append("") - .Append("#").Append(HtmlEncode(name)) + .Append(symbol).Append(HtmlEncode(name)) .Append(""); } else if (mention.Kind == MentionKind.Role) diff --git a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs index f5aa61e..a4f6cfc 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs @@ -43,6 +43,10 @@ namespace DiscordChatExporter.Core.Exporting.Writers.MarkdownVisitors var name = channel?.Name ?? "deleted-channel"; _buffer.Append($"#{name}"); + + // Voice channel marker + if (channel?.IsVoiceChannel == true) + _buffer.Append(" [voice]"); } else if (mention.Kind == MentionKind.Role) { diff --git a/DiscordChatExporter.Core/Markdown/Parsing/MarkdownParser.cs b/DiscordChatExporter.Core/Markdown/Parsing/MarkdownParser.cs index ca8cd4d..aa6a4ff 100644 --- a/DiscordChatExporter.Core/Markdown/Parsing/MarkdownParser.cs +++ b/DiscordChatExporter.Core/Markdown/Parsing/MarkdownParser.cs @@ -135,7 +135,7 @@ namespace DiscordChatExporter.Core.Markdown.Parsing // Capture <#123456> private static readonly IMatcher ChannelMentionNodeMatcher = new RegexMatcher( - new Regex("<#(\\d+)>", DefaultRegexOptions), + new Regex("<#!?(\\d+)>", DefaultRegexOptions), (_, m) => new MentionNode(m.Groups[1].Value, MentionKind.Channel) ); diff --git a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs index 1f898e9..4dcf201 100644 --- a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs @@ -168,7 +168,8 @@ namespace DiscordChatExporter.Gui.ViewModels var guildChannelMap = new Dictionary>(); await foreach (var guild in discord.GetUserGuildsAsync()) { - guildChannelMap[guild] = await discord.GetGuildChannelsAsync(guild.Id); + var channels = await discord.GetGuildChannelsAsync(guild.Id); + guildChannelMap[guild] = channels.Where(c => c.IsTextChannel).ToArray(); } GuildChannelMap = guildChannelMap;