From 91f4f02a351d15bbd7b94a4c2bffe2deb19abb40 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sat, 5 Sep 2020 17:05:18 +0300 Subject: [PATCH] Refactor last commit --- .../Commands/ExportAllCommand.cs | 33 ++++++++++--------- .../Commands/ExportDirectMessagesCommand.cs | 4 +-- .../Commands/ExportGuildCommand.cs | 4 +-- .../Commands/GetChannelsCommand.cs | 6 ++-- .../GetDirectMessageChannelsCommand.cs | 4 +-- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs index 8e79241..153565b 100644 --- a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs @@ -1,33 +1,36 @@ -using System.Linq; +using System.Collections.Generic; using System.Threading.Tasks; using CliFx; using CliFx.Attributes; using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Domain.Discord.Models; -using DiscordChatExporter.Domain.Utilities; namespace DiscordChatExporter.Cli.Commands { - [Command("exportall", Description = "Export all direct messages and all channels within all guilds.")] + [Command("exportall", Description = "Export all accessible channels.")] public class ExportAllCommand : ExportMultipleCommandBase { - [CommandOption("exclude-dm", 'e', Description = "If this flag is present, direct messages will not be exported.")] - public bool ExcludeDMs { get; set; } - + [CommandOption("include-dm", Description = "Whether to also export direct message channels.")] + public bool IncludeDirectMessages { get; set; } = true; + public override async ValueTask ExecuteAsync(IConsole console) { + var channels = new List(); - if(!ExcludeDMs){ - var dmChannels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id); - await ExportMultipleAsync(console, dmChannels); - } - - var guilds = await GetDiscordClient().GetUserGuildsAsync(); - foreach (var guild in guilds.OrderBy(g => g.Name)) + // Aggregate channels from all guilds + await foreach (var guild in GetDiscordClient().GetUserGuildsAsync()) { - var guildChannels = await GetDiscordClient().GetGuildChannelsAsync(guild.Id); - await ExportMultipleAsync(console, guildChannels); + // Skip DMs if instructed to + if (!IncludeDirectMessages && guild.Id == Guild.DirectMessages.Id) + continue; + + await foreach (var channel in GetDiscordClient().GetGuildChannelsAsync(guild.Id)) + { + channels.Add(channel); + } } + + await ExportMultipleAsync(console, channels); } } } diff --git a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs index a81d3c9..92f4b81 100644 --- a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs @@ -12,8 +12,8 @@ namespace DiscordChatExporter.Cli.Commands { public override async ValueTask ExecuteAsync(IConsole console) { - var dmChannels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id); - await ExportMultipleAsync(console, dmChannels); + var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id); + await ExportMultipleAsync(console, channels); } } } \ No newline at end of file diff --git a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs index a5fe430..9d2f228 100644 --- a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs @@ -14,8 +14,8 @@ namespace DiscordChatExporter.Cli.Commands public override async ValueTask ExecuteAsync(IConsole console) { - var guildChannels = await GetDiscordClient().GetGuildChannelsAsync(GuildId); - await ExportMultipleAsync(console, guildChannels); + var channels = await GetDiscordClient().GetGuildChannelsAsync(GuildId); + await ExportMultipleAsync(console, channels); } } } \ No newline at end of file diff --git a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs index d2ae0fc..b73326f 100644 --- a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs @@ -7,7 +7,7 @@ using DiscordChatExporter.Domain.Utilities; namespace DiscordChatExporter.Cli.Commands { - [Command("channels", Description = "Get the list of channels in specified guild.")] + [Command("channels", Description = "Get the list of channels in a guild.")] public class GetChannelsCommand : TokenCommandBase { [CommandOption("guild", 'g', IsRequired = true, Description = "Guild ID.")] @@ -15,9 +15,9 @@ namespace DiscordChatExporter.Cli.Commands public override async ValueTask ExecuteAsync(IConsole console) { - var guildChannels = await GetDiscordClient().GetGuildChannelsAsync(GuildId); + var channels = await GetDiscordClient().GetGuildChannelsAsync(GuildId); - foreach (var channel in guildChannels.OrderBy(c => c.Category).ThenBy(c => c.Name)) + foreach (var channel in channels.OrderBy(c => c.Category).ThenBy(c => c.Name)) console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}"); } } diff --git a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs index 817467e..f4b2b91 100644 --- a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs @@ -13,9 +13,9 @@ namespace DiscordChatExporter.Cli.Commands { public override async ValueTask ExecuteAsync(IConsole console) { - var dmChannels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id); + var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id); - foreach (var channel in dmChannels.OrderBy(c => c.Category).ThenBy(c => c.Name)) + foreach (var channel in channels.OrderBy(c => c.Category).ThenBy(c => c.Name)) console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}"); } }