From 2c7986c4e6694d99b3b6915fba646f6a0003ab7b Mon Sep 17 00:00:00 2001 From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> Date: Tue, 28 Jun 2022 16:38:44 +0300 Subject: [PATCH] Refactor `IsTextChannel` and `IsVoiceChannel` to extensions --- .../Commands/ExportAllCommand.cs | 2 +- .../Commands/ExportDirectMessagesCommand.cs | 2 +- .../Commands/ExportGuildCommand.cs | 3 ++- .../Commands/GetChannelsCommand.cs | 3 ++- .../Commands/GetDirectMessageChannelsCommand.cs | 2 +- DiscordChatExporter.Core/Discord/Data/Channel.cs | 13 ++----------- .../Discord/Data/ChannelKind.cs | 13 +++++++++++++ .../Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs | 2 +- .../MarkdownVisitors/PlainTextMarkdownVisitor.cs | 3 ++- .../ViewModels/Components/DashboardViewModel.cs | 2 +- 10 files changed, 26 insertions(+), 19 deletions(-) diff --git a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs index 15a30be..c805a53 100644 --- a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs @@ -33,7 +33,7 @@ public class ExportAllCommand : ExportCommandBase await foreach (var channel in Discord.GetGuildChannelsAsync(guild.Id, cancellationToken)) { // Skip non-text channels - if (!channel.IsTextChannel) + if (!channel.Kind.IsText()) continue; channels.Add(channel); diff --git a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs index 6b91c01..a9022f2 100644 --- a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs @@ -19,7 +19,7 @@ public class ExportDirectMessagesCommand : ExportCommandBase await console.Output.WriteLineAsync("Fetching channels..."); var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken); - var textChannels = channels.Where(c => c.IsTextChannel).ToArray(); + var textChannels = channels.Where(c => c.Kind.IsText()).ToArray(); await base.ExecuteAsync(console, textChannels); } diff --git a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs index b00e93b..9778cc6 100644 --- a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs @@ -4,6 +4,7 @@ using CliFx.Attributes; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Core.Discord; +using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Cli.Commands; @@ -27,7 +28,7 @@ public class ExportGuildCommand : ExportCommandBase await console.Output.WriteLineAsync("Fetching channels..."); var channels = await Discord.GetGuildChannelsAsync(GuildId, cancellationToken); - var textChannels = channels.Where(c => c.IsTextChannel).ToArray(); + var textChannels = channels.Where(c => c.Kind.IsText()).ToArray(); await base.ExecuteAsync(console, textChannels); } diff --git a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs index bc53677..28e069e 100644 --- a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs @@ -5,6 +5,7 @@ using CliFx.Attributes; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Core.Discord; +using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Cli.Commands; @@ -27,7 +28,7 @@ public class GetChannelsCommand : TokenCommandBase var channels = await Discord.GetGuildChannelsAsync(GuildId, cancellationToken); var textChannels = channels - .Where(c => c.IsTextChannel) + .Where(c => c.Kind.IsText()) .OrderBy(c => c.Category.Position) .ThenBy(c => c.Name) .ToArray(); diff --git a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs index e50a825..e7bc31d 100644 --- a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs @@ -19,7 +19,7 @@ public class GetDirectMessageChannelsCommand : TokenCommandBase var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken); var textChannels = channels - .Where(c => c.IsTextChannel) + .Where(c => c.Kind.IsText()) .OrderBy(c => c.Category.Position) .ThenBy(c => c.Name) .ToArray(); diff --git a/DiscordChatExporter.Core/Discord/Data/Channel.cs b/DiscordChatExporter.Core/Discord/Data/Channel.cs index 218341a..8b3f7cf 100644 --- a/DiscordChatExporter.Core/Discord/Data/Channel.cs +++ b/DiscordChatExporter.Core/Discord/Data/Channel.cs @@ -14,17 +14,8 @@ public partial record Channel( ChannelCategory Category, string Name, int? Position, - string? Topic) : IHasId -{ - public bool IsTextChannel => Kind is - ChannelKind.GuildTextChat or - ChannelKind.DirectTextChat or - ChannelKind.DirectGroupTextChat or - ChannelKind.GuildNews or - ChannelKind.GuildStore; - - public bool IsVoiceChannel => !IsTextChannel; -} + string? Topic +) : IHasId; public partial record Channel { diff --git a/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs b/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs index 6862ca2..634a76c 100644 --- a/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs +++ b/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs @@ -11,4 +11,17 @@ public enum ChannelKind GuildCategory, GuildNews, GuildStore +} + +public static class ChannelKindExtensions +{ + public static bool IsText(this ChannelKind kind) => kind is + ChannelKind.GuildTextChat or + ChannelKind.DirectTextChat or + ChannelKind.DirectGroupTextChat or + ChannelKind.GuildNews or + ChannelKind.GuildStore; + + public static bool IsVoice(this ChannelKind kind) => kind is + ChannelKind.GuildVoiceChat; } \ No newline at end of file diff --git a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs index 020a976..4c8814f 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/HtmlMarkdownVisitor.cs @@ -158,7 +158,7 @@ internal partial class HtmlMarkdownVisitor : MarkdownVisitor else if (mention.Kind == MentionKind.Channel) { var channel = mention.TargetId?.Pipe(_context.TryGetChannel); - var symbol = channel?.IsVoiceChannel == true ? "🔊" : "#"; + var symbol = channel?.Kind.IsVoice() == true ? "🔊" : "#"; var name = channel?.Name ?? "deleted-channel"; _buffer diff --git a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs index 842ba1a..e615dcd 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs @@ -1,4 +1,5 @@ using System.Text; +using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Markdown; using DiscordChatExporter.Core.Markdown.Parsing; using DiscordChatExporter.Core.Utils.Extensions; @@ -58,7 +59,7 @@ internal partial class PlainTextMarkdownVisitor : MarkdownVisitor _buffer.Append($"#{name}"); // Voice channel marker - if (channel?.IsVoiceChannel == true) + if (channel?.Kind.IsVoice() == true) _buffer.Append(" [voice]"); } else if (mention.Kind == MentionKind.Role) diff --git a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs index 884d594..b6dded1 100644 --- a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs @@ -105,7 +105,7 @@ public class DashboardViewModel : PropertyChangedBase await foreach (var guild in discord.GetUserGuildsAsync()) { var channels = await discord.GetGuildChannelsAsync(guild.Id); - guildChannelMap[guild] = channels.Where(c => c.IsTextChannel).ToArray(); + guildChannelMap[guild] = channels.Where(c => c.Kind.IsText()).ToArray(); } _discord = discord;