From 94ef4b69817403a03cdda603f1592fed1f2d516f Mon Sep 17 00:00:00 2001 From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> Date: Tue, 28 Jun 2022 18:27:01 +0300 Subject: [PATCH] Support listing and exporting voice channels Closes #874 --- .../Commands/ExportAllCommand.cs | 6 ---- .../Commands/ExportDirectMessagesCommand.cs | 6 ++-- .../Commands/ExportGuildCommand.cs | 7 ++--- .../Commands/GetChannelsCommand.cs | 2 -- .../GetDirectMessageChannelsCommand.cs | 1 - .../Discord/Data/Channel.cs | 7 +++-- .../Discord/Data/ChannelKind.cs | 30 +++++++------------ .../MarkdownVisitors/HtmlMarkdownVisitor.cs | 2 +- .../PlainTextMarkdownVisitor.cs | 5 ++-- .../Services/SettingsService.cs | 3 +- .../Components/DashboardViewModel.cs | 5 +--- .../Views/Components/DashboardView.xaml | 16 ++++++++-- 12 files changed, 39 insertions(+), 51 deletions(-) diff --git a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs index c805a53..5000888 100644 --- a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs @@ -31,13 +31,7 @@ public class ExportAllCommand : ExportCommandBase continue; await foreach (var channel in Discord.GetGuildChannelsAsync(guild.Id, cancellationToken)) - { - // Skip non-text channels - if (!channel.Kind.IsText()) - continue; - channels.Add(channel); - } } await base.ExecuteAsync(console, channels); diff --git a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs index a9022f2..608adcb 100644 --- a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs @@ -1,5 +1,4 @@ -using System.Linq; -using System.Threading.Tasks; +using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; @@ -19,8 +18,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.Kind.IsText()).ToArray(); - await base.ExecuteAsync(console, textChannels); + await base.ExecuteAsync(console, channels); } } \ No newline at end of file diff --git a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs index 9778cc6..be9fc01 100644 --- a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs @@ -1,10 +1,8 @@ -using System.Linq; -using System.Threading.Tasks; +using System.Threading.Tasks; 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; @@ -28,8 +26,7 @@ public class ExportGuildCommand : ExportCommandBase await console.Output.WriteLineAsync("Fetching channels..."); var channels = await Discord.GetGuildChannelsAsync(GuildId, cancellationToken); - var textChannels = channels.Where(c => c.Kind.IsText()).ToArray(); - await base.ExecuteAsync(console, textChannels); + await base.ExecuteAsync(console, channels); } } \ No newline at end of file diff --git a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs index cfcef1a..1ff1d08 100644 --- a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs @@ -5,7 +5,6 @@ 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; @@ -26,7 +25,6 @@ public class GetChannelsCommand : TokenCommandBase var cancellationToken = console.RegisterCancellationHandler(); var channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken)) - .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 adaa647..03265d1 100644 --- a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs @@ -17,7 +17,6 @@ public class GetDirectMessageChannelsCommand : TokenCommandBase var cancellationToken = console.RegisterCancellationHandler(); var channels = (await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken)) - .Where(c => c.Kind.IsText()) .OrderByDescending(c => c.LastMessageId) .ThenBy(c => c.Name) .ToArray(); diff --git a/DiscordChatExporter.Core/Discord/Data/Channel.cs b/DiscordChatExporter.Core/Discord/Data/Channel.cs index b5862e0..c60b79d 100644 --- a/DiscordChatExporter.Core/Discord/Data/Channel.cs +++ b/DiscordChatExporter.Core/Discord/Data/Channel.cs @@ -15,8 +15,10 @@ public partial record Channel( string Name, int? Position, string? Topic, - Snowflake? LastMessageId -) : IHasId; + Snowflake? LastMessageId) : IHasId +{ + public bool SupportsVoice => Kind is ChannelKind.GuildVoiceChat or ChannelKind.GuildStageVoice; +} public partial record Channel { @@ -28,7 +30,6 @@ public partial record Channel ChannelKind.DirectTextChat => "Private", ChannelKind.DirectGroupTextChat => "Group", ChannelKind.GuildNews => "News", - ChannelKind.GuildStore => "Store", _ => "Default" }, null diff --git a/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs b/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs index 634a76c..35908e4 100644 --- a/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs +++ b/DiscordChatExporter.Core/Discord/Data/ChannelKind.cs @@ -5,23 +5,15 @@ public enum ChannelKind { GuildTextChat = 0, - DirectTextChat, - GuildVoiceChat, - DirectGroupTextChat, - 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; + DirectTextChat = 1, + GuildVoiceChat = 2, + DirectGroupTextChat = 3, + GuildCategory = 4, + GuildNews = 5, + GuildNewsThread = 10, + GuildPublicThread = 11, + GuildPrivateThread = 12, + GuildStageVoice = 13, + GuildDirectory = 14, + GuildForum = 15 } \ 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 4c8814f..ef14c08 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?.Kind.IsVoice() == true ? "🔊" : "#"; + var symbol = channel?.SupportsVoice == 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 e615dcd..c834dc9 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/Writers/MarkdownVisitors/PlainTextMarkdownVisitor.cs @@ -1,5 +1,4 @@ using System.Text; -using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Markdown; using DiscordChatExporter.Core.Markdown.Parsing; using DiscordChatExporter.Core.Utils.Extensions; @@ -53,13 +52,13 @@ internal partial class PlainTextMarkdownVisitor : MarkdownVisitor } else if (mention.Kind == MentionKind.Channel) { - var channel =mention.TargetId?.Pipe(_context.TryGetChannel); + var channel = mention.TargetId?.Pipe(_context.TryGetChannel); var name = channel?.Name ?? "deleted-channel"; _buffer.Append($"#{name}"); // Voice channel marker - if (channel?.Kind.IsVoice() == true) + if (channel?.SupportsVoice == true) _buffer.Append(" [voice]"); } else if (mention.Kind == MentionKind.Role) diff --git a/DiscordChatExporter.Gui/Services/SettingsService.cs b/DiscordChatExporter.Gui/Services/SettingsService.cs index cca7042..31a6bd6 100644 --- a/DiscordChatExporter.Gui/Services/SettingsService.cs +++ b/DiscordChatExporter.Gui/Services/SettingsService.cs @@ -45,7 +45,8 @@ public partial class SettingsService try { return Registry.CurrentUser.OpenSubKey( - "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", false + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", + false )?.GetValue("AppsUseLightTheme") is 0; } catch diff --git a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs index 8f6daf2..a0d058f 100644 --- a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs @@ -105,10 +105,7 @@ public class DashboardViewModel : PropertyChangedBase var guildChannelMap = new Dictionary>(); await foreach (var guild in discord.GetUserGuildsAsync()) - { - var channels = await discord.GetGuildChannelsAsync(guild.Id); - guildChannelMap[guild] = channels.Where(c => c.Kind.IsText()).ToArray(); - } + guildChannelMap[guild] = await discord.GetGuildChannelsAsync(guild.Id); _discord = discord; GuildChannelMap = guildChannelMap; diff --git a/DiscordChatExporter.Gui/Views/Components/DashboardView.xaml b/DiscordChatExporter.Gui/Views/Components/DashboardView.xaml index 774bee7..83b1175 100644 --- a/DiscordChatExporter.Gui/Views/Components/DashboardView.xaml +++ b/DiscordChatExporter.Gui/Views/Components/DashboardView.xaml @@ -348,8 +348,20 @@ + VerticalAlignment="Center"> + + + +