diff --git a/.docs/Using-the-CLI.md b/.docs/Using-the-CLI.md index 36de6c95b..591fc2c3f 100644 --- a/.docs/Using-the-CLI.md +++ b/.docs/Using-the-CLI.md @@ -23,84 +23,104 @@ Now we're ready to run the commands. Type the following command in your terminal of choice, then press ENTER to run it. This will list all available subcommands and options. ```console -./DiscordChatExporter.Cli +./dce ``` > **Note**: -> On Windows, if you're using the default Command Prompt (`cmd`), omit the leading `./` at the start of the command. +> On Windows, use `dce` instead of `./dce`. > **Docker** users, please refer to the [Docker usage instructions](Docker.md). ## CLI commands -| Command | Description | -| ----------- | ---------------------------------------------------- | -| export | Exports a channel | -| exportdm | Exports all direct message channels | -| exportguild | Exports all channels within the specified server | -| exportall | Exports all accessible channels | -| channels | Outputs the list of channels in the given server | -| dm | Outputs the list of direct message channels | -| guilds | Outputs the list of accessible servers | -| guide | Explains how to obtain token, server, and channel ID | +| Command | Description | +| ----------------- | -------------------------------------------------------------------- | +| export | Exports one or more channels | +| list channels | Outputs the list of channels in the given server(s) | +| list channels dm | Outputs the list of direct message channels | +| list servers | Outputs the list of accessible servers | +| list unwrap | Resolves categories and forums in a channel list to their child channels and threads | +| guide | Explains how to obtain token, server, and channel ID | -To use the commands, you'll need a token. For the instructions on how to get a token, please refer to [this page](Token-and-IDs.md), or run `./DiscordChatExporter.Cli guide`. +To use the commands, you'll need a token. For the instructions on how to get a token, please refer to [this page](Token-and-IDs.md), or run `./dce guide`. + +To pass the token, use the `-t|--token` option: + +```console +./dce export 53555 -t "mfa.Ifrn" +``` + +Alternatively, you can set the `DISCORD_TOKEN` environment variable and omit `-t`: + +**Linux/macOS:** + +```console +export DISCORD_TOKEN="mfa.Ifrn" +``` + +**Windows:** + +```console +set DISCORD_TOKEN=mfa.Ifrn +``` + +The pipeline examples in this guide assume `DISCORD_TOKEN` is already set. To get help with a specific command, run: ```console -./DiscordChatExporter.Cli command --help +./dce command --help ``` For example, to figure out how to use the `export` command, run: ```console -./DiscordChatExporter.Cli export --help +./dce export --help ``` ## Export a specific channel -You can quickly export with DCE's default settings by using just `-t token` and `-c channelid`. +You can quickly export with DCE's default settings by providing the channel ID as a positional argument and `-t|--token`. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 +./dce export 53555 -t "mfa.Ifrn" ``` #### Changing the format -You can change the export format to `HtmlDark`, `HtmlLight`, `PlainText` `Json` or `Csv` with `-f format`. The default +You can change the export format to `HtmlDark`, `HtmlLight`, `PlainText` `Json` or `Csv` with `-f|--format`. The default format is `HtmlDark`. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 -f Json +./dce export 53555 -t "mfa.Ifrn" -f Json ``` #### Changing the output filename -You can change the filename by using `-o name.ext`. e.g. for the `HTML` format: +You can change the filename by using `-o|--output`. e.g. for the `HTML` format: ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 -o myserver.html +./dce export 53555 -t "mfa.Ifrn" -o myserver.html ``` #### Changing the output directory -You can change the export directory by using `-o` and providing a path that ends with a slash or does not have a file +You can change the export directory by using `-o|--output` and providing a path that ends with a slash or does not have a file extension. If any of the folders in the path have a space in its name, escape them with quotes ("). ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 -o "C:\Discord Exports" +./dce export 53555 -t "mfa.Ifrn" -o "C:\Discord Exports" ``` #### Changing the filename and output directory -You can change both the filename and export directory by using `-o directory\name.ext`. +You can change both the filename and export directory by using `-o|--output`. Note that the filename must have an extension, otherwise it will be considered a directory name. If any of the folders in the path have a space in its name, escape them with quotes ("). ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 -o "C:\Discord Exports\myserver.html" +./dce export 53555 -t "mfa.Ifrn" -o "C:\Discord Exports\myserver.html" ``` #### Generating the filename and output directory dynamically @@ -108,7 +128,7 @@ If any of the folders in the path have a space in its name, escape them with quo You can use template tokens to generate the output file path based on the server and channel metadata. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 -o "C:\Discord Exports\%G\%T\%C.html" +./dce export 53555 -t "mfa.Ifrn" -o "C:\Discord Exports\%G\%T\%C.html" ``` Assuming you are exporting a channel named `"my-channel"` in the `"Text channels"` category from a server @@ -136,13 +156,13 @@ You can use partitioning to split files after a given number of messages or file For example, a channel with 36 messages set to be partitioned every 10 messages will output 4 files. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 -p 10 +./dce export 53555 -t "mfa.Ifrn" -p 10 ``` A 45 MB channel set to be partitioned every 20 MB will output 3 files. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 -p 20mb +./dce export 53555 -t "mfa.Ifrn" -p 20mb ``` #### Downloading assets @@ -153,7 +173,7 @@ downloaded when using the plain text (TXT) export format. A folder containing the assets will be created along with the exported chat. They must be kept together. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 --media +./dce export 53555 -t "mfa.Ifrn" --media ``` #### Reusing assets @@ -162,7 +182,7 @@ Previously downloaded assets can be reused to skip redundant downloads as long a same folder. Using this option can speed up future exports. This option requires the `--media` option. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 --media --reuse-media +./dce export 53555 -t "mfa.Ifrn" --media --reuse-media ``` #### Changing the media directory @@ -171,7 +191,7 @@ By default, the media directory is created alongside the exported chat. You can providing a path that ends with a slash. All of the exported media will be stored in this directory. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 --media --media-dir "C:\Discord Media" +./dce export 53555 -t "mfa.Ifrn" --media --media-dir "C:\Discord Media" ``` #### Changing the date format @@ -180,7 +200,7 @@ You can customize how dates are formatted in the exported files by using `--loca locales. The default locale is `en-US`. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 --locale "de-DE" +./dce export 53555 -t "mfa.Ifrn" --locale "de-DE" ``` #### Date ranges @@ -189,14 +209,14 @@ locales. The default locale is `en-US`. Use `--before` to export messages sent before the provided date. E.g. messages sent before September 18th, 2019: ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 --before 2019-09-18 +./dce export 53555 -t "mfa.Ifrn" --before 2019-09-18 ``` **Messages sent after a date** Use `--after` to export messages sent after the provided date. E.g. messages sent after September 17th, 2019 11:34 PM: ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 --after "2019-09-17 23:34" +./dce export 53555 -t "mfa.Ifrn" --after "2019-09-17 23:34" ``` **Messages sent in a date range** @@ -204,7 +224,7 @@ Use `--before` and `--after` to export messages sent during the provided date ra September 17th, 2019 11:34 PM and September 18th: ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 --after "2019-09-17 23:34" --before "2019-09-18" +./dce export 53555 -t "mfa.Ifrn" --after "2019-09-17 23:34" --before "2019-09-18" ``` You can try different formats like `17-SEP-2019 11:34 PM` or even refine your ranges down to @@ -218,75 +238,111 @@ formats [here](https://docs.microsoft.com/en-us/dotnet/standard/base-types/custo Use `--filter` to filter what messages are included in the export. ```console -./DiscordChatExporter.Cli export -t "mfa.Ifrn" -c 53555 --filter "from:Tyrrrz has:image" +./dce export 53555 -t "mfa.Ifrn" --filter "from:Tyrrrz has:image" ``` Documentation on message filter syntax can be found [here](https://github.com/Tyrrrz/DiscordChatExporter/blob/prime/.docs/Message-filters.md). ### Export channels from a specific server -To export all channels in a specific server, use the `exportguild` command and provide the server ID through the `-g|--guild` option: +> [!IMPORTANT] +> The following examples assume `DISCORD_TOKEN` is already set. See [CLI commands](#cli-commands) for instructions. + +To export all channels in a specific server, use `list channels` to list channels and pipe the result to `export`. + +**Linux/macOS:** + +```console +./dce list channels 21814 | ./dce export +``` + +**Windows:** ```console -./DiscordChatExporter.Cli exportguild -t "mfa.Ifrn" -g 21814 +dce list channels 21814 | dce export +``` + +You can also list channels for multiple servers at once: + +```console +./dce list channels 21814 35930 | ./dce export ``` #### Including threads -By default, threads are not included in the export. You can change this behavior by using `--include-threads` and -specifying which threads should be included. It has possible values of `none`, `active`, or `all`, indicating which -threads should be included. To include both active and archived threads, use `--include-threads all`. +By default, threads are not included. You can change this behavior by passing `--include-threads` to the `list channels` command. It has possible values of `none`, `active`, or `all`, indicating which threads should be included. To include both active and archived threads, use `--include-threads all`. ```console -./DiscordChatExporter.Cli exportguild -t "mfa.Ifrn" -g 21814 --include-threads all +./dce list channels 21814 --include-threads all | ./dce export ``` #### Including voice channels -By default, voice channels are included in the export. You can change this behavior by using `--include-vc` and -specifying whether to include voice channels in the export. It has possible values of `true` or `false`, to exclude -voice channels, use `--include-vc false`. +By default, voice channels are included. You can change this behavior by passing `--include-vc false` to the `list channels` command. ```console -./DiscordChatExporter.Cli exportguild -t "mfa.Ifrn" -g 21814 --include-vc false +./dce list channels 21814 --include-vc false | ./dce export ``` -### Export all channels +### Export all DMs + +> [!IMPORTANT] +> The following examples assume `DISCORD_TOKEN` is already set. See [CLI commands](#cli-commands) for instructions. -To export all accessible channels, use the `exportall` command: +To export all DMs: + +**Linux/macOS:** ```console -./DiscordChatExporter.Cli exportall -t "mfa.Ifrn" +./dce list channels dm | ./dce export ``` -#### Excluding DMs - -To exclude DMs, add the `--include-dm false` option. +**Windows:** ```console -./DiscordChatExporter.Cli exportall -t "mfa.Ifrn" --include-dm false +dce list channels dm | dce export ``` ### List channels in a server -To list the channels available in a specific server, use the `channels` command and provide the server ID through the `-g|--guild` option: +To list the channels available in a specific server, use the `list channels` command and provide the server ID as an argument: + +```console +./dce list channels 21814 -t "mfa.Ifrn" +``` + +The `list channels` command outputs a JSON array of channel objects. You can pipe this directly to the `export` command: ```console -./DiscordChatExporter.Cli channels -t "mfa.Ifrn" -g 21814 +./dce list channels 21814 | ./dce export ``` ### List direct message channels -To list all DM channels accessible to the current account, use the `dm` command: +To list all DM channels accessible to the current account, use the `list channels dm` command: + +```console +./dce list channels dm -t "mfa.Ifrn" +``` + +The `list channels dm` command outputs a JSON array of channel objects. You can pipe this directly to the `export` command: + +```console +./dce list channels dm | ./dce export +``` + +### Unwrap categories and forums + +To resolve category and forum channels in a list to their child channels and thread posts, use the `list unwrap` command: ```console -./DiscordChatExporter.Cli dm -t "mfa.Ifrn" +./dce list channels 21814 | ./dce list unwrap | ./dce export ``` ### List servers -To list all servers accessible by the current account, use the `guilds` command: +To list all servers accessible by the current account, use the `list servers` command: ```console -./DiscordChatExporter.Cli guilds -t "mfa.Ifrn" > C:\path\to\output.txt +./dce list servers -t "mfa.Ifrn" > C:\path\to\output.txt ``` diff --git a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs b/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs deleted file mode 100644 index 464ecf0a8..000000000 --- a/DiscordChatExporter.Cli/Commands/ExportAllCommand.cs +++ /dev/null @@ -1,156 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using CliFx.Binding; -using CliFx.Infrastructure; -using DiscordChatExporter.Cli.Commands.Base; -using DiscordChatExporter.Cli.Utils.Extensions; -using DiscordChatExporter.Core.Discord.Data; -using DiscordChatExporter.Core.Discord.Dump; -using DiscordChatExporter.Core.Exceptions; -using Spectre.Console; - -namespace DiscordChatExporter.Cli.Commands; - -[Command("exportall", Description = "Exports all accessible channels.")] -public partial class ExportAllCommand : ExportCommandBase -{ - [CommandOption("include-dm", Description = "Include direct message channels.")] - public bool IncludeDirectChannels { get; set; } = true; - - [CommandOption("include-guilds", Description = "Include server channels.")] - public bool IncludeGuildChannels { get; set; } = true; - - [CommandOption("include-vc", Description = "Include voice channels.")] - public bool IncludeVoiceChannels { get; set; } = true; - - [CommandOption( - "data-package", - Description = "Path to the personal data package (ZIP file) requested from Discord. " - + "If provided, only channels referenced in the dump will be exported." - )] - public string? DataPackageFilePath { get; set; } - - public override async ValueTask ExecuteAsync(IConsole console) - { - await base.ExecuteAsync(console); - - var cancellationToken = console.RegisterCancellationHandler(); - var channels = new List(); - - // Pull from the API - if (string.IsNullOrWhiteSpace(DataPackageFilePath)) - { - await foreach (var guild in Discord.GetUserGuildsAsync(cancellationToken)) - { - // Regular channels - await console.Output.WriteLineAsync( - $"Fetching channels for server '{guild.Name}'..." - ); - - var fetchedChannelsCount = 0; - await console - .CreateStatusTicker() - .StartAsync( - "...", - async ctx => - { - await foreach ( - var channel in Discord.GetGuildChannelsAsync( - guild.Id, - cancellationToken - ) - ) - { - if (channel.IsCategory) - continue; - - if (!IncludeVoiceChannels && channel.IsVoice) - continue; - - channels.Add(channel); - - ctx.Status( - Markup.Escape($"Fetched '{channel.GetHierarchicalName()}'.") - ); - - fetchedChannelsCount++; - } - } - ); - - await console.Output.WriteLineAsync($"Fetched {fetchedChannelsCount} channel(s)."); - } - } - // Pull from the data package - else - { - await console.Output.WriteLineAsync("Extracting channels..."); - - var dump = await DataDump.LoadAsync(DataPackageFilePath, cancellationToken); - var inaccessibleChannels = new List(); - - await console - .CreateStatusTicker() - .StartAsync( - "...", - async ctx => - { - foreach (var dumpChannel in dump.Channels) - { - ctx.Status( - Markup.Escape( - $"Fetching '{dumpChannel.Name}' ({dumpChannel.Id})..." - ) - ); - - try - { - var channel = await Discord.GetChannelAsync( - dumpChannel.Id, - cancellationToken - ); - - channels.Add(channel); - } - catch (DiscordChatExporterException) - { - inaccessibleChannels.Add(dumpChannel); - } - } - } - ); - - await console.Output.WriteLineAsync($"Fetched {channels} channel(s)."); - - // Print inaccessible channels - if (inaccessibleChannels.Any()) - { - await console.Output.WriteLineAsync(); - - using (console.WithForegroundColor(ConsoleColor.Red)) - { - await console.Error.WriteLineAsync( - "Failed to access the following channel(s):" - ); - } - - foreach (var dumpChannel in inaccessibleChannels) - await console.Error.WriteLineAsync($"{dumpChannel.Name} ({dumpChannel.Id})"); - - await console.Error.WriteLineAsync(); - } - } - - // Filter out unwanted channels - if (!IncludeDirectChannels) - channels.RemoveAll(c => c.IsDirect); - if (!IncludeGuildChannels) - channels.RemoveAll(c => c.IsGuild); - if (!IncludeVoiceChannels) - channels.RemoveAll(c => c.IsVoice); - - await ExportAsync(console, channels); - } -} diff --git a/DiscordChatExporter.Cli/Commands/ExportChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/ExportChannelsCommand.cs index 3e963cade..ae2e45593 100644 --- a/DiscordChatExporter.Cli/Commands/ExportChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/ExportChannelsCommand.cs @@ -1,25 +1,27 @@ using System.Collections.Generic; +using System.Text.Json; using System.Threading.Tasks; +using CliFx; using CliFx.Binding; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; +using DiscordChatExporter.Cli.Utils.Extensions; using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord.Data; -using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Cli.Commands; [Command("export", Description = "Exports one or multiple channels.")] public partial class ExportChannelsCommand : ExportCommandBase { - // TODO: change this to plural (breaking change) - [CommandOption( - "channel", - 'c', + [CommandParameter( + 0, + Name = "channel-ids", Description = "Channel ID(s). " - + "If provided with category ID(s), all channels inside those categories will be exported." + + "If not provided, channel IDs are read from standard input (one per line or as a JSON array), " + + "enabling piping from the 'list channels' or 'list channels dm' commands." )] - public required IReadOnlyList ChannelIds { get; set; } + public IReadOnlyList ChannelIds { get; set; } = []; public override async ValueTask ExecuteAsync(IConsole console) { @@ -27,37 +29,48 @@ public override async ValueTask ExecuteAsync(IConsole console) var cancellationToken = console.RegisterCancellationHandler(); - await console.Output.WriteLineAsync("Resolving channel(s)..."); - - var channels = new List(); - var channelsByGuild = new Dictionary>(); - - foreach (var channelId in ChannelIds) + // If no channel IDs were specified, read them from stdin + var channelIds = new List(ChannelIds); + if (channelIds.Count == 0 && console.IsInputRedirected) { - var channel = await Discord.GetChannelAsync(channelId, cancellationToken); - - // Unwrap categories - if (channel.IsCategory) + await foreach (var line in console.Input.ReadLinesAsync(cancellationToken)) { - var guildChannels = - channelsByGuild.GetValueOrDefault(channel.GuildId) - ?? await Discord.GetGuildChannelsAsync(channel.GuildId, cancellationToken); + var trimmed = line.Trim(); + if (string.IsNullOrEmpty(trimmed)) + continue; - foreach (var guildChannel in guildChannels) + // Snowflake IDs are numeric; non-numeric input is treated as a JSON array + if (!char.IsAsciiDigit(trimmed[0])) { - if (guildChannel.Parent?.Id == channel.Id) - channels.Add(guildChannel); + using var doc = JsonDocument.Parse(trimmed); + foreach (var element in doc.RootElement.EnumerateArray()) + channelIds.Add(Snowflake.Parse(element.GetProperty("id").GetString()!)); + } + else + { + channelIds.Add(Snowflake.Parse(trimmed)); } - - // Cache the guild channels to avoid redundant work - channelsByGuild[channel.GuildId] = guildChannels; - } - else - { - channels.Add(channel); } } + if (channelIds.Count == 0) + { + throw new CommandException( + "No channel IDs provided. " + + "Specify channel IDs as arguments or pipe them from the 'list channels' or 'list channels dm' commands." + ); + } + + await console.Output.WriteLineAsync("Resolving channel(s)..."); + + var channels = new List(); + + foreach (var channelId in channelIds) + { + var channel = await Discord.GetChannelAsync(channelId, cancellationToken); + channels.Add(channel); + } + await ExportAsync(console, channels); } } diff --git a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs b/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs deleted file mode 100644 index 39f41f40e..000000000 --- a/DiscordChatExporter.Cli/Commands/ExportDirectMessagesCommand.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Threading.Tasks; -using CliFx.Binding; -using CliFx.Infrastructure; -using DiscordChatExporter.Cli.Commands.Base; -using DiscordChatExporter.Core.Discord.Data; -using DiscordChatExporter.Core.Utils.Extensions; - -namespace DiscordChatExporter.Cli.Commands; - -[Command("exportdm", Description = "Exports all direct message channels.")] -public partial class ExportDirectMessagesCommand : ExportCommandBase -{ - public override async ValueTask ExecuteAsync(IConsole console) - { - await base.ExecuteAsync(console); - - var cancellationToken = console.RegisterCancellationHandler(); - - await console.Output.WriteLineAsync("Fetching channels..."); - var channels = await Discord.GetGuildChannelsAsync( - Guild.DirectMessages.Id, - cancellationToken - ); - - await ExportAsync(console, channels); - } -} diff --git a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs b/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs deleted file mode 100644 index 037d8c58f..000000000 --- a/DiscordChatExporter.Cli/Commands/ExportGuildCommand.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using CliFx.Binding; -using CliFx.Infrastructure; -using DiscordChatExporter.Cli.Commands.Base; -using DiscordChatExporter.Cli.Utils.Extensions; -using DiscordChatExporter.Core.Discord; -using DiscordChatExporter.Core.Discord.Data; -using Spectre.Console; - -namespace DiscordChatExporter.Cli.Commands; - -[Command("exportguild", Description = "Exports all channels within the specified server.")] -public partial class ExportGuildCommand : ExportCommandBase -{ - [CommandOption("guild", 'g', Description = "Server ID.")] - public required Snowflake GuildId { get; set; } - - [CommandOption("include-vc", Description = "Include voice channels.")] - public bool IncludeVoiceChannels { get; set; } = true; - - public override async ValueTask ExecuteAsync(IConsole console) - { - await base.ExecuteAsync(console); - - var cancellationToken = console.RegisterCancellationHandler(); - var channels = new List(); - - await console.Output.WriteLineAsync("Fetching channels..."); - - var fetchedChannelsCount = 0; - await console - .CreateStatusTicker() - .StartAsync( - "...", - async ctx => - { - await foreach ( - var channel in Discord.GetGuildChannelsAsync(GuildId, cancellationToken) - ) - { - if (channel.IsCategory) - continue; - - if (!IncludeVoiceChannels && channel.IsVoice) - continue; - - channels.Add(channel); - - ctx.Status(Markup.Escape($"Fetched '{channel.GetHierarchicalName()}'.")); - - fetchedChannelsCount++; - } - } - ); - - await console.Output.WriteLineAsync($"Fetched {fetchedChannelsCount} channel(s)."); - - await ExportAsync(console, channels); - } -} diff --git a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs index b0116ed77..f0bd5e652 100644 --- a/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetChannelsCommand.cs @@ -1,21 +1,24 @@ -using System; +using System.Collections.Generic; using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using CliFx.Binding; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Converters; using DiscordChatExporter.Cli.Commands.Shared; +using DiscordChatExporter.Cli.Utils.Json; using DiscordChatExporter.Core.Discord; +using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Cli.Commands; -[Command("channels", Description = "Get the list of channels in a server.")] +[Command("list channels", Description = "Gets the list of channels in one or more servers.")] public partial class GetChannelsCommand : DiscordCommandBase { - [CommandOption("guild", 'g', Description = "Server ID.")] - public required Snowflake GuildId { get; set; } + [CommandParameter(0, Name = "server-ids", Description = "Server ID(s).")] + public required IReadOnlyList ServerIds { get; set; } [CommandOption("include-vc", Description = "Include voice channels.")] public bool IncludeVoiceChannels { get; set; } = true; @@ -33,82 +36,44 @@ public override async ValueTask ExecuteAsync(IConsole console) var cancellationToken = console.RegisterCancellationHandler(); - var channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken)) - .Where(c => !c.IsCategory) - .Where(c => IncludeVoiceChannels || !c.IsVoice) - .OrderBy(c => c.Parent?.Position) - .ThenBy(c => c.Name) - .ToArray(); + var allChannels = new List(); - var channelIdMaxLength = channels - .Select(c => c.Id.ToString().Length) - .OrderDescending() - .FirstOrDefault(); - - var threads = - ThreadInclusionMode != ThreadInclusionMode.None - ? ( - await Discord.GetGuildThreadsAsync( - GuildId, - ThreadInclusionMode == ThreadInclusionMode.All, - null, - null, - cancellationToken - ) - ) - .OrderBy(c => c.Name) - .ToArray() - : []; - - foreach (var channel in channels) + foreach (var serverId in ServerIds) { - // Channel ID - await console.Output.WriteAsync( - channel.Id.ToString().PadRight(channelIdMaxLength, ' ') - ); - - // Separator - using (console.WithForegroundColor(ConsoleColor.DarkGray)) - await console.Output.WriteAsync(" | "); - - // Channel name - using (console.WithForegroundColor(ConsoleColor.White)) - await console.Output.WriteLineAsync(channel.GetHierarchicalName()); - - var channelThreads = threads.Where(t => t.Parent?.Id == channel.Id).ToArray(); - var channelThreadIdMaxLength = channelThreads - .Select(t => t.Id.ToString().Length) - .OrderDescending() - .FirstOrDefault(); + var channels = (await Discord.GetGuildChannelsAsync(serverId, cancellationToken)) + .Where(c => !c.IsCategory) + .Where(c => IncludeVoiceChannels || !c.IsVoice) + .OrderBy(c => c.Parent?.Position) + .ThenBy(c => c.Name) + .ToArray(); + + var threads = + ThreadInclusionMode != ThreadInclusionMode.None + ? ( + await Discord.GetGuildThreadsAsync( + serverId, + ThreadInclusionMode == ThreadInclusionMode.All, + null, + null, + cancellationToken + ) + ) + .OrderBy(c => c.Name) + .ToArray() + : []; - foreach (var channelThread in channelThreads) + foreach (var channel in channels) { - // Indent - await console.Output.WriteAsync(" * "); - - // Thread ID - await console.Output.WriteAsync( - channelThread.Id.ToString().PadRight(channelThreadIdMaxLength, ' ') - ); - - // Separator - using (console.WithForegroundColor(ConsoleColor.DarkGray)) - await console.Output.WriteAsync(" | "); - - // Thread name - using (console.WithForegroundColor(ConsoleColor.White)) - await console.Output.WriteAsync($"Thread / {channelThread.Name}"); - - // Separator - using (console.WithForegroundColor(ConsoleColor.DarkGray)) - await console.Output.WriteAsync(" | "); - - // Thread status - using (console.WithForegroundColor(ConsoleColor.White)) - await console.Output.WriteLineAsync( - channelThread.IsArchived ? "Archived" : "Active" - ); + allChannels.Add(channel); + allChannels.AddRange(threads.Where(t => t.Parent?.Id == channel.Id)); } } + + await console.Output.WriteLineAsync( + JsonSerializer.Serialize( + allChannels.ToArray(), + CliJsonSerializerContext.Instance.ChannelArray + ) + ); } } diff --git a/DiscordChatExporter.Cli/Commands/GetDirectChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetDirectChannelsCommand.cs index 06ea69b14..95b38a195 100644 --- a/DiscordChatExporter.Cli/Commands/GetDirectChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetDirectChannelsCommand.cs @@ -1,15 +1,16 @@ -using System; -using System.Linq; +using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using CliFx.Binding; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; +using DiscordChatExporter.Cli.Utils.Json; using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Cli.Commands; -[Command("dm", Description = "Gets the list of all direct message channels.")] +[Command("list channels dm", Description = "Gets the list of direct message channels.")] public partial class GetDirectChannelsCommand : DiscordCommandBase { public override async ValueTask ExecuteAsync(IConsole console) @@ -25,25 +26,8 @@ await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken) .ThenBy(c => c.Name) .ToArray(); - var channelIdMaxLength = channels - .Select(c => c.Id.ToString().Length) - .OrderDescending() - .FirstOrDefault(); - - foreach (var channel in channels) - { - // Channel ID - await console.Output.WriteAsync( - channel.Id.ToString().PadRight(channelIdMaxLength, ' ') - ); - - // Separator - using (console.WithForegroundColor(ConsoleColor.DarkGray)) - await console.Output.WriteAsync(" | "); - - // Channel name - using (console.WithForegroundColor(ConsoleColor.White)) - await console.Output.WriteLineAsync(channel.GetHierarchicalName()); - } + await console.Output.WriteLineAsync( + JsonSerializer.Serialize(channels, CliJsonSerializerContext.Instance.ChannelArray) + ); } } diff --git a/DiscordChatExporter.Cli/Commands/GetGuildsCommand.cs b/DiscordChatExporter.Cli/Commands/GetGuildsCommand.cs index 6d8b5410e..9b77cfd72 100644 --- a/DiscordChatExporter.Cli/Commands/GetGuildsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetGuildsCommand.cs @@ -1,15 +1,16 @@ -using System; -using System.Linq; +using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using CliFx.Binding; using CliFx.Infrastructure; using DiscordChatExporter.Cli.Commands.Base; +using DiscordChatExporter.Cli.Utils.Json; using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Cli.Commands; -[Command("guilds", Description = "Gets the list of accessible servers.")] +[Command("list servers", Description = "Gets the list of accessible servers.")] public partial class GetGuildsCommand : DiscordCommandBase { public override async ValueTask ExecuteAsync(IConsole console) @@ -24,23 +25,8 @@ public override async ValueTask ExecuteAsync(IConsole console) .ThenBy(g => g.Name) .ToArray(); - var guildIdMaxLength = guilds - .Select(g => g.Id.ToString().Length) - .OrderDescending() - .FirstOrDefault(); - - foreach (var guild in guilds) - { - // Guild ID - await console.Output.WriteAsync(guild.Id.ToString().PadRight(guildIdMaxLength, ' ')); - - // Separator - using (console.WithForegroundColor(ConsoleColor.DarkGray)) - await console.Output.WriteAsync(" | "); - - // Guild name - using (console.WithForegroundColor(ConsoleColor.White)) - await console.Output.WriteLineAsync(guild.Name); - } + await console.Output.WriteLineAsync( + JsonSerializer.Serialize(guilds, CliJsonSerializerContext.Instance.GuildArray) + ); } } diff --git a/DiscordChatExporter.Cli/Commands/ListCommand.cs b/DiscordChatExporter.Cli/Commands/ListCommand.cs new file mode 100644 index 000000000..719084a48 --- /dev/null +++ b/DiscordChatExporter.Cli/Commands/ListCommand.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using CliFx; +using CliFx.Binding; +using CliFx.Infrastructure; + +namespace DiscordChatExporter.Cli.Commands; + +[Command("list", Description = "Lists channels, DMs, or servers.")] +public partial class ListCommand : ICommand +{ + public ValueTask ExecuteAsync(IConsole console) => + throw new CommandException("Use one of the named commands listed below.", showHelp: true); +} diff --git a/DiscordChatExporter.Cli/Commands/UnwrapChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/UnwrapChannelsCommand.cs new file mode 100644 index 000000000..7a71621fd --- /dev/null +++ b/DiscordChatExporter.Cli/Commands/UnwrapChannelsCommand.cs @@ -0,0 +1,95 @@ +using System.Collections.Generic; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using CliFx; +using CliFx.Binding; +using CliFx.Infrastructure; +using DiscordChatExporter.Cli.Commands.Base; +using DiscordChatExporter.Cli.Utils.Extensions; +using DiscordChatExporter.Cli.Utils.Json; +using DiscordChatExporter.Core.Discord; +using DiscordChatExporter.Core.Discord.Data; +using DiscordChatExporter.Core.Utils.Extensions; + +namespace DiscordChatExporter.Cli.Commands; + +[Command( + "list unwrap", + Description = "Resolves categories and forums in a channel list to their child channels and threads." +)] +public partial class UnwrapChannelsCommand : DiscordCommandBase +{ + public override async ValueTask ExecuteAsync(IConsole console) + { + await base.ExecuteAsync(console); + + var cancellationToken = console.RegisterCancellationHandler(); + + // Read all JSON from stdin (produced by 'list channels' or 'list channels dm') + var sb = new StringBuilder(); + await foreach (var line in console.Input.ReadLinesAsync(cancellationToken)) + sb.Append(line); + + Channel[] channels; + try + { + channels = + JsonSerializer.Deserialize( + sb.ToString().Trim(), + CliJsonSerializerContext.Instance.ChannelArray + ) ?? []; + } + catch (JsonException) + { + throw new CommandException( + "Failed to parse input as a JSON channel array. " + + "Pipe the output of 'list channels' or 'list channels dm' to this command." + ); + } + + var result = new List(); + var channelsByGuild = new Dictionary>(); + + foreach (var channel in channels) + { + if (channel.IsCategory) + { + // Expand category to its child channels + var guildChannels = + channelsByGuild.GetValueOrDefault(channel.GuildId) + ?? await Discord.GetGuildChannelsAsync(channel.GuildId, cancellationToken); + + foreach (var guildChannel in guildChannels) + { + if (guildChannel.Parent?.Id == channel.Id) + result.Add(guildChannel); + } + + channelsByGuild[channel.GuildId] = guildChannels; + } + else if (channel.Kind == ChannelKind.GuildForum) + { + // Expand forum to its thread posts + await foreach ( + var thread in Discord.GetChannelThreadsAsync( + [channel], + cancellationToken: cancellationToken + ) + ) + result.Add(thread); + } + else + { + result.Add(channel); + } + } + + await console.Output.WriteLineAsync( + JsonSerializer.Serialize( + result.ToArray(), + CliJsonSerializerContext.Instance.ChannelArray + ) + ); + } +} diff --git a/DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj b/DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj index 9e4b02c88..f0358085c 100644 --- a/DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj +++ b/DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj @@ -17,4 +17,17 @@ + + + + + diff --git a/DiscordChatExporter.Cli/Utils/Extensions/ConsoleExtensions.cs b/DiscordChatExporter.Cli/Utils/Extensions/ConsoleExtensions.cs index d7d47c8c1..2cc75c8da 100644 --- a/DiscordChatExporter.Cli/Utils/Extensions/ConsoleExtensions.cs +++ b/DiscordChatExporter.Cli/Utils/Extensions/ConsoleExtensions.cs @@ -1,4 +1,8 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using CliFx.Infrastructure; using Spectre.Console; @@ -61,4 +65,15 @@ Func performOperationAsync progressTask.StopTask(); } } + + public static async IAsyncEnumerable ReadLinesAsync( + this TextReader reader, + [EnumeratorCancellation] CancellationToken cancellationToken = default + ) + { + while (await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false) is { } line) + { + yield return line; + } + } } diff --git a/DiscordChatExporter.Cli/Utils/Json/CliJsonSerializerContext.cs b/DiscordChatExporter.Cli/Utils/Json/CliJsonSerializerContext.cs new file mode 100644 index 000000000..ac8cf31e1 --- /dev/null +++ b/DiscordChatExporter.Cli/Utils/Json/CliJsonSerializerContext.cs @@ -0,0 +1,26 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using DiscordChatExporter.Core.Discord.Data; + +namespace DiscordChatExporter.Cli.Utils.Json; + +[JsonSourceGenerationOptions( + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + GenerationMode = JsonSourceGenerationMode.Metadata +)] +[JsonSerializable(typeof(Channel[]))] +[JsonSerializable(typeof(Guild[]))] +internal partial class CliJsonSerializerContext : JsonSerializerContext +{ + // Instance pre-configured with converters for Snowflake (serialised as a string) + // and all enum types (serialised as their name). Defined here so the Core types + // are never touched. + public static CliJsonSerializerContext Instance { get; } = + new( + new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + Converters = { new SnowflakeJsonConverter(), new JsonStringEnumConverter() }, + } + ); +} diff --git a/DiscordChatExporter.Cli/Utils/Json/SnowflakeJsonConverter.cs b/DiscordChatExporter.Cli/Utils/Json/SnowflakeJsonConverter.cs new file mode 100644 index 000000000..032012b5d --- /dev/null +++ b/DiscordChatExporter.Cli/Utils/Json/SnowflakeJsonConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; +using DiscordChatExporter.Core.Discord; + +namespace DiscordChatExporter.Cli.Utils.Json; + +internal class SnowflakeJsonConverter : JsonConverter +{ + public override Snowflake Read( + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options + ) => Snowflake.Parse(reader.GetString()!); + + public override void Write( + Utf8JsonWriter writer, + Snowflake value, + JsonSerializerOptions options + ) => writer.WriteStringValue(value.ToString()); +} diff --git a/DiscordChatExporter.Cli/dce b/DiscordChatExporter.Cli/dce new file mode 100644 index 000000000..4665f18ec --- /dev/null +++ b/DiscordChatExporter.Cli/dce @@ -0,0 +1,2 @@ +#!/usr/bin/env sh +exec "$(dirname "$0")/DiscordChatExporter.Cli" "$@" diff --git a/DiscordChatExporter.Cli/dce.bat b/DiscordChatExporter.Cli/dce.bat new file mode 100644 index 000000000..0db4aa975 --- /dev/null +++ b/DiscordChatExporter.Cli/dce.bat @@ -0,0 +1,2 @@ +@echo off +"%~dp0DiscordChatExporter.Cli.exe" %*