From d4f62387a56b712de5745fd3029a5169ee03cd2e Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Fri, 23 Jun 2023 21:23:55 +0300 Subject: [PATCH] Add CLI option to include or exclude voice channels in `exportall` and `exportguild` --- DiscordChatExporter.Cli/Commands/ExportAllCommand.cs | 8 ++++++++ DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs | 7 +++++++ DiscordChatExporter.Core/Discord/Data/Channel.cs | 3 ++- DiscordChatExporter.Core/Discord/Data/ChannelKind.cs | 3 +++ DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs | 2 +- .../Exporting/PlainTextMarkdownVisitor.cs | 3 ++- 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs index 96abb0e..a6525c9 100644 --- a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs @@ -28,6 +28,12 @@ public class ExportAllCommand : ExportCommandBase )] public bool IncludeGuildChannels { get; init; } = true; + [CommandOption( + "include-vc", + Description = "Include voice channels." + )] + public bool IncludeVoiceChannels { get; init; } = true; + [CommandOption( "data-package", Description = @@ -97,6 +103,8 @@ public class ExportAllCommand : ExportCommandBase channels.RemoveAll(c => c.Kind.IsDirect()); if (!IncludeGuildChannels) channels.RemoveAll(c => c.Kind.IsGuild()); + if (!IncludeVoiceChannels) + channels.RemoveAll(c => c.Kind.IsVoice()); await base.ExecuteAsync(console, channels); } diff --git a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs index b3dde50..ee021ba 100644 --- a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs @@ -19,6 +19,12 @@ public class ExportGuildCommand : ExportCommandBase )] public required Snowflake GuildId { get; init; } + [CommandOption( + "include-vc", + Description = "Include voice channels." + )] + public bool IncludeVoiceChannels { get; init; } = true; + public override async ValueTask ExecuteAsync(IConsole console) { await base.ExecuteAsync(console); @@ -29,6 +35,7 @@ public class ExportGuildCommand : ExportCommandBase var channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken)) .Where(c => c.Kind != ChannelKind.GuildCategory) + .Where(c => IncludeVoiceChannels || !c.Kind.IsVoice()) .ToArray(); await base.ExecuteAsync(console, channels); diff --git a/DiscordChatExporter.Core/Discord/Data/Channel.cs b/DiscordChatExporter.Core/Discord/Data/Channel.cs index 9121622..a9eb9af 100644 --- a/DiscordChatExporter.Core/Discord/Data/Channel.cs +++ b/DiscordChatExporter.Core/Discord/Data/Channel.cs @@ -18,7 +18,8 @@ public partial record Channel( string? Topic, Snowflake? LastMessageId) : IHasId { - public bool IsVoice => Kind is ChannelKind.GuildVoiceChat or ChannelKind.GuildStageVoice; + // Only needed for WPF data binding. Don't use anywhere else. + public bool IsVoice => Kind.IsVoice(); } public partial record Channel diff --git a/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs b/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs index 41e68b8..9bc4a0e 100644 --- a/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs +++ b/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs @@ -24,4 +24,7 @@ public static class ChannelKindExtensions public static bool IsGuild(this ChannelKind kind) => !kind.IsDirect(); + + public static bool IsVoice(this ChannelKind kind) => + kind is ChannelKind.GuildVoiceChat or ChannelKind.GuildStageVoice; } \ No newline at end of file diff --git a/DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs b/DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs index 437cfdf..0cb8b2a 100644 --- a/DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs @@ -261,7 +261,7 @@ internal partial class HtmlMarkdownVisitor : MarkdownVisitor else if (mention.Kind == MentionKind.Channel) { var channel = mention.TargetId?.Pipe(_context.TryGetChannel); - var symbol = channel?.IsVoice == true ? "🔊" : "#"; + var symbol = channel?.Kind.IsVoice() == true ? "🔊" : "#"; var name = channel?.Name ?? "deleted-channel"; _buffer.Append( diff --git a/DiscordChatExporter.Core/Exporting/PlainTextMarkdownVisitor.cs b/DiscordChatExporter.Core/Exporting/PlainTextMarkdownVisitor.cs index f357a29..6de2886 100644 --- a/DiscordChatExporter.Core/Exporting/PlainTextMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/PlainTextMarkdownVisitor.cs @@ -1,6 +1,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Markdown; using DiscordChatExporter.Core.Markdown.Parsing; using DiscordChatExporter.Core.Utils.Extensions; @@ -71,7 +72,7 @@ internal partial class PlainTextMarkdownVisitor : MarkdownVisitor _buffer.Append($"#{name}"); // Voice channel marker - if (channel?.IsVoice == true) + if (channel?.Kind.IsVoice() == true) _buffer.Append(" [voice]"); } else if (mention.Kind == MentionKind.Role)