Compare commits

...

129 Commits

Author SHA1 Message Date
Oleksii Holub 16d44ce654 Update version 2023-09-15 13:02:35 +03:00
Tyrrrz 5abe74894c Remove Channel.ParentNameWithFallback 2023-09-07 16:36:29 +03:00
Oleksii Holub 59344cedbe Replace the date format option with a locale option (#1130) 2023-09-07 14:34:08 +03:00
Oleksii Holub 53b11d6c49 Update docks for Docker usage 2023-09-07 00:18:29 +03:00
Tyrrrz ccdf082689 Clean up formatting 2023-09-03 22:19:03 +03:00
Tyrrrz 9601e0acea Refactor 2023-09-03 21:46:20 +03:00
Adam Slatinský 9583e2684d Optimize fetching threads (#1125) 2023-09-03 21:18:49 +03:00
Tyrrrz d24550f521 Hide completed tasks in CLI when exporting in parallel
Closes #1124
2023-08-30 18:43:31 +03:00
Tyrrrz c422ac2351 Clean up 2023-08-30 18:43:12 +03:00
Tyrrrz 3740d64601 Optimize thread inclusion UX across GUI and CLI
Related to #1119
2023-08-28 22:08:51 +03:00
Tyrrrz d430f77ae1 Replace --include-threads and --include-archived-threads with a single multi-value option
Related to #1119
2023-08-28 21:52:56 +03:00
Tyrrrz 5f6e51f6fb Show threads in GUI at the bottom of the list, in specialized groups
Related to #1119
2023-08-28 21:31:51 +03:00
Tyrrrz f1c094ac14 Implicitly enable threads if archived threads are enabled in GUI 2023-08-28 21:09:42 +03:00
Tyrrrz 6fb6108610 Be safer when pulling threads using a bot token 2023-08-23 15:58:53 +03:00
Tyrrrz e75a1933f8 Bypass CSharpier in docker build 2023-08-22 21:36:50 +03:00
Tyrrrz 20f58963a6 Use CSharpier 2023-08-22 21:17:19 +03:00
Tyrrrz c410e745b1 Add rudimentary thread support in GUI
Related to #1119
2023-08-22 21:14:34 +03:00
Tyrrrz c29de2c77c Fetch threads more efficiently for user tokens 2023-08-22 19:16:32 +03:00
Tyrrrz 67d41fbdc4 Fix jitter in the GUI when switching from DM guild to any other and back 2023-08-22 17:41:36 +03:00
Tyrrrz 041086267c Short-circuit GetGuildThreadsAsync(...) for DMs 2023-08-22 00:10:04 +03:00
Tyrrrz 47588296a9 Use ValueTask for consistency 2023-08-22 00:08:45 +03:00
Tyrrrz d1a8f466f6 Pull channels lazily in the GUI
Closes #1005
2023-08-22 00:03:29 +03:00
Tyrrrz 758fb26dad Add --include-threads and --include-archived-threads to exportall
Related to #1119
2023-08-21 17:45:12 +03:00
Tyrrrz 8236ba7c4d Add defaults to inits 2023-08-21 17:35:10 +03:00
Tyrrrz eac974cfb1 Add --include-threads and --include-archived-threads to exportguild
Related to #1119
2023-08-21 17:33:22 +03:00
Tyrrrz d1d560fb55 Add --include-vc to the channels command 2023-08-21 17:24:05 +03:00
Tyrrrz 6fba60e570 Print archived threads only if explicitly requested in the channels command 2023-08-21 17:22:44 +03:00
Tyrrrz ac1bd16439 Clean up 2023-08-21 01:05:57 +03:00
Tyrrrz 60389ab002 Add warning about IsBotToken deprecation 2023-08-21 01:03:29 +03:00
Tyrrrz 817d54039b Improve naming of dictionaries 2023-08-20 16:48:39 +03:00
Tyrrrz 0ca42f5a14 Update issue forms 2023-08-18 01:40:01 +03:00
Tyrrrz 193cfd7b1e Update issue forms 2023-08-13 13:40:22 +03:00
Tyrrrz 21293dc7af Update bug report form 2023-08-12 21:55:27 +03:00
Tyrrrz 88dee76690 Update issue forms 2023-08-12 21:44:29 +03:00
Tyrrrz cd5afd07ac Update version 2023-08-11 00:06:15 +03:00
Tyrrrz 55270ad6d3 Switch to MIT license 2023-08-08 22:46:07 +03:00
Tyrrrz e82ad2b9c6 Add comment linking an issue that explains why the EscapedSymbolTextNodeMatcher is used 2023-08-05 18:35:22 +03:00
Tyrrrz dd0a35838d Allow empty lines in repeated single-line quote nodes
Closes #1115
2023-08-05 18:30:11 +03:00
Tyrrrz c9b68427f3 Update NuGet packages 2023-08-05 18:17:06 +03:00
Tyrrrz 1ba0057174 Clean up 2023-07-31 20:22:10 +03:00
Adam Slatinský d00ecf0c37 Export reaction users in JSON format (#1107) 2023-07-31 20:11:47 +03:00
Tyrrrz b224fca6c0 ToHex() should be uppercase by default 2023-07-30 14:54:15 +03:00
Tyrrrz c9447c5c8f Clean up 2023-07-29 20:53:00 +03:00
Tyrrrz de67cfae1c Use invariant culture 2023-07-29 20:47:43 +03:00
Tyrrrz 4efa6094dc Round up the "call lasted for X minutes" value 2023-07-29 20:47:36 +03:00
Tyrrrz 088612a78f Update version 2023-07-29 00:58:32 +03:00
Tyrrrz de531d4a97 Fix category not working in GUI
Closes #1112
2023-07-29 00:57:45 +03:00
Tyrrrz 5fc2fae28a Update version 2023-07-27 23:59:17 +03:00
Tyrrrz ddfbe51cfa Improve error reporting on unexpected HTTP status code 2023-07-25 18:59:05 +03:00
Tyrrrz bcf652edbe Don't throw when parent channel cannot be retrieved
Closes #1108
2023-07-23 16:04:53 +03:00
Tyrrrz 1f5407f032 Update version 2023-07-21 02:03:47 +03:00
Tyrrrz 83a63c2ed7 Update NuGet packages 2023-07-21 02:03:32 +03:00
Tyrrrz f33a0ac0a7 Update Onova 2023-07-18 22:12:26 +03:00
Tyrrrz 779b8984d7 Update readme 2023-07-17 00:47:30 +03:00
Tyrrrz 3e44a2e88d Re-add IHasId to Channel 2023-07-16 23:12:57 +03:00
Tyrrrz b44e6087be Update NuGet packages 2023-07-16 23:09:31 +03:00
Tyrrrz 49b96087d3 Clean up 2023-07-16 23:05:53 +03:00
Tyrrrz e175c93038 Refactor 2023-07-16 22:55:36 +03:00
Tyrrrz 0ee1107638 Make password box un-editable 2023-07-16 21:37:40 +03:00
Tyrrrz 81a6d363d1 Use favicon as avatar for Discord webhooks 2023-07-11 23:53:12 +03:00
Tyrrrz 90d71c5b9e Clean up 2023-07-10 20:40:32 +03:00
Adam Slatinský 5e9c7392db Add export timestamp in JSON format (#1102) 2023-07-09 18:22:09 +03:00
Tyrrrz c4137cf77e Hide token using password box 2023-07-09 18:13:41 +03:00
Tyrrrz 51192425b7 Expect channel parents to be null 2023-07-08 19:21:00 +03:00
William Unsworth 8776a6955b Better support for exporting threads (#1046) 2023-07-07 20:33:17 +03:00
Tyrrrz c69211797f Clean up 2023-07-03 18:35:28 +03:00
Adam Slatinský 768fb88c8c Export role details in JSON format (#1101) 2023-07-03 18:18:30 +03:00
Tyrrrz d4f62387a5 Add CLI option to include or exclude voice channels in exportall and exportguild 2023-06-23 21:23:55 +03:00
Tyrrrz bd4cfcdaf6 Update avatar resolving for users without discriminator 2023-06-21 22:05:08 +03:00
Tyrrrz c3abeadf01 Update version 2023-06-09 01:20:55 +03:00
Tyrrrz 86ee9c8d5f Fix deploy in workflow 2023-06-09 01:20:47 +03:00
Tyrrrz c99355dec3 Consistency in parsing 2023-06-09 01:20:39 +03:00
Tyrrrz 11d34109a7 Extract thread status and improve CLI output 2023-06-09 01:20:32 +03:00
Tyrrrz 609ca0fc0d Simplify guide for extracting channel IDs 2023-06-09 01:19:36 +03:00
Tyrrrz 6cadb19cc8 Update NuGet packages 2023-06-09 01:19:12 +03:00
Tyrrrz d02649773e Fix tests 2023-06-08 16:53:03 +03:00
Tyrrrz 18d9c189b7 Update docs with more information on message filter caveats in CLI
Closes #1045
2023-06-08 16:48:32 +03:00
Tyrrrz 3d2d197904 Introduce the foundation for the new username system 2023-06-08 16:37:05 +03:00
Tyrrrz bf0d8ab31e Rename markdown Header to Heading to avoid ambiguity and for consistency with MD/HTML standards 2023-06-02 13:59:39 +03:00
Tyrrrz 30b447d2ae Update NuGet packages 2023-05-31 22:15:24 +03:00
Tyrrrz 3d047321cf Parse YouTube video IDs natively 2023-05-31 22:14:58 +03:00
Tyrrrz 6d787c4071 Collect source information in tests 2023-05-30 01:31:46 +03:00
Tyrrrz d779ed9bc8 Blur the token in GUI when the control is out of focus 2023-05-29 19:01:49 +03:00
Tyrrrz 3319b4734d Use event data to reference repo info in workflows 2023-05-27 23:49:05 +03:00
Tyrrrz e052241713 Simplify workflows 2023-05-27 23:32:24 +03:00
Tyrrrz 2b7f1ed4ed Use granular permissions for GitHub Actions 2023-05-27 22:42:04 +03:00
Tyrrrz 03784f0d7c Update favicon 2023-05-26 20:40:45 +03:00
Tyrrrz e7310274ba Clean up tests 2023-05-25 18:51:49 +03:00
Tyrrrz 4eab781450 Update NuGet packages 2023-05-25 09:13:21 +03:00
Tyrrrz 6582e26cd3 Increase padding in output to accomodate longer snowflakes 2023-05-24 21:54:51 +03:00
Tyrrrz c48899e0b1 Refactor 2023-05-24 21:51:24 +03:00
William Unsworth 41b4e47d0e Use alternative endpoint to retrieve threads when requested by a bot (#1037)
Co-authored-by: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com>
2023-05-24 19:02:34 +03:00
Tyrrrz 6bbde4ccdc Add support for lists in markdown 2023-05-22 14:14:17 +03:00
Tyrrrz 469a731892 Add support for headers in markdown 2023-05-22 11:05:14 +03:00
Tyrrrz d8315c7827 Rename AsyncExtensions to AsyncCollectionExtensions 2023-05-22 10:08:38 +03:00
Tyrrrz a4111c184e Better test naming 2023-05-20 07:32:21 +03:00
Tyrrrz c945c98f75 Update NuGet package 2023-05-20 07:11:16 +03:00
Tyrrrz 31c7ae9312 Refactor 2023-05-20 07:09:19 +03:00
Tyrrrz 03c5c1bc5e Clean up 2023-05-19 14:49:31 +03:00
Tyrrrz 0ae9062a30 Fix typo and refactor 2023-05-19 02:13:19 +03:00
William Unsworth 25caf04445 Preliminary support for threads (#1032) 2023-05-19 01:46:37 +03:00
Tyrrrz 9048557b17 Short-circuit if trying to export an empty channel or a channel with mismatching boundaries
Closes #1024
2023-05-13 21:45:15 +03:00
Tyrrrz faad994332 Add docker badge 2023-05-11 18:10:54 +03:00
Tyrrrz d7537425f8 Use tar as output for docker to avoid unsupported file paths 2023-05-11 16:17:30 +03:00
Tyrrrz e261edde8b Re-add the Docker build job on GitHub Actions 2023-05-11 16:07:06 +03:00
Tyrrrz 72bb886a5e More explicit names 2023-05-11 01:59:36 +03:00
Tyrrrz 9428d5ed56 Prettify workflow files 2023-05-11 01:06:58 +03:00
Tyrrrz b8b1fdb1d1 Make push work in docker workflow 2023-05-11 00:39:09 +03:00
Tyrrrz 168b3c2f31 Fix docker workflow issues 2023-05-11 00:27:22 +03:00
Tyrrrz 9575fd11b5 Update docker workflow 2023-05-11 00:20:29 +03:00
Tyrrrz a332bda07d Make dockerfile more platform-agnostic 2023-05-11 00:06:30 +03:00
Tyrrrz c3f181c9e0 Clean up the docker file 2023-05-10 22:02:50 +03:00
Tyrrrz b9cd40f211 Fix notify in CI 2023-05-02 19:20:23 +03:00
Tyrrrz ab933a7240 Rename TitleLinkNodeMatcher to MaskedLinkNodeMatcher to align with Discord's own terminology 2023-04-30 20:05:31 +03:00
Tyrrrz a9fc439cc5 Update NuGet packages 2023-04-28 17:20:04 +03:00
Tyrrrz d4f5fc5efc Update NuGet packages 2023-04-24 19:39:32 +03:00
Tyrrrz 505330e117 Consistency 2023-04-14 21:04:21 +03:00
Tyrrrz 36db99b5cf Fix workflows 2023-04-06 11:01:45 +03:00
Tyrrrz a4110a362f Use my own GitHub actions 2023-04-05 19:17:43 +03:00
Tyrrrz 0ab8bd67c4 Clean up 2023-04-02 11:41:43 +03:00
Jamie Dubs 31952a109e Fix broken links to the 'Scheduling-*' guides (#1020) 2023-03-23 21:19:04 +02:00
Tyrrrz 2e1636e6c9 Re-implement changes in the previous commit 2023-03-23 13:23:32 +02:00
Tyrrrz 20e782e6ed Revert "Add env variable fallback for --fuck-russia"
This reverts commit ebb838bb81.

Closes #1019
2023-03-23 02:59:43 +02:00
Tyrrrz ebb838bb81 Add env variable fallback for --fuck-russia 2023-03-15 20:44:24 +02:00
Tyrrrz 966627f289 Update version 2023-03-10 16:35:13 +02:00
Tyrrrz 4ec00d0ec8 Clean up CSS 2023-03-10 16:33:56 +02:00
Tyrrrz 8bc1bcaf16 Render linked videos as embeds in HTML
Related to #1012
2023-03-10 16:17:35 +02:00
Tyrrrz 8f40acdda7 Load mentions for users that left the guild 2023-03-08 12:32:59 +02:00
Mark Cilia Vincenti bbe18368fa Update to AsyncKeyedLock 6.2.0 (#1003) 2023-02-25 20:30:00 +02:00
203 changed files with 13222 additions and 12360 deletions
+15 -53
View File
@@ -16,9 +16,9 @@ Note the `:stable` tag. DiscordChatExporter images are tagged according to the f
- `stable`: Latest stable version release and maps to the latest GitHub release. This tag is updated with release of each new version. Recommended for personal use. - `stable`: Latest stable version release and maps to the latest GitHub release. This tag is updated with release of each new version. Recommended for personal use.
- `latest`: Built from the latest commit and maps to the latest CI build on GitHub. This tag is updated with each new commit to `master` branch. Not recommended for everyday use, but you can use it if you want to try out some features that haven't been officially released yet. - `latest`: Built from the latest commit and maps to the latest CI build on GitHub. This tag is updated with each new commit to `master` branch. Not recommended for everyday use, but you can use it if you want to try out some features that haven't been officially released yet.
- `x.y.z` (e.g. `2.30.1`): Fixed version release and maps to the corresponding tag on GitHub. This tag is never updated once that version has been released. Recommended for use in various automation scenarios. - `x.y.z` (e.g. `2.30.1`): Fixed version release and maps to the corresponding tag on GitHub. This tag is never updated once that version has been released. Recommended for use in automation scenarios.
You can see all available tags [here](https://hub.docker.com/r/tyrrrz/discordchatexporter/tags?page=1&ordering=name). You can see all available tags [here](https://hub.docker.com/r/tyrrrz/discordchatexporter/tags?ordering=name).
## Usage ## Usage
@@ -34,7 +34,7 @@ To export a channel:
docker run --rm -v /path/on/machine:/out tyrrrz/discordchatexporter:stable export -t TOKEN -c CHANNELID docker run --rm -v /path/on/machine:/out tyrrrz/discordchatexporter:stable export -t TOKEN -c CHANNELID
``` ```
If you want colored output, real-time progress reporting, and some other stuff, pass `-it` (interactive) option: If you want colored output and real-time progress reporting, pass the `-it` (interactive) option:
``` ```
docker run --rm -it -v /path/on/machine:/out tyrrrz/discordchatexporter:stable export -t TOKEN -c CHANNELID docker run --rm -it -v /path/on/machine:/out tyrrrz/discordchatexporter:stable export -t TOKEN -c CHANNELID
@@ -42,68 +42,30 @@ docker run --rm -it -v /path/on/machine:/out tyrrrz/discordchatexporter:stable e
Note the `-v /path/on/machine:/out` option, which instructs Docker to bind the `/out` directory inside the container to a path on your host machine. Replace `/path/on/machine` with the directory you want the files to be saved at. Note the `-v /path/on/machine:/out` option, which instructs Docker to bind the `/out` directory inside the container to a path on your host machine. Replace `/path/on/machine` with the directory you want the files to be saved at.
**If you are running SELinux, you will need to add the `:z` option after `/out`, e.g.:** > **Note**:
> If you are running SELinux, you will need to add the `:z` option after `/out`, e.g.:
``` >
docker run --rm -v /path/on/machine:/out:z tyrrrz/discordchatexporter:stable export -t TOKEN -c CHANNELID > ```
``` > docker run --rm -v /path/on/machine:/out:z tyrrrz/discordchatexporter:stable export -t TOKEN -c CHANNELID
> ```
For more information, refer to the [Docker docs SELinux labels for bind mounts page](https://docs.docker.com/storage/bind-mounts/#configure-the-selinux-label). >
> For more information, refer to the [Docker docs SELinux labels for bind mounts page](https://docs.docker.com/storage/bind-mounts/#configure-the-selinux-label).
You can also use the current working directory as the output directory by specifying: You can also use the current working directory as the output directory by specifying:
- `-v $PWD:/out` in Mac/Linux - `-v $PWD:/out` in Bash
- `-v $pwd.Path:/out` in PowerShell - `-v $pwd.Path:/out` in PowerShell
For more information, please refer to the [Dockerfile](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/Dockerfile) and [Docker documentation](https://docs.docker.com/engine/reference/run/). For more information, please refer to the [Dockerfile](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/DiscordChatExporter.Cli.dockerfile) and [Docker documentation](https://docs.docker.com/engine/reference/run).
To get your Token and Channel IDs, please refer to [this page](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Token-and-IDs.md). To get your Token and Channel IDs, please refer to [this page](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Token-and-IDs.md).
## Environment variables ## Environment variables
DiscordChatExpoter CLI accepts environment variables as fallbacks for the `--token` and `--bot` options. DiscordChatExpoter CLI accepts the `DISCORD_TOKEN` environment variable as a fallback for the `--token` option. You can set this variable either with the `--env` Docker option or with a combination of the `--env-file` Docker option and a `.env` file.
- `DISCORD_TOKEN` to set the token Please refer to the [Docker documentation](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file) for more information.
- `DISCORD_TOKEN_BOT` to set whether it's a bot token (`true` or `false`)
You can use these variables either with the `--env` Docker option or with a combination of the `--env-file` Docker option and a `.env` file.
Please refer to the Docker documentation for more information:
- [Docker run - Set environment variables](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file)
## Permission issues with Linux hosts ## Permission issues with Linux hosts
When bounding volumes between the container and the host, files mirrored from the Docker container will be owned by the Docker user (in most setups this is `root`). Please refer to this [issue comment](https://github.com/Tyrrrz/DiscordChatExporter/issues/800#issuecomment-1030471970) for instructions on how to override this behavior. When bounding volumes between the container and the host, files mirrored from the Docker container will be owned by the Docker user (in most setups this is `root`). Please refer to this [issue comment](https://github.com/Tyrrrz/DiscordChatExporter/issues/800#issuecomment-1030471970) for instructions on how to override this behavior.
## Build Docker images for arm on Raspberry Pi
If you use the Docker image on arm (for example Raspberry Pi), you'd build the image as below.
```bash
$ git clone https://github.com/Tyrrrz/DiscordChatExporter.git
$ cd DiscordChatExporter
$ docker build -f DiscordChatExporter.Cli.dockerfile -t tyrrrz/discordchatexporter:latest .
```
After a while, some images are built like below.
```bash
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
tyrrrz/discordchatexporter latest 7c9ead725177 49 seconds ago 199MB
<none> <none> 57f9f49d05af About a minute ago 678MB
mcr.microsoft.com/dotnet/sdk 5.0 72af6071941e 4 days ago 641MB
mcr.microsoft.com/dotnet/runtime 3.1 6229bebd5a11 4 days ago 197MB
```
Among these, two images (`tyrrrz/discordchatexporter:latest` and `mcr.microsoft.com/dotnet/runtime:3.1`) are required, so you may delete the other two images.
Now you can execute DiscordChatExporter:
```bash
$ docker run --rm tyrrrz/discordchatexporter:latest
```
---
Special thanks to [@simnalamburt](https://github.com/simnalamburt) (dockerize) and [@Nimja](https://github.com/nimja) (better instructions)
+16 -4
View File
@@ -2,7 +2,7 @@
You can use a special notation to filter messages that you want to have included in an export. The notation syntax is designed to mimic Discord's search query syntax, but with additional capabilities. You can use a special notation to filter messages that you want to have included in an export. The notation syntax is designed to mimic Discord's search query syntax, but with additional capabilities.
To configure a filter, specify it in advanced export parameters when using the GUI or by passing the `--filter` option when using the CLI. To configure a filter, specify it in advanced export parameters when using the GUI or by passing the `--filter` option when using the CLI. For the CLI version, see also [caveats](#cli-caveats).
## Examples ## Examples
@@ -84,10 +84,22 @@ from:Tyrrrz | from:"96-LB"
from:96\-LB from:96\-LB
``` ```
## CLI Caveat ## CLI Caveats
Negated filters (those that start with `-`) may cause parsing issues when using the CLI version of DiscordChatExporter. To avoid this, use the tilde (`~`) character instead of the dash (`-`): In most cases, you will need to enclose your filter in quotes (`"`) to escape characters that may have special meaning in your shell:
```
DiscordChatExporter.Cli export [...] --filter "from:Tyrrrz has:image"
```
If you need to include quotes inside the filter itself as well, use single quotes (`'`) for those instead:
```
DiscordChatExporter.Cli export [...] --filter "from:Tyrrrz 'hello world'"
```
Additionally, negated filters (those that start with `-`) may cause parsing issues even when enclosed in quotes. To avoid this, use the tilde (`~`) character instead of the dash (`-`):
``` ```
DiscordChatExporter.Cli export [...] --filter ~from:Tyrrrz DiscordChatExporter.Cli export [...] --filter ~from:Tyrrrz
``` ```
+3 -3
View File
@@ -14,9 +14,9 @@
- [How to get Token and Channel IDs](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Token-and-IDs.md) - [How to get Token and Channel IDs](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Token-and-IDs.md)
- [How to use message filters](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Message-filters.md) - [How to use message filters](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Message-filters.md)
- Export scheduling with CLI: - Export scheduling with CLI:
- [Windows](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/scheduling-windows.md) - [Windows](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Scheduling-Windows.md)
- [macOS](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/scheduling-MacOS.md) - [macOS](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Scheduling-MacOS.md)
- [Linux](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/scheduling-Linux.md) - [Linux](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs/Scheduling-Linux.md)
## Video tutorial ## Video tutorial
+2 -22
View File
@@ -234,29 +234,9 @@ Prerequisite step: Navigate to [discord.com](https://discord.com) and login.
--- ---
## How to get a Server ID or a Server Channel ID ## How to get a Server ID or a Channel ID
1. Open Discord Settings 1. Open Discord Settings
2. Go to the **Advanced** section 2. Go to the **Advanced** section
3. Enable **Developer Mode** 3. Enable **Developer Mode**
4. Right-click on the desired server or channel and click Copy ID 4. Right-click on the desired server or channel and click Copy Server ID or Copy Channel ID
## How to get a Direct Message Channel ID
1. Click the three dots next to any message in the channel you want to export.
2. Paste the link into a text editor.
3. Copy the text in the highlighted part of the link.
> E.g. in https//discord.com/channels/@me/**189716987098470**/0985709387059874, you should copy **189716987098470**
Make sure you're not copying the user's ID.
**Alternatively, you can also:**
1. Open the desired direct message channel
2. Press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd> (<kbd>⌥</kbd>+<kbd>⌘</kbd>+<kbd>I</kbd> on macOS) on Discord to show developer tools
3. Navigate to the `Console` tab
4. Type `window.location.href` and press Enter
5. Copy the first long sequence of numbers inside the URL
> E.g. in https//discord.com/channels/@me/**84289740160**, you should copy **84289740160**
+63 -20
View File
@@ -1,4 +1,4 @@
name: 🐞 Bug report name: 🐛 Bug report
description: Report broken functionality. description: Report broken functionality.
labels: [bug] labels: [bug]
@@ -6,26 +6,25 @@ body:
- type: markdown - type: markdown
attributes: attributes:
value: | value: |
🧐 **Guidelines:** - Avoid generic or vague titles such as "Something's not working" or "A couple of problems" — be as descriptive as possible.
- Keep your issue focused on one single problem. If you have multiple bug reports, please create a separate issue for each of them.
- Issues should represent **complete and actionable** work items. If you are unsure about something or have a question, please start a [discussion](https://github.com/Tyrrrz/DiscordChatExporter/discussions/new) instead.
- Remember that **DiscordChatExporter** is an open-source project funded by the community. If you find it useful, **please consider [donating](https://tyrrrz.me/donate) to support its development**.
- Search through [existing issues](https://github.com/Tyrrrz/DiscordChatExporter/issues?q=is%3Aissue) first to ensure that this bug has not been reported before. ___
- Write a descriptive title for your issue. Avoid generic or vague titles such as "Something's not working" or "A couple of problems".
- Keep your issue focused on one single problem. If you have multiple bug reports, please create separate issues for each of them.
- Provide as much context as possible in the details section. Include screenshots, screen recordings, links, references, or anything else you may consider relevant.
- If you want to ask a question instead of reporting a bug, please use [discussions](https://github.com/Tyrrrz/DiscordChatExporter/discussions/new) instead.
- type: input - type: input
attributes: attributes:
label: Version label: Version
description: Which version of DiscordChatExporter does this bug affect? description: Which version of the application does this bug affect? Make sure you're not using an outdated version.
placeholder: ver X.Y.Z placeholder: v1.0.0
validations: validations:
required: true required: true
- type: dropdown - type: dropdown
attributes: attributes:
label: Flavor label: Flavor
description: Does this bug affect the GUI version, the CLI version, or both? description: Which flavor(s) of the application does this bug affect?
multiple: true multiple: true
options: options:
- GUI (Graphical User Interface) - GUI (Graphical User Interface)
@@ -33,10 +32,18 @@ body:
validations: validations:
required: true required: true
- type: input
attributes:
label: Platform
description: Which platform do you experience this bug on?
placeholder: Docker / Windows 11
validations:
required: true
- type: dropdown - type: dropdown
attributes: attributes:
label: Export format label: Export format
description: Does this bug affect specific export format(s)? If not applicable, leave this field blank. description: Which export format(s) do you experience this bug with, if applicable?
multiple: true multiple: true
options: options:
- HTML - HTML
@@ -44,21 +51,57 @@ body:
- JSON - JSON
- CSV - CSV
- type: textarea
attributes:
label: Details
description: Clear and thorough explanation of the bug.
placeholder: I was doing X expecting Y to happen, but Z happened instead.
validations:
required: true
- type: textarea - type: textarea
attributes: attributes:
label: Steps to reproduce label: Steps to reproduce
description: Minimum steps required to reproduce the bug. description: >
Minimum steps required to reproduce the bug, including prerequisites, export settings, or other relevant items.
The information provided in this field must be readily actionable, meaning that anyone should be able to reproduce the bug by following these steps.
If the bug depends on external factors (such as a specific server, channel, or message), then this field must include the server invite and the corresponding link.
placeholder: | placeholder: |
Server invite: https://discord.gg/...
Channel or message link: https://discord.com/channels/.../...
Export settings:
- ...
Application settings:
- ...
Steps:
- Step 1 - Step 1
- Step 2 - Step 2
- Step 3 - Step 3
validations: validations:
required: true required: true
- type: textarea
attributes:
label: Details
description: Clear and thorough explanation of the bug, including any additional information you may find relevant.
placeholder: |
- Expected behavior: ...
- Actual behavior: ...
validations:
required: true
- type: checkboxes
attributes:
label: Checklist
description: Quick list of checks to ensure that everything is in order.
options:
- label: I have looked through existing issues to make sure that this bug has not been reported before
required: true
- label: I have provided a descriptive title for this issue
required: true
- label: I have made sure that that this bug is reproducible on the latest version of the application
required: true
- label: I have provided all the information needed to reproduce this bug as efficiently as possible
required: true
- label: I have sponsored this project
required: false
- type: markdown
attributes:
value: |
If you are struggling to provide actionable reproduction steps, or if something else is preventing you from creating a complete bug report, please start a [discussion](https://github.com/Tyrrrz/DiscordChatExporter/discussions/new) instead.
+4 -4
View File
@@ -3,12 +3,12 @@ contact_links:
- name: ⚠ Feature request - name: ⚠ Feature request
url: https://github.com/Tyrrrz/.github/blob/master/docs/project-status.md url: https://github.com/Tyrrrz/.github/blob/master/docs/project-status.md
about: Sorry, but this project is in maintenance mode and no longer accepts new feature requests. about: Sorry, but this project is in maintenance mode and no longer accepts new feature requests.
- name: 💬 Discord server
url: https://discord.gg/2SUWKFnHSm
about: Chat with the project community.
- name: 📖 Documentation - name: 📖 Documentation
url: https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs url: https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs
about: Find usage guides and frequently asked questions. about: Find usage guides and frequently asked questions.
- name: 🗨 Discussions - name: 🗨 Discussions
url: https://github.com/Tyrrrz/DiscordChatExporter/discussions/new url: https://github.com/Tyrrrz/DiscordChatExporter/discussions/new
about: Ask and answer questions. about: Ask and answer questions.
- name: 💬 Discord server
url: https://discord.gg/2SUWKFnHSm
about: Chat with the project community.
+51 -20
View File
@@ -5,51 +5,82 @@ on: [push, pull_request]
jobs: jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
actions: write
contents: read
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Build container - name: Install Docker Buildx
run: docker build -f DiscordChatExporter.Cli.dockerfile . uses: docker/setup-buildx-action@v2
- name: Build image
run: >
docker buildx build
--file DiscordChatExporter.Cli.dockerfile
--platform linux/amd64,linux/arm64
--output type=tar,dest=DiscordChatExporter.Cli.Docker.tar
.
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: DiscordChatExporter.Cli.Docker
path: DiscordChatExporter.Cli.Docker.tar
deploy-latest: deploy-latest:
if: ${{ github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master' }} if: ${{ github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master' }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
contents: read
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Install Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub - name: Login to DockerHub
run: docker login -u tyrrrz -p ${{ secrets.DOCKER_TOKEN }} run: >
echo ${{ secrets.DOCKER_TOKEN }} |
docker login --username tyrrrz --password-stdin
- name: Build container - name: Build & push image
run: docker build -f DiscordChatExporter.Cli.dockerfile -t tyrrrz/discordchatexporter:latest . run: >
docker buildx build
- name: Push container --file DiscordChatExporter.Cli.dockerfile
run: docker push tyrrrz/discordchatexporter:latest --platform linux/amd64,linux/arm64
--push
--tag tyrrrz/discordchatexporter:latest
.
deploy-stable: deploy-stable:
if: ${{ github.event_name == 'push' && github.ref_type == 'tag' }} if: ${{ github.event_name == 'push' && github.ref_type == 'tag' }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
contents: read
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Get release version - name: Install Docker Buildx
id: get_version uses: docker/setup-buildx-action@v2
uses: dawidd6/action-get-tag@v1
- name: Login to DockerHub - name: Login to DockerHub
run: docker login -u tyrrrz -p ${{ secrets.DOCKER_TOKEN }} run: >
echo ${{ secrets.DOCKER_TOKEN }} |
docker login --username tyrrrz --password-stdin
- name: Build container - name: Build & push image
run: docker build -f DiscordChatExporter.Cli.dockerfile -t tyrrrz/discordchatexporter:stable -t tyrrrz/discordchatexporter:${{ steps.get_version.outputs.tag }} . run: >
docker buildx build
- name: Push container (stable) --file DiscordChatExporter.Cli.dockerfile
run: docker push tyrrrz/discordchatexporter:stable --platform linux/amd64,linux/arm64
--push
- name: Push container (${{ steps.get_version.outputs.tag }}) --tag tyrrrz/discordchatexporter:stable
run: docker push tyrrrz/discordchatexporter:${{ steps.get_version.outputs.tag }} --tag tyrrrz/discordchatexporter:${{ github.ref_name }}
.
+53 -26
View File
@@ -5,6 +5,8 @@ on: [push, pull_request]
jobs: jobs:
test: test:
runs-on: windows-latest runs-on: windows-latest
permissions:
contents: read
env: env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
@@ -23,7 +25,14 @@ jobs:
- name: Run tests - name: Run tests
# Tests need access to secrets, so we can't run them against PRs because of limited trust # Tests need access to secrets, so we can't run them against PRs because of limited trust
if: ${{ github.event_name != 'pull_request' }} if: ${{ github.event_name != 'pull_request' }}
run: dotnet test --configuration Release --logger GitHubActions --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover run: >
dotnet test
--configuration Release
--logger "GitHubActions;summary.includePassedTests=true;summary.includeSkippedTests=true"
--collect:"XPlat Code Coverage"
--
RunConfiguration.CollectSourceInformation=true
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
env: env:
DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }} DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }}
@@ -32,9 +41,12 @@ jobs:
with: with:
token: ${{ secrets.CODECOV_TOKEN }} token: ${{ secrets.CODECOV_TOKEN }}
package: pack:
needs: test needs: test
runs-on: windows-latest runs-on: windows-latest
permissions:
actions: write
contents: read
env: env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
@@ -51,27 +63,36 @@ jobs:
dotnet-version: 7.0.x dotnet-version: 7.0.x
- name: Publish (CLI) - name: Publish (CLI)
run: dotnet publish DiscordChatExporter.Cli/ -o DiscordChatExporter.Cli/bin/Publish/ --configuration Release run: >
dotnet publish DiscordChatExporter.Cli
--output DiscordChatExporter.Cli/publish/
--configuration Release
- name: Publish (GUI) - name: Publish (GUI)
run: dotnet publish DiscordChatExporter.Gui/ -o DiscordChatExporter.Gui/bin/Publish/ --configuration Release run: >
dotnet publish DiscordChatExporter.Gui
--output DiscordChatExporter.Gui/publish/
--configuration Release
- name: Upload artifacts (CLI) - name: Upload artifacts (CLI)
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3
with: with:
name: DiscordChatExporter.Cli name: DiscordChatExporter.Cli
path: DiscordChatExporter.Cli/bin/Publish/ path: DiscordChatExporter.Cli/publish/
- name: Upload artifacts (GUI) - name: Upload artifacts (GUI)
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3
with: with:
name: DiscordChatExporter name: DiscordChatExporter
path: DiscordChatExporter.Gui/bin/Publish/ path: DiscordChatExporter.Gui/publish/
deploy: deploy:
if: ${{ github.event_name == 'push' && github.ref_type == 'tag' }} if: ${{ github.event_name == 'push' && github.ref_type == 'tag' }}
needs: package needs: pack
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
actions: read
contents: write
steps: steps:
- name: Download artifacts (CLI) - name: Download artifacts (CLI)
@@ -87,43 +108,49 @@ jobs:
path: DiscordChatExporter.Gui path: DiscordChatExporter.Gui
- name: Create package (CLI) - name: Create package (CLI)
run: Compress-Archive -Path DiscordChatExporter.Cli/* -DestinationPath DiscordChatExporter.Cli.zip -Force
shell: pwsh shell: pwsh
run: >
Compress-Archive
-Path DiscordChatExporter.Cli/*
-DestinationPath DiscordChatExporter.Cli.zip
-Force
- name: Create package (GUI) - name: Create package (GUI)
run: Compress-Archive -Path DiscordChatExporter.Gui/* -DestinationPath DiscordChatExporter.zip -Force
shell: pwsh shell: pwsh
run: >
Compress-Archive
-Path DiscordChatExporter.Gui/*
-DestinationPath DiscordChatExporter.zip
-Force
- name: Create release - name: Create release
uses: softprops/action-gh-release@v1
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: run: >
files: | gh release create "${{ github.ref_name }}"
DiscordChatExporter.Cli.zip "DiscordChatExporter.Cli.zip"
DiscordChatExporter.zip "DiscordChatExporter.zip"
body: | --repo "${{ github.event.repository.full_name }}"
[Changelog](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/Changelog.md) --title "${{ github.ref_name }}"
--notes "[Changelog](${{ github.event.repository.html_url }}/blob/${{ github.ref_name }}/Changelog.md)"
--verify-tag
notify: notify:
needs: deploy needs: deploy
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
contents: read
steps: steps:
- name: Get release version
id: get_version
uses: dawidd6/action-get-tag@v1
- name: Notify Discord - name: Notify Discord
uses: satak/webrequest-action@v1.2.4 uses: tyrrrz/action-http-request@v1
with: with:
url: ${{ secrets.DISCORD_WEBHOOK }} url: ${{ secrets.DISCORD_WEBHOOK }}
method: POST method: POST
headers: | headers: |
Content-Type: application/json; charset=UTF-8
body: |
{ {
"ContentType": "application/json; charset=UTF-8" "avatar_url": "https://raw.githubusercontent.com/${{ github.event.repository.full_name }}/${{ github.ref_name }}/favicon.png",
} "content": "**${{ github.event.repository.name }}** new version released!\nVersion: `${{ github.ref_name }}`\nChangelog: <${{ github.event.repository.html_url }}/blob/${{ github.ref_name }}/Changelog.md>"
payload: |
{
"content": "**DiscordChatExporter** new version released!\nVersion: `${{ steps.get_version.outputs.tag }}`\nChangelog: <https://github.com/Tyrrrz/DiscordChatExporter/blob/${{ steps.get_version.outputs.tag }}/Changelog.md>"
} }
+70 -4
View File
@@ -1,8 +1,74 @@
# Changelog # Changelog
## v2.41 (15-Sep-2023)
- General changes:
- Replaced the "date format" option with a "locale" option. Now, you can choose one of the locales supported by the Discord app, and DiscordChatExporter will infer all the required date, time, and number formats automatically. In GUI, the locale can be selected from the settings window. In CLI, the locale can be specified using the `--locale` option (e.g. `--locale "en-US"`). By default, the current system locale is used.
- Optimized the algorithm for fetching threads to improve performance. (Thanks [@slatinsky](https://github.com/slatinsky))
- GUI changes:
- Improved the initial load time after submitting the token. Now, only the guilds are loaded initially, and the channels are loaded on-demand when the user selects a guild. This should significantly improve performance for accounts with many guilds.
- Added an option to include threads in the channel list, which is configurable in settings. When enabled, threads will appear at the bottom of the list, grouped by the parent channel. Note that pulling threads (especially archived threads) can be a very time-consuming process, depending on the guild size. By default, threads are not included.
- CLI changes:
- Added a warning when using the `--bot` option, informing that it's obsolete and shouldn't be used.
- Changed the `--include-threads` option on the `channels` command from a switch (boolean) option, to an enum (choice) option. It now 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`. For backwards compatibility, the `--include-threads` option can still be used as a switch, in which case it will be treated as `--include-threads active`. Note that pulling threads (especially archived threads) can be a very time-consuming process, depending on the guild size.
- Added the `--include-threads` option to the `exportguild` and `exportall` commands. Using this option, you can specify whether to include threads (and which type) in the export. By default, threads are not included.
- Added the `--include-vc` option to the `channels` command. Using this option, you can specify whether to include voice-channel-accompanying text channels in the output. By default, these channels are included.
- Changed the progress reporting behavior of export commands such that tasks are now hidden on completion if parallelization is enabled (i.e. `--parallel` is set to `2` or higher). This should help improve visual clarity when exporting many channels in parallel.
## v2.40.4 (11-Aug-2023)
- HTML changes:
- Fixed an issue where the "call lasted for X minutes" system message was unnecessarily too precise. Now, it's rounded to the nearest minute.
- Fixed an issue where multi-line quote blocks with empty lines in the middle were rendered as separate blocks.
- JSON changes:
- Added the `users` field to the reaction object, containing an array with limited information about the users that reacted with that particular emoji. (Thanks [@slatinsky](https://github.com/slatinsky))
## v2.40.3 (29-Jul-2023)
- GUI changes:
- Fixed an issue where guild categories were rendered with blank names.
## v2.40.2 (27-Jul-2023)
- General changes:
- Improved error reporting on unexpected HTTP errors.
- CLI changes:
- Fixed an issue which caused a crash when trying to export a private (but accessible) channel inside a private (but not accessible) category.
## v2.40.1 (21-Jul-2023)
- General changes:
- Fixed an issue where the fallback avatar (i.e. the avatar used for when the user doesn't have a custom one set) was not resolved properly for users that don't have a discriminator.
- CLI changes:
- Added the `--include-vc` option to the `exportall` and `exportguild` commands, which instructs whether to include voice-channel-accompanying text channels in the export. By default, this is set to `true` to match the previous behavior. To disable the option, use `--include-vc false`.
- GUI changes:
- Replaced the blur effect applied to the token text box with a password mask.
- JSON format changes:
- Added the `exportedAt` field to the export metadata, which contains the timestamp corresponding to when the export was created. (Thanks [@slatinsky](https://github.com/slatinsky))
- Added the `roles` field to the object describing a user. This field contains an array of objects that describe the roles assigned to the user. (Thanks [@slatinsky](https://github.com/slatinsky))
## v2.40 (08-Jun-2023)
- General changes:
- Added support for users that don't have discriminators.
- Added a precondition check that verifies whether the requested channel contains any messages for the specified period, before starting the export process. This check works by comparing the export bounds against the ID of the last message in the channel, which is provided by the Discord API as part of the channel metadata. When exporting many channels in batches (e.g. using the `exportall` command), this check may eliminate a significant number of unnecessary API calls and speed up the export process.
- CLI changes:
- Extended the `channels` command with the `--include-threads` option. When this option is provided, the command will include channel threads in the output. (Thanks [@WAUthethird](https://github.com/WAUthethird))
- Simplified the information in the `guide` command related to the extraction of DM channel IDs. Discord now provides a native way of copying IDs of DM channels, so the previous devtools-based workaround is no longer necessary.
- GUI changes:
- Added a blur effect to the token text box, which activates when the control loses focus. This is done to reduce the risk of accidentally exposing the token when taking screenshots.
- HTML format changes:
- Added support for the header markdown syntax.
- Added support for the list markdown syntax.
## v2.39.1 (10-Mar-2023)
- Fixed an issue where messages containing mentions of users that left the server (but haven't deleted their account) were incorrectly rendered as `@Unknown`.
- Fixed an issue where messages containing video links were not rendered correctly in the HTML export. Now, they are rendered using a video player and the referenced assets are pulled if the "download assets" option is enabled.
## v2.39 (25-Feb-2023) ## v2.39 (25-Feb-2023)
- Added an option to specify the directory path for downloaded assets. This can be used, in combination with the "reuse assets" option, to have a shared asset directory for multiple exports. In GUI, this option can be found in the advanced setion of the export dialog. In CLI, it's available as `--media-dir <path>`. (Thanks [@96-LB](https://github.com/96-LB)) - Added an option to specify the directory path for downloaded assets. This can be used, in combination with the "reuse assets" option, to have a shared asset directory for multiple exports. In GUI, this option can be found in the advanced section of the export dialog. In CLI, it's available as `--media-dir <path>`. (Thanks [@96-LB](https://github.com/96-LB))
- Added support for server-level user avatars. If a user has a custom avatar set on the server, it will be rendered instead of their main avatar. - Added support for server-level user avatars. If a user has a custom avatar set on the server, it will be rendered instead of their main avatar.
- CLI changes: - CLI changes:
- Extended the `exportall` command with the `--data-package` option. You can use this option to provide the path to your personal data package (`package.zip` file), requested from Discord. When provided, the command will pull channels listed in the package instead of using the Discord API to query them. This can be particularly useful if you want to export DM channels that have been closed, as the personal data package should still contain their IDs. - Extended the `exportall` command with the `--data-package` option. You can use this option to provide the path to your personal data package (`package.zip` file), requested from Discord. When provided, the command will pull channels listed in the package instead of using the Discord API to query them. This can be particularly useful if you want to export DM channels that have been closed, as the personal data package should still contain their IDs.
@@ -21,7 +87,7 @@
- Updated the usage guide with additional steps required to retrieve the user token. - Updated the usage guide with additional steps required to retrieve the user token.
- Fixed an issue where certain user mentions were incorrectly rendered as `@Unknown`. - Fixed an issue where certain user mentions were incorrectly rendered as `@Unknown`.
- Fixed an issue where some embed images were rendered using their actual URLs instead of Discord proxy URLs. - Fixed an issue where some embed images were rendered using their actual URLs instead of Discord proxy URLs.
- GUI changes: - GUI changes:
- Added a text box for specifying the output path when exporting channels. It can be used to set an output path that uses template tokens, which previously required hacky workarounds. This text box can be left empty, in which case clicking on the "Export" button will open the file or folder picker dialog, matching the previous behavior. - Added a text box for specifying the output path when exporting channels. It can be used to set an output path that uses template tokens, which previously required hacky workarounds. This text box can be left empty, in which case clicking on the "Export" button will open the file or folder picker dialog, matching the previous behavior.
- Added a notification when the application is updated to a new version, containing the changelog link. - Added a notification when the application is updated to a new version, containing the changelog link.
- Moved the "reuse assets" option from the settings dialog to the export dialog. - Moved the "reuse assets" option from the settings dialog to the export dialog.
@@ -29,7 +95,7 @@
- Minor cosmetic improvements. - Minor cosmetic improvements.
- CLI changes: - CLI changes:
- Changed the behavior of the `export` command when providing IDs of category channels. Now, instead of displaying an error, the command will export all channels contained within the corresponding categories. - Changed the behavior of the `export` command when providing IDs of category channels. Now, instead of displaying an error, the command will export all channels contained within the corresponding categories.
- HTML format changes: - HTML format changes:
- Added proper support for more system notification messages. - Added proper support for more system notification messages.
- Added support for rendering server invites. Now, if the message contains an invite link, the export will show the server icon and include additional information, such as the name of the server and the target channel. - Added support for rendering server invites. Now, if the message contains an invite link, the export will show the server icon and include additional information, such as the name of the server and the target channel.
- Changed the preamble part of the export to show the channel icon when available, instead of the default Discord logo. This is only available for group DM channels. (Thanks [@CanePlayz](https://github.com/CanePlayz)) - Changed the preamble part of the export to show the channel icon when available, instead of the default Discord logo. This is only available for group DM channels. (Thanks [@CanePlayz](https://github.com/CanePlayz))
@@ -524,4 +590,4 @@ Please note that CSV export is most likely not going to receive future updates a
- Added support for user and role mentions. - Added support for user and role mentions.
- Added support for channel mentions. - Added support for channel mentions.
- Fixed text in pre blocks not being wrapped correctly. - Fixed text in pre blocks not being wrapped correctly.
- Added workaround for non-default message types. - Added workaround for non-default message types.
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<Version>2.39</Version> <Version>2.41</Version>
<Company>Tyrrrz</Company> <Company>Tyrrrz</Company>
<Copyright>Copyright (c) Oleksii Holub</Copyright> <Copyright>Copyright (c) Oleksii Holub</Copyright>
<LangVersion>preview</LangVersion> <LangVersion>preview</LangVersion>
@@ -8,22 +8,22 @@
<ItemGroup> <ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" /> <Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
<None Include="*.secret" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AngleSharp" Version="1.0.1" /> <PackageReference Include="AngleSharp" Version="1.0.4" />
<PackageReference Include="FluentAssertions" Version="6.10.0" /> <PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.0.1" PrivateAssets="all" /> <PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.2" PrivateAssets="all" />
<PackageReference Include="JsonExtensions" Version="1.2.0" /> <PackageReference Include="JsonExtensions" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="ReflectionMagic" Version="4.1.0" /> <PackageReference Include="ReflectionMagic" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.5.0" PrivateAssets="all" />
<PackageReference Include="coverlet.collector" Version="3.2.0" PrivateAssets="all" /> <PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -23,4 +23,4 @@ public static class ChannelIds
public static Snowflake SelfContainedTestCases { get; } = Snowflake.Parse("887441432678379560"); public static Snowflake SelfContainedTestCases { get; } = Snowflake.Parse("887441432678379560");
public static Snowflake StickerTestCases { get; } = Snowflake.Parse("939668868253769729"); public static Snowflake StickerTestCases { get; } = Snowflake.Parse("939668868253769729");
} }
@@ -19,14 +19,16 @@ namespace DiscordChatExporter.Cli.Tests.Infra;
public static class ExportWrapper public static class ExportWrapper
{ {
private static readonly AsyncKeyedLocker<string> Locker = new(o => private static readonly AsyncKeyedLocker<string> Locker =
{ new(o =>
o.PoolSize = 20; {
o.PoolInitialFill = 1; o.PoolSize = 20;
}); o.PoolInitialFill = 1;
});
private static readonly string DirPath = Path.Combine( private static readonly string DirPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Directory.GetCurrentDirectory(), Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? Directory.GetCurrentDirectory(),
"ExportCache" "ExportCache"
); );
@@ -36,9 +38,7 @@ public static class ExportWrapper
{ {
Directory.Delete(DirPath, true); Directory.Delete(DirPath, true);
} }
catch (DirectoryNotFoundException) catch (DirectoryNotFoundException) { }
{
}
Directory.CreateDirectory(DirPath); Directory.CreateDirectory(DirPath);
} }
@@ -48,32 +48,31 @@ public static class ExportWrapper
var fileName = channelId.ToString() + '.' + format.GetFileExtension(); var fileName = channelId.ToString() + '.' + format.GetFileExtension();
var filePath = Path.Combine(DirPath, fileName); var filePath = Path.Combine(DirPath, fileName);
// Lock separately for each channel and format using var _ = await Locker.LockAsync(filePath);
using (await Locker.LockAsync(filePath)) using var console = new FakeConsole();
{
// Perform export only if it hasn't been done before
if (!File.Exists(filePath))
{
await new ExportChannelsCommand
{
Token = Secrets.DiscordToken,
ChannelIds = new[] { channelId },
ExportFormat = format,
OutputPath = filePath
}.ExecuteAsync(new FakeConsole());
}
return await File.ReadAllTextAsync(filePath); // Perform the export only if it hasn't been done before
if (!File.Exists(filePath))
{
await new ExportChannelsCommand
{
Token = Secrets.DiscordToken,
ChannelIds = new[] { channelId },
ExportFormat = format,
OutputPath = filePath,
Locale = "en-US",
IsUtcNormalizationEnabled = true
}.ExecuteAsync(console);
} }
return await File.ReadAllTextAsync(filePath);
} }
public static async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId) => Html.Parse( public static async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId) =>
await ExportAsync(channelId, ExportFormat.HtmlDark) Html.Parse(await ExportAsync(channelId, ExportFormat.HtmlDark));
);
public static async ValueTask<JsonElement> ExportAsJsonAsync(Snowflake channelId) => Json.Parse( public static async ValueTask<JsonElement> ExportAsJsonAsync(Snowflake channelId) =>
await ExportAsync(channelId, ExportFormat.Json) Json.Parse(await ExportAsync(channelId, ExportFormat.Json));
);
public static async ValueTask<string> ExportAsPlainTextAsync(Snowflake channelId) => public static async ValueTask<string> ExportAsPlainTextAsync(Snowflake channelId) =>
await ExportAsync(channelId, ExportFormat.PlainText); await ExportAsync(channelId, ExportFormat.PlainText);
@@ -81,27 +80,27 @@ public static class ExportWrapper
public static async ValueTask<string> ExportAsCsvAsync(Snowflake channelId) => public static async ValueTask<string> ExportAsCsvAsync(Snowflake channelId) =>
await ExportAsync(channelId, ExportFormat.Csv); await ExportAsync(channelId, ExportFormat.Csv);
public static async ValueTask<IReadOnlyList<IElement>> GetMessagesAsHtmlAsync(Snowflake channelId) => public static async ValueTask<IReadOnlyList<IElement>> GetMessagesAsHtmlAsync(
(await ExportAsHtmlAsync(channelId)) Snowflake channelId
.QuerySelectorAll("[data-message-id]") ) => (await ExportAsHtmlAsync(channelId)).QuerySelectorAll("[data-message-id]").ToArray();
.ToArray();
public static async ValueTask<IReadOnlyList<JsonElement>> GetMessagesAsJsonAsync(Snowflake channelId) => public static async ValueTask<IReadOnlyList<JsonElement>> GetMessagesAsJsonAsync(
(await ExportAsJsonAsync(channelId)) Snowflake channelId
.GetProperty("messages") ) => (await ExportAsJsonAsync(channelId)).GetProperty("messages").EnumerateArray().ToArray();
.EnumerateArray()
.ToArray();
public static async ValueTask<IElement> GetMessageAsHtmlAsync(Snowflake channelId, Snowflake messageId) public static async ValueTask<IElement> GetMessageAsHtmlAsync(
Snowflake channelId,
Snowflake messageId
)
{ {
var message = (await GetMessagesAsHtmlAsync(channelId)) var message = (await GetMessagesAsHtmlAsync(channelId)).SingleOrDefault(
.SingleOrDefault(e => e =>
string.Equals( string.Equals(
e.GetAttribute("data-message-id"), e.GetAttribute("data-message-id"),
messageId.ToString(), messageId.ToString(),
StringComparison.OrdinalIgnoreCase StringComparison.OrdinalIgnoreCase
) )
); );
if (message is null) if (message is null)
{ {
@@ -113,16 +112,19 @@ public static class ExportWrapper
return message; return message;
} }
public static async ValueTask<JsonElement> GetMessageAsJsonAsync(Snowflake channelId, Snowflake messageId) public static async ValueTask<JsonElement> GetMessageAsJsonAsync(
Snowflake channelId,
Snowflake messageId
)
{ {
var message = (await GetMessagesAsJsonAsync(channelId)) var message = (await GetMessagesAsJsonAsync(channelId)).SingleOrDefault(
.SingleOrDefault(j => j =>
string.Equals( string.Equals(
j.GetProperty("id").GetString(), j.GetProperty("id").GetString(),
messageId.ToString(), messageId.ToString(),
StringComparison.OrdinalIgnoreCase StringComparison.OrdinalIgnoreCase
) )
); );
if (message.ValueKind == JsonValueKind.Undefined) if (message.ValueKind == JsonValueKind.Undefined)
{ {
@@ -133,4 +135,4 @@ public static class ExportWrapper
return message; return message;
} }
} }
@@ -12,6 +12,6 @@ internal static class Secrets
.Build(); .Build();
public static string DiscordToken => public static string DiscordToken =>
Configuration["DISCORD_TOKEN"] ?? Configuration["DISCORD_TOKEN"]
throw new InvalidOperationException("Discord token not provided for tests."); ?? throw new InvalidOperationException("Discord token not provided for tests.");
} }
@@ -8,22 +8,24 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class CsvContentSpecs public class CsvContentSpecs
{ {
[Fact] [Fact]
public async Task Messages_are_exported_correctly() public async Task I_can_export_a_channel_in_the_CSV_format()
{ {
// Act // Act
var document = await ExportWrapper.ExportAsCsvAsync(ChannelIds.DateRangeTestCases); var document = await ExportWrapper.ExportAsCsvAsync(ChannelIds.DateRangeTestCases);
// Assert // Assert
document.Should().ContainAll( document
"Tyrrrz#5447", .Should()
"Hello world", .ContainAll(
"Goodbye world", "tyrrrz",
"Foo bar", "Hello world",
"Hurdle Durdle", "Goodbye world",
"One", "Foo bar",
"Two", "Hurdle Durdle",
"Three", "One",
"Yeet" "Two",
); "Three",
"Yeet"
);
} }
} }
@@ -17,7 +17,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class DateRangeSpecs public class DateRangeSpecs
{ {
[Fact] [Fact]
public async Task Messages_filtered_after_specific_date_only_include_messages_sent_after_that_date() public async Task I_can_filter_the_export_to_only_include_messages_sent_after_the_specified_date()
{ {
// Arrange // Arrange
var after = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero); var after = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero);
@@ -34,8 +34,7 @@ public class DateRangeSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
var timestamps = Json var timestamps = Json.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages") .GetProperty("messages")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("timestamp").GetDateTimeOffset()) .Select(j => j.GetProperty("timestamp").GetDateTimeOffset())
@@ -43,25 +42,32 @@ public class DateRangeSpecs
timestamps.All(t => t > after).Should().BeTrue(); timestamps.All(t => t > after).Should().BeTrue();
timestamps.Should().BeEquivalentTo(new[] timestamps
{ .Should()
new DateTimeOffset(2021, 07, 24, 13, 49, 13, TimeSpan.Zero), .BeEquivalentTo(
new DateTimeOffset(2021, 07, 24, 14, 52, 38, TimeSpan.Zero), new[]
new DateTimeOffset(2021, 07, 24, 14, 52, 39, TimeSpan.Zero), {
new DateTimeOffset(2021, 07, 24, 14, 52, 40, TimeSpan.Zero), new DateTimeOffset(2021, 07, 24, 13, 49, 13, TimeSpan.Zero),
new DateTimeOffset(2021, 09, 08, 14, 26, 35, TimeSpan.Zero) new DateTimeOffset(2021, 07, 24, 14, 52, 38, TimeSpan.Zero),
}, o => new DateTimeOffset(2021, 07, 24, 14, 52, 39, TimeSpan.Zero),
{ new DateTimeOffset(2021, 07, 24, 14, 52, 40, TimeSpan.Zero),
return o new DateTimeOffset(2021, 09, 08, 14, 26, 35, TimeSpan.Zero)
.Using<DateTimeOffset>(ctx => },
ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1)) o =>
) {
.WhenTypeIs<DateTimeOffset>(); return o.Using<DateTimeOffset>(
}); ctx =>
ctx.Subject
.Should()
.BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1))
)
.WhenTypeIs<DateTimeOffset>();
}
);
} }
[Fact] [Fact]
public async Task Messages_filtered_before_specific_date_only_include_messages_sent_before_that_date() public async Task I_can_filter_the_export_to_only_include_messages_sent_before_the_specified_date()
{ {
// Arrange // Arrange
var before = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero); var before = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero);
@@ -78,8 +84,7 @@ public class DateRangeSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
var timestamps = Json var timestamps = Json.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages") .GetProperty("messages")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("timestamp").GetDateTimeOffset()) .Select(j => j.GetProperty("timestamp").GetDateTimeOffset())
@@ -87,23 +92,30 @@ public class DateRangeSpecs
timestamps.All(t => t < before).Should().BeTrue(); timestamps.All(t => t < before).Should().BeTrue();
timestamps.Should().BeEquivalentTo(new[] timestamps
{ .Should()
new DateTimeOffset(2021, 07, 19, 13, 34, 18, TimeSpan.Zero), .BeEquivalentTo(
new DateTimeOffset(2021, 07, 19, 15, 58, 48, TimeSpan.Zero), new[]
new DateTimeOffset(2021, 07, 19, 17, 23, 58, TimeSpan.Zero) {
}, o => new DateTimeOffset(2021, 07, 19, 13, 34, 18, TimeSpan.Zero),
{ new DateTimeOffset(2021, 07, 19, 15, 58, 48, TimeSpan.Zero),
return o new DateTimeOffset(2021, 07, 19, 17, 23, 58, TimeSpan.Zero)
.Using<DateTimeOffset>(ctx => },
ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1)) o =>
) {
.WhenTypeIs<DateTimeOffset>(); return o.Using<DateTimeOffset>(
}); ctx =>
ctx.Subject
.Should()
.BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1))
)
.WhenTypeIs<DateTimeOffset>();
}
);
} }
[Fact] [Fact]
public async Task Messages_filtered_between_specific_dates_only_include_messages_sent_between_those_dates() public async Task I_can_filter_the_export_to_only_include_messages_sent_between_the_specified_dates()
{ {
// Arrange // Arrange
var after = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero); var after = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero);
@@ -122,8 +134,7 @@ public class DateRangeSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
var timestamps = Json var timestamps = Json.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages") .GetProperty("messages")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("timestamp").GetDateTimeOffset()) .Select(j => j.GetProperty("timestamp").GetDateTimeOffset())
@@ -131,19 +142,26 @@ public class DateRangeSpecs
timestamps.All(t => t < before && t > after).Should().BeTrue(); timestamps.All(t => t < before && t > after).Should().BeTrue();
timestamps.Should().BeEquivalentTo(new[] timestamps
{ .Should()
new DateTimeOffset(2021, 07, 24, 13, 49, 13, TimeSpan.Zero), .BeEquivalentTo(
new DateTimeOffset(2021, 07, 24, 14, 52, 38, TimeSpan.Zero), new[]
new DateTimeOffset(2021, 07, 24, 14, 52, 39, TimeSpan.Zero), {
new DateTimeOffset(2021, 07, 24, 14, 52, 40, TimeSpan.Zero) new DateTimeOffset(2021, 07, 24, 13, 49, 13, TimeSpan.Zero),
}, o => new DateTimeOffset(2021, 07, 24, 14, 52, 38, TimeSpan.Zero),
{ new DateTimeOffset(2021, 07, 24, 14, 52, 39, TimeSpan.Zero),
return o new DateTimeOffset(2021, 07, 24, 14, 52, 40, TimeSpan.Zero)
.Using<DateTimeOffset>(ctx => },
ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1)) o =>
) {
.WhenTypeIs<DateTimeOffset>(); return o.Using<DateTimeOffset>(
}); ctx =>
ctx.Subject
.Should()
.BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1))
)
.WhenTypeIs<DateTimeOffset>();
}
);
} }
} }
@@ -16,7 +16,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class FilterSpecs public class FilterSpecs
{ {
[Fact] [Fact]
public async Task Messages_filtered_by_text_only_include_messages_that_contain_that_text() public async Task I_can_filter_the_export_to_only_include_messages_that_contain_the_specified_text()
{ {
// Arrange // Arrange
using var file = TempFile.Create(); using var file = TempFile.Create();
@@ -32,8 +32,7 @@ public class FilterSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
Json Json.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages") .GetProperty("messages")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("content").GetString()) .Select(j => j.GetProperty("content").GetString())
@@ -42,7 +41,7 @@ public class FilterSpecs
} }
[Fact] [Fact]
public async Task Messages_filtered_by_author_only_include_messages_sent_by_that_author() public async Task I_can_filter_the_export_to_only_include_messages_that_were_sent_by_the_specified_author()
{ {
// Arrange // Arrange
using var file = TempFile.Create(); using var file = TempFile.Create();
@@ -58,17 +57,16 @@ public class FilterSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
Json Json.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages") .GetProperty("messages")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("author").GetProperty("name").GetString()) .Select(j => j.GetProperty("author").GetProperty("name").GetString())
.Should() .Should()
.AllBe("Tyrrrz"); .AllBe("tyrrrz");
} }
[Fact] [Fact]
public async Task Messages_filtered_by_content_only_include_messages_that_have_that_content() public async Task I_can_filter_the_export_to_only_include_messages_that_contain_the_specified_content()
{ {
// Arrange // Arrange
using var file = TempFile.Create(); using var file = TempFile.Create();
@@ -84,8 +82,7 @@ public class FilterSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
Json Json.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages") .GetProperty("messages")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("content").GetString()) .Select(j => j.GetProperty("content").GetString())
@@ -94,7 +91,7 @@ public class FilterSpecs
} }
[Fact] [Fact]
public async Task Messages_filtered_by_pin_only_include_messages_that_have_been_pinned() public async Task I_can_filter_the_export_to_only_include_messages_that_have_been_pinned()
{ {
// Arrange // Arrange
using var file = TempFile.Create(); using var file = TempFile.Create();
@@ -110,8 +107,7 @@ public class FilterSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
Json Json.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages") .GetProperty("messages")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("content").GetString()) .Select(j => j.GetProperty("content").GetString())
@@ -120,7 +116,7 @@ public class FilterSpecs
} }
[Fact] [Fact]
public async Task Messages_filtered_by_mention_only_include_messages_that_have_that_mention() public async Task I_can_filter_the_export_to_only_include_messages_that_contain_the_specified_mention()
{ {
// Arrange // Arrange
using var file = TempFile.Create(); using var file = TempFile.Create();
@@ -136,12 +132,11 @@ public class FilterSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
Json Json.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages") .GetProperty("messages")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("content").GetString()) .Select(j => j.GetProperty("content").GetString())
.Should() .Should()
.ContainSingle("This has mention"); .ContainSingle("This has mention");
} }
} }
@@ -11,7 +11,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class HtmlAttachmentSpecs public class HtmlAttachmentSpecs
{ {
[Fact] [Fact]
public async Task Message_with_a_generic_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_generic_attachment()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -20,11 +20,7 @@ public class HtmlAttachmentSpecs
); );
// Assert // Assert
message.Text().Should().ContainAll( message.Text().Should().ContainAll("Generic file attachment", "Test.txt", "11 bytes");
"Generic file attachment",
"Test.txt",
"11 bytes"
);
message message
.QuerySelectorAll("a") .QuerySelectorAll("a")
@@ -36,7 +32,7 @@ public class HtmlAttachmentSpecs
} }
[Fact] [Fact]
public async Task Message_with_an_image_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_an_image_attachment()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -57,7 +53,7 @@ public class HtmlAttachmentSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_video_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_video_attachment()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/333 // https://github.com/Tyrrrz/DiscordChatExporter/issues/333
@@ -71,13 +67,15 @@ public class HtmlAttachmentSpecs
message.Text().Should().Contain("Video attachment"); message.Text().Should().Contain("Video attachment");
var videoUrl = message.QuerySelector("video source")?.GetAttribute("src"); var videoUrl = message.QuerySelector("video source")?.GetAttribute("src");
videoUrl.Should().Be( videoUrl
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4" .Should()
); .Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
);
} }
[Fact] [Fact]
public async Task Message_with_an_audio_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_an_audio_attachment()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/333 // https://github.com/Tyrrrz/DiscordChatExporter/issues/333
@@ -91,8 +89,10 @@ public class HtmlAttachmentSpecs
message.Text().Should().Contain("Audio attachment"); message.Text().Should().Contain("Audio attachment");
var audioUrl = message.QuerySelector("audio source")?.GetAttribute("src"); var audioUrl = message.QuerySelector("audio source")?.GetAttribute("src");
audioUrl.Should().Be( audioUrl
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3" .Should()
); .Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
);
} }
} }
@@ -2,7 +2,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using AngleSharp.Dom; using AngleSharp.Dom;
using DiscordChatExporter.Cli.Tests.Infra; using DiscordChatExporter.Cli.Tests.Infra;
using DiscordChatExporter.Core.Discord;
using FluentAssertions; using FluentAssertions;
using Xunit; using Xunit;
@@ -11,49 +10,38 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class HtmlContentSpecs public class HtmlContentSpecs
{ {
[Fact] [Fact]
public async Task Messages_are_exported_correctly() public async Task I_can_export_a_channel_in_the_HTML_format()
{ {
// Act // Act
var messages = await ExportWrapper.GetMessagesAsHtmlAsync(ChannelIds.DateRangeTestCases); var messages = await ExportWrapper.GetMessagesAsHtmlAsync(ChannelIds.DateRangeTestCases);
// Assert // Assert
messages.Select(e => e.GetAttribute("data-message-id")).Should().Equal( messages
"866674314627121232", .Select(e => e.GetAttribute("data-message-id"))
"866710679758045195", .Should()
"866732113319428096", .Equal(
"868490009366396958", "866674314627121232",
"868505966528835604", "866710679758045195",
"868505969821364245", "866732113319428096",
"868505973294268457", "868490009366396958",
"885169254029213696" "868505966528835604",
); "868505969821364245",
"868505973294268457",
"885169254029213696"
);
messages.SelectMany(e => e.Text()).Should().ContainInOrder( messages
"Hello world", .SelectMany(e => e.Text())
"Goodbye world", .Should()
"Foo bar", .ContainInOrder(
"Hurdle Durdle", "Hello world",
"One", "Goodbye world",
"Two", "Foo bar",
"Three", "Hurdle Durdle",
"Yeet" "One",
); "Two",
"Three",
"Yeet"
);
} }
}
[Fact]
public async Task Messages_cross_posted_from_other_guilds_are_rendered_with_the_server_tag()
{
// https://github.com/Tyrrrz/DiscordChatExporter/issues/633
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.ReplyTestCases,
Snowflake.Parse("1072165330853576876")
);
// Assert
message.Text().Should().Contain("This is a test message from an announcement channel on another server");
message.Text().Should().Contain("SERVER");
message.QuerySelector(".chatlog__reply-link").Should().BeNull();
}
}
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using AngleSharp.Dom; using AngleSharp.Dom;
using DiscordChatExporter.Cli.Tests.Infra; using DiscordChatExporter.Cli.Tests.Infra;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Utils.Extensions;
using FluentAssertions; using FluentAssertions;
using Xunit; using Xunit;
@@ -11,7 +12,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class HtmlEmbedSpecs public class HtmlEmbedSpecs
{ {
[Fact] [Fact]
public async Task Message_with_an_embed_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_rich_embed()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -20,19 +21,25 @@ public class HtmlEmbedSpecs
); );
// Assert // Assert
message.Text().Should().ContainAll( message
"Embed author", .Text()
"Embed title", .Should()
"Embed description", .ContainAll(
"Field 1", "Value 1", "Embed author",
"Field 2", "Value 2", "Embed title",
"Field 3", "Value 3", "Embed description",
"Embed footer" "Field 1",
); "Value 1",
"Field 2",
"Value 2",
"Field 3",
"Value 3",
"Embed footer"
);
} }
[Fact] [Fact]
public async Task Message_with_an_image_link_is_rendered_with_an_image_embed() public async Task I_can_export_a_channel_that_contains_a_message_with_an_image_embed()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/537 // https://github.com/Tyrrrz/DiscordChatExporter/issues/537
@@ -46,13 +53,14 @@ public class HtmlEmbedSpecs
message message
.QuerySelectorAll("img") .QuerySelectorAll("img")
.Select(e => e.GetAttribute("src")) .Select(e => e.GetAttribute("src"))
.Where(s => s?.EndsWith("i.redd.it/f8w05ja8s4e61.png") ?? false) .WhereNotNull()
.Where(s => s.EndsWith("f8w05ja8s4e61.png"))
.Should() .Should()
.ContainSingle(); .ContainSingle();
} }
[Fact] [Fact]
public async Task Message_with_an_image_link_and_nothing_else_is_rendered_without_text_content() public async Task I_can_export_a_channel_that_contains_a_message_with_an_image_embed_and_the_text_is_hidden_if_it_only_contains_the_image_link()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/682 // https://github.com/Tyrrrz/DiscordChatExporter/issues/682
@@ -68,7 +76,31 @@ public class HtmlEmbedSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_GIFV_link_is_rendered_with_a_video_embed() public async Task I_can_export_a_channel_that_contains_a_message_with_a_video_embed()
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases,
Snowflake.Parse("1083751036596002856")
);
// Assert
message
.QuerySelectorAll("source")
.Select(e => e.GetAttribute("src"))
.WhereNotNull()
.Where(
s =>
s.EndsWith(
"i_am_currently_feeling_slight_displeasure_of_what_you_have_just_sent_lqrem.mp4"
)
)
.Should()
.ContainSingle();
}
[Fact]
public async Task I_can_export_a_channel_that_contains_a_message_with_a_GIFV_embed()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -80,13 +112,14 @@ public class HtmlEmbedSpecs
message message
.QuerySelectorAll("source") .QuerySelectorAll("source")
.Select(e => e.GetAttribute("src")) .Select(e => e.GetAttribute("src"))
.Where(s => s?.EndsWith("media.tenor.com/DDAJeW6BQKkAAAPo/tooncasm-test-copy.mp4") ?? false) .WhereNotNull()
.Where(s => s.EndsWith("tooncasm-test-copy.mp4"))
.Should() .Should()
.ContainSingle(); .ContainSingle();
} }
[Fact] [Fact]
public async Task Message_with_a_GIFV_link_and_nothing_else_is_rendered_without_text_content() public async Task I_can_export_a_channel_that_contains_a_message_with_a_GIFV_embed_and_the_text_is_hidden_if_it_only_contains_the_video_link()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -100,7 +133,7 @@ public class HtmlEmbedSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_Spotify_track_link_is_rendered_with_a_track_embed() public async Task I_can_export_a_channel_that_contains_a_message_with_a_Spotify_track_embed()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/657 // https://github.com/Tyrrrz/DiscordChatExporter/issues/657
@@ -116,7 +149,7 @@ public class HtmlEmbedSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_YouTube_video_link_is_rendered_with_a_video_embed() public async Task I_can_export_a_channel_that_contains_a_message_with_a_YouTube_video_embed()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/570 // https://github.com/Tyrrrz/DiscordChatExporter/issues/570
@@ -132,7 +165,7 @@ public class HtmlEmbedSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_Twitter_post_link_with_multiple_images_is_rendered_as_a_single_embed() public async Task I_can_export_a_channel_that_contains_a_message_with_a_Twitter_post_embed_that_includes_multiple_images()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/695 // https://github.com/Tyrrrz/DiscordChatExporter/issues/695
@@ -158,7 +191,7 @@ public class HtmlEmbedSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_guild_invite_link_is_rendered_with_a_widget() public async Task I_can_export_a_channel_that_contains_a_message_with_a_guild_invite()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/649 // https://github.com/Tyrrrz/DiscordChatExporter/issues/649
@@ -171,4 +204,4 @@ public class HtmlEmbedSpecs
// Assert // Assert
message.Text().Should().Contain("DiscordChatExporter TestServer"); message.Text().Should().Contain("DiscordChatExporter TestServer");
} }
} }
@@ -15,7 +15,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class HtmlGroupingSpecs public class HtmlGroupingSpecs
{ {
[Fact] [Fact]
public async Task Messages_are_grouped_correctly() public async Task I_can_export_a_channel_and_the_messages_are_grouped_according_to_their_author_and_timestamps()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/152 // https://github.com/Tyrrrz/DiscordChatExporter/issues/152
@@ -32,8 +32,7 @@ public class HtmlGroupingSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
var messageGroups = Html var messageGroups = Html.Parse(await File.ReadAllTextAsync(file.Path))
.Parse(await File.ReadAllTextAsync(file.Path))
.QuerySelectorAll(".chatlog__message-group"); .QuerySelectorAll(".chatlog__message-group");
messageGroups.Should().HaveCount(2); messageGroups.Should().HaveCount(2);
@@ -59,12 +58,6 @@ public class HtmlGroupingSpecs
.QuerySelectorAll(".chatlog__content") .QuerySelectorAll(".chatlog__content")
.Select(e => e.Text()) .Select(e => e.Text())
.Should() .Should()
.ContainInOrder( .ContainInOrder("Eleventh", "Twelveth", "Thirteenth", "Fourteenth", "Fifteenth");
"Eleventh",
"Twelveth",
"Thirteenth",
"Fourteenth",
"Fifteenth"
);
} }
} }
@@ -1,8 +1,6 @@
using System; using System.Threading.Tasks;
using System.Threading.Tasks;
using AngleSharp.Dom; using AngleSharp.Dom;
using DiscordChatExporter.Cli.Tests.Infra; using DiscordChatExporter.Cli.Tests.Infra;
using DiscordChatExporter.Cli.Tests.Utils;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using FluentAssertions; using FluentAssertions;
using Xunit; using Xunit;
@@ -12,217 +10,130 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class HtmlMarkdownSpecs public class HtmlMarkdownSpecs
{ {
[Fact] [Fact]
public async Task Message_with_a_timestamp_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_timestamp_marker()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323136411078787")
);
try // Assert
{ message.Text().Should().Contain("Default timestamp: 2/12/2023 1:36 PM");
// Act message.InnerHtml.Should().Contain("Sunday, February 12, 2023 1:36 PM");
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323136411078787")
);
// Assert
message.Text().Should().Contain("Default timestamp: 02/12/2023 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
[Fact] [Fact]
public async Task Message_with_a_short_time_timestamp_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_timestamp_marker_in_the_short_format()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323205268967596")
);
try // Assert
{ message.Text().Should().Contain("Short time timestamp: 1:36 PM");
// Act message.InnerHtml.Should().Contain("Sunday, February 12, 2023 1:36 PM");
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323205268967596")
);
// Assert
message.Text().Should().Contain("Short time timestamp: 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
[Fact] [Fact]
public async Task Message_with_a_long_time_timestamp_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_timestamp_marker_in_the_long_format()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323235342139483")
);
try // Assert
{ message.Text().Should().Contain("Long time timestamp: 1:36:12 PM");
// Act message.InnerHtml.Should().Contain("Sunday, February 12, 2023 1:36 PM");
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323235342139483")
);
// Assert
message.Text().Should().Contain("Long time timestamp: 3:36:12 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
[Fact] [Fact]
public async Task Message_with_a_short_date_timestamp_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_timestamp_marker_in_the_short_date_format()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323326727634984")
);
try // Assert
{ message.Text().Should().Contain("Short date timestamp: 2/12/2023");
// Act message.InnerHtml.Should().Contain("Sunday, February 12, 2023 1:36 PM");
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323326727634984")
);
// Assert
message.Text().Should().Contain("Short date timestamp: 02/12/2023");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
[Fact] [Fact]
public async Task Message_with_a_long_date_timestamp_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_timestamp_marker_in_the_long_date_format()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323350731640863")
);
try // Assert
{ message.Text().Should().Contain("Long date timestamp: Sunday, February 12, 2023");
// Act message.InnerHtml.Should().Contain("Sunday, February 12, 2023 1:36 PM");
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323350731640863")
);
// Assert
message.Text().Should().Contain("Long date timestamp: February 12, 2023");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
[Fact] [Fact]
public async Task Message_with_a_full_timestamp_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_timestamp_marker_in_the_full_format()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323374379118593")
);
try // Assert
{ message.Text().Should().Contain("Full timestamp: Sunday, February 12, 2023 1:36 PM");
// Act message.InnerHtml.Should().Contain("Sunday, February 12, 2023 1:36 PM");
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323374379118593")
);
// Assert
message.Text().Should().Contain("Full timestamp: February 12, 2023 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
[Fact] [Fact]
public async Task Message_with_a_full_long_timestamp_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_timestamp_marker_in_the_full_long_format()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323409095376947")
);
try // Assert
{ message
// Act .Text()
var message = await ExportWrapper.GetMessageAsHtmlAsync( .Should()
ChannelIds.MarkdownTestCases, .Contain("Full long timestamp: Sunday, February 12, 2023 1:36:12 PM");
Snowflake.Parse("1074323409095376947") message.InnerHtml.Should().Contain("Sunday, February 12, 2023 1:36 PM");
);
// Assert
message.Text().Should().Contain("Full long timestamp: Sunday, February 12, 2023 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
[Fact] [Fact]
public async Task Message_with_a_relative_timestamp_is_rendered_as_the_default_timestamp() public async Task I_can_export_a_channel_that_contains_a_message_with_a_timestamp_marker_in_the_relative_format()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323436853285004")
);
try // Assert
{ message.Text().Should().Contain("Relative timestamp: 2/12/2023 1:36 PM");
// Act message.InnerHtml.Should().Contain("Sunday, February 12, 2023 1:36 PM");
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074323436853285004")
);
// Assert
message.Text().Should().Contain("Relative timestamp: 02/12/2023 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
[Fact] [Fact]
public async Task Message_with_an_invalid_timestamp_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_an_invalid_timestamp_marker()
{ {
// Date formatting code relies on the local time zone, so we need to set it to a fixed value // Act
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2)); var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074328534409019563")
);
try // Assert
{ message.Text().Should().Contain("Invalid timestamp: Invalid date");
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.MarkdownTestCases,
Snowflake.Parse("1074328534409019563")
);
// Assert
message.Text().Should().Contain("Invalid timestamp: Invalid date");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
} }
} }
@@ -10,7 +10,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class HtmlMentionSpecs public class HtmlMentionSpecs
{ {
[Fact] [Fact]
public async Task Message_with_a_user_mention_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_user_mention()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -20,11 +20,11 @@ public class HtmlMentionSpecs
// Assert // Assert
message.Text().Should().Contain("User mention: @Tyrrrz"); message.Text().Should().Contain("User mention: @Tyrrrz");
message.InnerHtml.Should().Contain("Tyrrrz#5447"); message.InnerHtml.Should().Contain("tyrrrz");
} }
[Fact] [Fact]
public async Task Message_with_a_text_channel_mention_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_text_channel_mention()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -37,7 +37,7 @@ public class HtmlMentionSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_voice_channel_mention_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_voice_channel_mention()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -50,7 +50,7 @@ public class HtmlMentionSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_role_mention_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_role_mention()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -61,4 +61,4 @@ public class HtmlMentionSpecs
// Assert // Assert
message.Text().Should().Contain("Role mention: @Role 1"); message.Text().Should().Contain("Role mention: @Role 1");
} }
} }
@@ -10,7 +10,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class HtmlReplySpecs public class HtmlReplySpecs
{ {
[Fact] [Fact]
public async Task Message_with_a_reply_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_that_replies_to_another_message()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -24,7 +24,7 @@ public class HtmlReplySpecs
} }
[Fact] [Fact]
public async Task Message_with_a_reply_to_a_deleted_message_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_that_replies_to_a_deleted_message()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/645 // https://github.com/Tyrrrz/DiscordChatExporter/issues/645
@@ -36,13 +36,15 @@ public class HtmlReplySpecs
// Assert // Assert
message.Text().Should().Contain("reply to deleted"); message.Text().Should().Contain("reply to deleted");
message.QuerySelector(".chatlog__reply-link")?.Text().Should().Contain( message
"Original message was deleted or could not be loaded." .QuerySelector(".chatlog__reply-link")
); ?.Text()
.Should()
.Contain("Original message was deleted or could not be loaded.");
} }
[Fact] [Fact]
public async Task Message_with_a_reply_to_an_empty_message_with_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_that_replies_to_an_empty_message_with_an_attachment()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/634 // https://github.com/Tyrrrz/DiscordChatExporter/issues/634
@@ -54,11 +56,15 @@ public class HtmlReplySpecs
// Assert // Assert
message.Text().Should().Contain("reply to attachment"); message.Text().Should().Contain("reply to attachment");
message.QuerySelector(".chatlog__reply-link")?.Text().Should().Contain("Click to see attachment"); message
.QuerySelector(".chatlog__reply-link")
?.Text()
.Should()
.Contain("Click to see attachment");
} }
[Fact] [Fact]
public async Task Message_with_a_reply_to_an_interaction_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_that_replies_to_an_interaction()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/569 // https://github.com/Tyrrrz/DiscordChatExporter/issues/569
@@ -71,4 +77,24 @@ public class HtmlReplySpecs
// Assert // Assert
message.Text().Should().Contain("used /poll"); message.Text().Should().Contain("used /poll");
} }
}
[Fact]
public async Task I_can_export_a_channel_that_contains_a_message_cross_posted_from_another_guild()
{
// https://github.com/Tyrrrz/DiscordChatExporter/issues/633
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.ReplyTestCases,
Snowflake.Parse("1072165330853576876")
);
// Assert
message
.Text()
.Should()
.Contain("This is a test message from an announcement channel on another server");
message.Text().Should().Contain("SERVER");
message.QuerySelector(".chatlog__reply-link").Should().BeNull();
}
}
@@ -9,7 +9,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class HtmlStickerSpecs public class HtmlStickerSpecs
{ {
[Fact] [Fact]
public async Task Message_with_a_PNG_based_sticker_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_PNG_sticker()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -23,7 +23,7 @@ public class HtmlStickerSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_Lottie_based_sticker_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_Lottie_sticker()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsHtmlAsync( var message = await ExportWrapper.GetMessageAsHtmlAsync(
@@ -32,7 +32,9 @@ public class HtmlStickerSpecs
); );
// Assert // Assert
var stickerUrl = message.QuerySelector("[title='Yikes'] [data-source]")?.GetAttribute("data-source"); var stickerUrl = message
.QuerySelector("[title='Yikes'] [data-source]")
?.GetAttribute("data-source");
stickerUrl.Should().Be("https://cdn.discordapp.com/stickers/816087132447178774.json"); stickerUrl.Should().Be("https://cdn.discordapp.com/stickers/816087132447178774.json");
} }
} }
@@ -10,7 +10,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class JsonAttachmentSpecs public class JsonAttachmentSpecs
{ {
[Fact] [Fact]
public async Task Message_with_a_generic_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_generic_attachment()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -23,15 +23,20 @@ public class JsonAttachmentSpecs
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray(); var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1); attachments.Should().HaveCount(1);
attachments.Single().GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt" attachments[0]
); .GetProperty("url")
attachments.Single().GetProperty("fileName").GetString().Should().Be("Test.txt"); .GetString()
attachments.Single().GetProperty("fileSizeBytes").GetInt64().Should().Be(11); .Should()
.Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
);
attachments[0].GetProperty("fileName").GetString().Should().Be("Test.txt");
attachments[0].GetProperty("fileSizeBytes").GetInt64().Should().Be(11);
} }
[Fact] [Fact]
public async Task Message_with_an_image_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_an_image_attachment()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -44,15 +49,20 @@ public class JsonAttachmentSpecs
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray(); var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1); attachments.Should().HaveCount(1);
attachments.Single().GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png" attachments[0]
); .GetProperty("url")
attachments.Single().GetProperty("fileName").GetString().Should().Be("bird-thumbnail.png"); .GetString()
attachments.Single().GetProperty("fileSizeBytes").GetInt64().Should().Be(466335); .Should()
.Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
);
attachments[0].GetProperty("fileName").GetString().Should().Be("bird-thumbnail.png");
attachments[0].GetProperty("fileSizeBytes").GetInt64().Should().Be(466335);
} }
[Fact] [Fact]
public async Task Message_with_a_video_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_video_attachment()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -65,15 +75,24 @@ public class JsonAttachmentSpecs
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray(); var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1); attachments.Should().HaveCount(1);
attachments.Single().GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4" attachments[0]
); .GetProperty("url")
attachments.Single().GetProperty("fileName").GetString().Should().Be("file_example_MP4_640_3MG.mp4"); .GetString()
attachments.Single().GetProperty("fileSizeBytes").GetInt64().Should().Be(3114374); .Should()
.Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
);
attachments[0]
.GetProperty("fileName")
.GetString()
.Should()
.Be("file_example_MP4_640_3MG.mp4");
attachments[0].GetProperty("fileSizeBytes").GetInt64().Should().Be(3114374);
} }
[Fact] [Fact]
public async Task Message_with_an_audio_attachment_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_an_audio_attachment()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -86,10 +105,15 @@ public class JsonAttachmentSpecs
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray(); var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1); attachments.Should().HaveCount(1);
attachments.Single().GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3" attachments[0]
); .GetProperty("url")
attachments.Single().GetProperty("fileName").GetString().Should().Be("file_example_MP3_1MG.mp3"); .GetString()
attachments.Single().GetProperty("fileSizeBytes").GetInt64().Should().Be(1087849); .Should()
.Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
);
attachments[0].GetProperty("fileName").GetString().Should().Be("file_example_MP3_1MG.mp3");
attachments[0].GetProperty("fileSizeBytes").GetInt64().Should().Be(1087849);
} }
} }
@@ -9,32 +9,38 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class JsonContentSpecs public class JsonContentSpecs
{ {
[Fact] [Fact]
public async Task Messages_are_exported_correctly() public async Task I_can_export_a_channel_in_the_JSON_format()
{ {
// Act // Act
var messages = await ExportWrapper.GetMessagesAsJsonAsync(ChannelIds.DateRangeTestCases); var messages = await ExportWrapper.GetMessagesAsJsonAsync(ChannelIds.DateRangeTestCases);
// Assert // Assert
messages.Select(j => j.GetProperty("id").GetString()).Should().Equal( messages
"866674314627121232", .Select(j => j.GetProperty("id").GetString())
"866710679758045195", .Should()
"866732113319428096", .Equal(
"868490009366396958", "866674314627121232",
"868505966528835604", "866710679758045195",
"868505969821364245", "866732113319428096",
"868505973294268457", "868490009366396958",
"885169254029213696" "868505966528835604",
); "868505969821364245",
"868505973294268457",
"885169254029213696"
);
messages.Select(j => j.GetProperty("content").GetString()).Should().Equal( messages
"Hello world", .Select(j => j.GetProperty("content").GetString())
"Goodbye world", .Should()
"Foo bar", .Equal(
"Hurdle Durdle", "Hello world",
"One", "Goodbye world",
"Two", "Foo bar",
"Three", "Hurdle Durdle",
"Yeet" "One",
); "Two",
"Three",
"Yeet"
);
} }
} }
@@ -10,7 +10,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class JsonEmbedSpecs public class JsonEmbedSpecs
{ {
[Fact] [Fact]
public async Task Message_with_an_embed_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_rich_embed()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -52,4 +52,4 @@ public class JsonEmbedSpecs
embedFields[2].GetProperty("value").GetString().Should().Be("Value 3"); embedFields[2].GetProperty("value").GetString().Should().Be("Value 3");
embedFields[2].GetProperty("isInline").GetBoolean().Should().BeTrue(); embedFields[2].GetProperty("isInline").GetBoolean().Should().BeTrue();
} }
} }
@@ -10,7 +10,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class JsonMentionSpecs public class JsonMentionSpecs
{ {
[Fact] [Fact]
public async Task Message_with_a_user_mention_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_user_mention()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -30,7 +30,7 @@ public class JsonMentionSpecs
} }
[Fact] [Fact]
public async Task Message_with_a_text_channel_mention_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_text_channel_mention()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -39,11 +39,15 @@ public class JsonMentionSpecs
); );
// Assert // Assert
message.GetProperty("content").GetString().Should().Be("Text channel mention: #mention-tests"); message
.GetProperty("content")
.GetString()
.Should()
.Be("Text channel mention: #mention-tests");
} }
[Fact] [Fact]
public async Task Message_with_a_voice_channel_mention_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_voice_channel_mention()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -52,11 +56,15 @@ public class JsonMentionSpecs
); );
// Assert // Assert
message.GetProperty("content").GetString().Should().Be("Voice channel mention: #general [voice]"); message
.GetProperty("content")
.GetString()
.Should()
.Be("Voice channel mention: #general [voice]");
} }
[Fact] [Fact]
public async Task Message_with_a_role_mention_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_role_mention()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -67,4 +75,4 @@ public class JsonMentionSpecs
// Assert // Assert
message.GetProperty("content").GetString().Should().Be("Role mention: @Role 1"); message.GetProperty("content").GetString().Should().Be("Role mention: @Role 1");
} }
} }
@@ -10,7 +10,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class JsonStickerSpecs public class JsonStickerSpecs
{ {
[Fact] [Fact]
public async Task Message_with_a_PNG_based_sticker_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_PNG_sticker()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -19,19 +19,20 @@ public class JsonStickerSpecs
); );
// Assert // Assert
var sticker = message var sticker = message.GetProperty("stickers").EnumerateArray().Single();
.GetProperty("stickers")
.EnumerateArray()
.Single();
sticker.GetProperty("id").GetString().Should().Be("904215665597120572"); sticker.GetProperty("id").GetString().Should().Be("904215665597120572");
sticker.GetProperty("name").GetString().Should().Be("rock"); sticker.GetProperty("name").GetString().Should().Be("rock");
sticker.GetProperty("format").GetString().Should().Be("Apng"); sticker.GetProperty("format").GetString().Should().Be("Apng");
sticker.GetProperty("sourceUrl").GetString().Should().Be("https://cdn.discordapp.com/stickers/904215665597120572.png"); sticker
.GetProperty("sourceUrl")
.GetString()
.Should()
.Be("https://cdn.discordapp.com/stickers/904215665597120572.png");
} }
[Fact] [Fact]
public async Task Message_with_a_Lottie_based_sticker_is_rendered_correctly() public async Task I_can_export_a_channel_that_contains_a_message_with_a_Lottie_sticker()
{ {
// Act // Act
var message = await ExportWrapper.GetMessageAsJsonAsync( var message = await ExportWrapper.GetMessageAsJsonAsync(
@@ -40,14 +41,15 @@ public class JsonStickerSpecs
); );
// Assert // Assert
var sticker = message var sticker = message.GetProperty("stickers").EnumerateArray().Single();
.GetProperty("stickers")
.EnumerateArray()
.Single();
sticker.GetProperty("id").GetString().Should().Be("816087132447178774"); sticker.GetProperty("id").GetString().Should().Be("816087132447178774");
sticker.GetProperty("name").GetString().Should().Be("Yikes"); sticker.GetProperty("name").GetString().Should().Be("Yikes");
sticker.GetProperty("format").GetString().Should().Be("Lottie"); sticker.GetProperty("format").GetString().Should().Be("Lottie");
sticker.GetProperty("sourceUrl").GetString().Should().Be("https://cdn.discordapp.com/stickers/816087132447178774.json"); sticker
.GetProperty("sourceUrl")
.GetString()
.Should()
.Be("https://cdn.discordapp.com/stickers/816087132447178774.json");
} }
} }
@@ -14,7 +14,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class PartitioningSpecs public class PartitioningSpecs
{ {
[Fact] [Fact]
public async Task Messages_partitioned_by_count_are_split_into_a_corresponding_number_of_files() public async Task I_can_export_a_channel_with_partitioning_based_on_message_count()
{ {
// Arrange // Arrange
using var dir = TempDir.Create(); using var dir = TempDir.Create();
@@ -31,13 +31,11 @@ public class PartitioningSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
Directory.EnumerateFiles(dir.Path, "output*") Directory.EnumerateFiles(dir.Path, "output*").Should().HaveCount(3);
.Should()
.HaveCount(3);
} }
[Fact] [Fact]
public async Task Messages_partitioned_by_file_size_are_split_into_a_corresponding_number_of_files() public async Task I_can_export_a_channel_with_partitioning_based_on_file_size()
{ {
// Arrange // Arrange
using var dir = TempDir.Create(); using var dir = TempDir.Create();
@@ -54,8 +52,6 @@ public class PartitioningSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
Directory.EnumerateFiles(dir.Path, "output*") Directory.EnumerateFiles(dir.Path, "output*").Should().HaveCount(8);
.Should()
.HaveCount(8);
} }
} }
@@ -8,22 +8,24 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class PlainTextContentSpecs public class PlainTextContentSpecs
{ {
[Fact] [Fact]
public async Task Messages_are_exported_correctly() public async Task I_can_export_a_channel_in_the_TXT_format()
{ {
// Act // Act
var document = await ExportWrapper.ExportAsPlainTextAsync(ChannelIds.DateRangeTestCases); var document = await ExportWrapper.ExportAsPlainTextAsync(ChannelIds.DateRangeTestCases);
// Assert // Assert
document.Should().ContainAll( document
"Tyrrrz#5447", .Should()
"Hello world", .ContainAll(
"Goodbye world", "tyrrrz",
"Foo bar", "Hello world",
"Hurdle Durdle", "Goodbye world",
"One", "Foo bar",
"Two", "Hurdle Durdle",
"Three", "One",
"Yeet" "Two",
); "Three",
"Yeet"
);
} }
} }
@@ -14,7 +14,7 @@ namespace DiscordChatExporter.Cli.Tests.Specs;
public class SelfContainedSpecs public class SelfContainedSpecs
{ {
[Fact] [Fact]
public async Task Messages_in_self_contained_export_only_reference_local_file_resources() public async Task I_can_export_a_channel_and_download_all_referenced_assets()
{ {
// Arrange // Arrange
using var dir = TempDir.Create(); using var dir = TempDir.Create();
@@ -31,8 +31,7 @@ public class SelfContainedSpecs
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(new FakeConsole());
// Assert // Assert
Html Html.Parse(await File.ReadAllTextAsync(filePath))
.Parse(await File.ReadAllTextAsync(filePath))
.QuerySelectorAll("body [src]") .QuerySelectorAll("body [src]")
.Select(e => e.GetAttribute("src")!) .Select(e => e.GetAttribute("src")!)
.Select(f => Path.GetFullPath(f, dir.Path)) .Select(f => Path.GetFullPath(f, dir.Path))
@@ -40,4 +39,4 @@ public class SelfContainedSpecs
.Should() .Should()
.BeTrue(); .BeTrue();
} }
} }
+1 -1
View File
@@ -8,4 +8,4 @@ internal static class Html
private static readonly IHtmlParser Parser = new HtmlParser(); private static readonly IHtmlParser Parser = new HtmlParser();
public static IHtmlDocument Parse(string source) => Parser.ParseDocument(source); public static IHtmlDocument Parse(string source) => Parser.ParseDocument(source);
} }
@@ -9,8 +9,7 @@ internal partial class TempDir : IDisposable
{ {
public string Path { get; } public string Path { get; }
public TempDir(string path) => public TempDir(string path) => Path = path;
Path = path;
public void Dispose() public void Dispose()
{ {
@@ -18,9 +17,7 @@ internal partial class TempDir : IDisposable
{ {
Directory.Delete(Path, true); Directory.Delete(Path, true);
} }
catch (DirectoryNotFoundException) catch (DirectoryNotFoundException) { }
{
}
} }
} }
@@ -29,7 +26,8 @@ internal partial class TempDir
public static TempDir Create() public static TempDir Create()
{ {
var dirPath = PathEx.Combine( var dirPath = PathEx.Combine(
PathEx.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Directory.GetCurrentDirectory(), PathEx.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? Directory.GetCurrentDirectory(),
"Temp", "Temp",
Guid.NewGuid().ToString() Guid.NewGuid().ToString()
); );
@@ -38,4 +36,4 @@ internal partial class TempDir
return new TempDir(dirPath); return new TempDir(dirPath);
} }
} }
@@ -9,8 +9,7 @@ internal partial class TempFile : IDisposable
{ {
public string Path { get; } public string Path { get; }
public TempFile(string path) => public TempFile(string path) => Path = path;
Path = path;
public void Dispose() public void Dispose()
{ {
@@ -18,9 +17,7 @@ internal partial class TempFile : IDisposable
{ {
File.Delete(Path); File.Delete(Path);
} }
catch (FileNotFoundException) catch (FileNotFoundException) { }
{
}
} }
} }
@@ -29,17 +26,15 @@ internal partial class TempFile
public static TempFile Create() public static TempFile Create()
{ {
var dirPath = PathEx.Combine( var dirPath = PathEx.Combine(
PathEx.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Directory.GetCurrentDirectory(), PathEx.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? Directory.GetCurrentDirectory(),
"Temp" "Temp"
); );
Directory.CreateDirectory(dirPath); Directory.CreateDirectory(dirPath);
var filePath = PathEx.Combine( var filePath = PathEx.Combine(dirPath, Guid.NewGuid() + ".tmp");
dirPath,
Guid.NewGuid() + ".tmp"
);
return new TempFile(filePath); return new TempFile(filePath);
} }
} }
@@ -1,14 +0,0 @@
using System;
using ReflectionMagic;
namespace DiscordChatExporter.Cli.Tests.Utils;
internal static class TimeZoneInfoEx
{
// https://stackoverflow.com/a/63700512/2205454
public static void SetLocal(TimeZoneInfo timeZone) =>
typeof(TimeZoneInfo).AsDynamicType().s_cachedData._localTimeZone = timeZone;
public static void SetLocal(TimeSpan offset) =>
SetLocal(TimeZoneInfo.CreateCustomTimeZone("test-tz", offset, "test-tz", "test-tz"));
}
+30 -28
View File
@@ -1,44 +1,46 @@
# -- Build # -- Build
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build # Specify the platform here so that we pull the SDK image matching the host platform,
# instead of the target platform specified during build by the `--platform` option.
# Use the .NET 8.0 preview because the `--arch` option is only available in that version.
# https://github.com/dotnet/dotnet-docker/issues/4388#issuecomment-1459038566
# TODO: switch images to Alpine once .NET 8.0 is released.
# Currently, the correct preview version is only available on Debian.
# https://github.com/dotnet/dotnet-docker/blob/main/samples/selecting-tags.md
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-preview AS build
# Expose the target architecture set by the `docker build --platform` option, so that
# we can build the assembly for the correct platform.
ARG TARGETARCH
WORKDIR /build WORKDIR /build
COPY favicon.ico ./ COPY favicon.ico .
COPY NuGet.config ./ COPY NuGet.config .
COPY Directory.Build.props ./ COPY Directory.Build.props .
COPY DiscordChatExporter.Core ./DiscordChatExporter.Core COPY DiscordChatExporter.Core DiscordChatExporter.Core
COPY DiscordChatExporter.Cli ./DiscordChatExporter.Cli COPY DiscordChatExporter.Cli DiscordChatExporter.Cli
# Publish a self-contained assembly so we can use a slimmer runtime image
RUN dotnet publish DiscordChatExporter.Cli \ RUN dotnet publish DiscordChatExporter.Cli \
--configuration Release \
-p:CSharpier_Bypass=true \
--self-contained \ --self-contained \
--use-current-runtime \ --use-current-runtime \
--configuration Release \ --arch $TARGETARCH \
--output ./publish --output publish/
# -- Run # -- Run
FROM mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine # Use `runtime-deps` instead of `runtime` because we have a self-contained assembly
FROM --platform=$TARGETPLATFORM mcr.microsoft.com/dotnet/runtime-deps:7.0 AS run
# Alpine dotnet image doesn't include timezone data, which is needed # Create a non-root user to run the app, so that the output files can be accessed by the host
# for certain date/time operations.
RUN apk add --no-cache tzdata
# Create a non-root user to run the app, so that the output files
# can be accessed by the host.
# https://github.com/Tyrrrz/DiscordChatExporter/issues/851 # https://github.com/Tyrrrz/DiscordChatExporter/issues/851
RUN adduser \ RUN adduser --disabled-password --no-create-home dce
--disabled-password \
--no-create-home \
dce
USER dce USER dce
COPY --from=build /build/publish /opt/dce # This directory is exposed to the user for mounting purposes, so it's important that it always
# stays the same for backwards compatibility.
# Need to keep this as /out for backwards compatibility with documentation.
# A lot of people have this directory mounted in their scripts files, so
# changing it would break existing workflows.
WORKDIR /out WORKDIR /out
# Add the app directory to PATH so that it's easier to debug using a shell COPY --from=build /build/publish /opt/dce
ENV PATH="$PATH:/opt/dce" ENTRYPOINT ["/opt/dce/DiscordChatExporter.Cli"]
ENTRYPOINT ["DiscordChatExporter.Cli"]
@@ -24,10 +24,28 @@ public abstract class DiscordCommandBase : ICommand
EnvironmentVariable = "DISCORD_TOKEN_BOT", EnvironmentVariable = "DISCORD_TOKEN_BOT",
Description = "This option doesn't do anything. Kept for backwards compatibility." Description = "This option doesn't do anything. Kept for backwards compatibility."
)] )]
public bool IsBotToken { get; init; } public bool IsBotToken { get; init; } = false;
private DiscordClient? _discordClient; private DiscordClient? _discordClient;
protected DiscordClient Discord => _discordClient ??= new DiscordClient(Token); protected DiscordClient Discord => _discordClient ??= new DiscordClient(Token);
public abstract ValueTask ExecuteAsync(IConsole console); public virtual ValueTask ExecuteAsync(IConsole console)
} {
#pragma warning disable CS0618
// Warn if the bot option is used
if (IsBotToken)
{
using (console.WithForegroundColor(ConsoleColor.DarkYellow))
{
console.Error.WriteLine(
"Warning: Option --bot is deprecated and should not be used. "
+ "The type of the provided token is now inferred automatically. "
+ "Please update your workflows as this option may be completely removed in a future version."
);
}
}
#pragma warning restore CS0618
return default;
}
}
@@ -1,12 +1,14 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Exceptions; using CliFx.Exceptions;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Utils.Extensions; using DiscordChatExporter.Cli.Utils.Extensions;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Discord.Data;
@@ -17,6 +19,7 @@ using DiscordChatExporter.Core.Exporting.Partitioning;
using DiscordChatExporter.Core.Utils; using DiscordChatExporter.Core.Utils;
using DiscordChatExporter.Core.Utils.Extensions; using DiscordChatExporter.Core.Utils.Extensions;
using Gress; using Gress;
using Spectre.Console;
namespace DiscordChatExporter.Cli.Commands.Base; namespace DiscordChatExporter.Cli.Commands.Base;
@@ -27,11 +30,10 @@ public abstract class ExportCommandBase : DiscordCommandBase
[CommandOption( [CommandOption(
"output", "output",
'o', 'o',
Description = Description = "Output file or directory path. "
"Output file or directory path. " + + "Directory path must end with a slash to avoid ambiguity. "
"If a directory is specified, file names will be generated automatically based on the channel names and other parameters. " + + "If a directory is specified, file names will be generated automatically. "
"Directory path should end with a slash to avoid ambiguity. " + + "Supports template tokens, see the documentation for more info."
"Supports template tokens, see the documentation for more info."
)] )]
public string OutputPath public string OutputPath
{ {
@@ -41,11 +43,7 @@ public abstract class ExportCommandBase : DiscordCommandBase
init => _outputPath = Path.GetFullPath(value); init => _outputPath = Path.GetFullPath(value);
} }
[CommandOption( [CommandOption("format", 'f', Description = "Export format.")]
"format",
'f',
Description = "Export format."
)]
public ExportFormat ExportFormat { get; init; } = ExportFormat.HtmlDark; public ExportFormat ExportFormat { get; init; } = ExportFormat.HtmlDark;
[CommandOption( [CommandOption(
@@ -63,14 +61,15 @@ public abstract class ExportCommandBase : DiscordCommandBase
[CommandOption( [CommandOption(
"partition", "partition",
'p', 'p',
Description = Description = "Split the output into partitions, each limited to the specified "
"Split output into partitions, each limited to this number of messages (e.g. '100') or file size (e.g. '10mb')." + "number of messages (e.g. '100') or file size (e.g. '10mb')."
)] )]
public PartitionLimit PartitionLimit { get; init; } = PartitionLimit.Null; public PartitionLimit PartitionLimit { get; init; } = PartitionLimit.Null;
[CommandOption( [CommandOption(
"filter", "filter",
Description = "Only include messages that satisfy this filter (e.g. 'from:foo#1234' or 'has:image')." Description = "Only include messages that satisfy this filter. "
+ "See the documentation for more info."
)] )]
public MessageFilter MessageFilter { get; init; } = MessageFilter.Null; public MessageFilter MessageFilter { get; init; } = MessageFilter.Null;
@@ -96,13 +95,14 @@ public abstract class ExportCommandBase : DiscordCommandBase
"reuse-media", "reuse-media",
Description = "Reuse previously downloaded assets to avoid redundant requests." Description = "Reuse previously downloaded assets to avoid redundant requests."
)] )]
public bool ShouldReuseAssets { get; init; } public bool ShouldReuseAssets { get; init; } = false;
private readonly string? _assetsDirPath; private readonly string? _assetsDirPath;
[CommandOption( [CommandOption(
"media-dir", "media-dir",
Description = "Download assets to this directory. If not specified, the asset directory path will be derived from the output path." Description = "Download assets to this directory. "
+ "If not specified, the asset directory path will be derived from the output path."
)] )]
public string? AssetsDirPath public string? AssetsDirPath
{ {
@@ -112,156 +112,172 @@ public abstract class ExportCommandBase : DiscordCommandBase
init => _assetsDirPath = value is not null ? Path.GetFullPath(value) : null; init => _assetsDirPath = value is not null ? Path.GetFullPath(value) : null;
} }
[Obsolete("This option doesn't do anything. Kept for backwards compatibility.")]
[CommandOption( [CommandOption(
"dateformat", "dateformat",
Description = "Format used when writing dates." Description = "This option doesn't do anything. Kept for backwards compatibility."
)] )]
public string DateFormat { get; init; } = "MM/dd/yyyy h:mm tt"; public string DateFormat { get; init; } = "MM/dd/yyyy h:mm tt";
[CommandOption("locale", Description = "Locale to use when formatting dates and numbers.")]
public string Locale { get; init; } = CultureInfo.CurrentCulture.Name;
[CommandOption("utc", Description = "Normalize all timestamps to UTC+0.")]
public bool IsUtcNormalizationEnabled { get; init; } = false;
[CommandOption( [CommandOption(
"fuck-russia", "fuck-russia",
Description = "Don't print the Support Ukraine message to the console." EnvironmentVariable = "FUCK_RUSSIA",
Description = "Don't print the Support Ukraine message to the console.",
// Use a converter to accept '1' as 'true' to reuse the existing environment variable
Converter = typeof(TruthyBooleanBindingConverter)
)] )]
public bool IsUkraineSupportMessageDisabled { get; init; } public bool IsUkraineSupportMessageDisabled { get; init; } = false;
private ChannelExporter? _channelExporter; private ChannelExporter? _channelExporter;
protected ChannelExporter Exporter => _channelExporter ??= new ChannelExporter(Discord); protected ChannelExporter Exporter => _channelExporter ??= new ChannelExporter(Discord);
protected async ValueTask ExecuteAsync(IConsole console, IReadOnlyList<Channel> channels) protected async ValueTask ExportAsync(IConsole console, IReadOnlyList<Channel> channels)
{ {
// Reuse assets option should only be used when the download assets option is set. // Asset reuse can only be enabled if the download assets option is set
// https://github.com/Tyrrrz/DiscordChatExporter/issues/425 // https://github.com/Tyrrrz/DiscordChatExporter/issues/425
if (ShouldReuseAssets && !ShouldDownloadAssets) if (ShouldReuseAssets && !ShouldDownloadAssets)
{ {
throw new CommandException( throw new CommandException("Option --reuse-media cannot be used without --media.");
"Option --reuse-media cannot be used without --media."
);
} }
// Assets directory should only be specified when the download assets option is set // Assets directory can only be specified if the download assets option is set
if (!string.IsNullOrWhiteSpace(AssetsDirPath) && !ShouldDownloadAssets) if (!string.IsNullOrWhiteSpace(AssetsDirPath) && !ShouldDownloadAssets)
{ {
throw new CommandException( throw new CommandException("Option --media-dir cannot be used without --media.");
"Option --media-dir cannot be used without --media."
);
} }
// Make sure the user does not try to export all channels into a single file. // Make sure the user does not try to export multiple channels into one file.
// Output path must either be a directory, or contain template tokens. // Output path must either be a directory or contain template tokens for this to work.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/799 // https://github.com/Tyrrrz/DiscordChatExporter/issues/799
// https://github.com/Tyrrrz/DiscordChatExporter/issues/917 // https://github.com/Tyrrrz/DiscordChatExporter/issues/917
var isValidOutputPath = var isValidOutputPath =
// Anything is valid when exporting a single channel // Anything is valid when exporting a single channel
channels.Count <= 1 || channels.Count <= 1
// When using template tokens, assume the user knows what they're doing // When using template tokens, assume the user knows what they're doing
OutputPath.Contains('%') || || OutputPath.Contains('%')
// Otherwise, require an existing directory or an unambiguous directory path // Otherwise, require an existing directory or an unambiguous directory path
Directory.Exists(OutputPath) || PathEx.IsDirectoryPath(OutputPath); || Directory.Exists(OutputPath)
|| PathEx.IsDirectoryPath(OutputPath);
if (!isValidOutputPath) if (!isValidOutputPath)
{ {
throw new CommandException( throw new CommandException(
"Attempted to export multiple channels, but the output path is neither a directory nor a template. " + "Attempted to export multiple channels, but the output path is neither a directory nor a template. "
"If the provided output path is meant to be treated as a directory, make sure it ends with a slash." + "If the provided output path is meant to be treated as a directory, make sure it ends with a slash."
); );
} }
var cancellationToken = console.RegisterCancellationHandler();
var errors = new ConcurrentDictionary<Channel, string>();
// Export // Export
var cancellationToken = console.RegisterCancellationHandler();
var errorsByChannel = new ConcurrentDictionary<Channel, string>();
await console.Output.WriteLineAsync($"Exporting {channels.Count} channel(s)..."); await console.Output.WriteLineAsync($"Exporting {channels.Count} channel(s)...");
await console.CreateProgressTicker().StartAsync(async progressContext => await console
{ .CreateProgressTicker()
await Parallel.ForEachAsync( .HideCompleted(
channels, // When exporting multiple channels in parallel, hide the completed tasks
new ParallelOptions // because it gets hard to visually parse them as they complete out of order.
{ // https://github.com/Tyrrrz/DiscordChatExporter/issues/1124
MaxDegreeOfParallelism = Math.Max(1, ParallelLimit), ParallelLimit > 1
CancellationToken = cancellationToken )
}, .StartAsync(async progressContext =>
async (channel, innerCancellationToken) => {
{ await Parallel.ForEachAsync(
try channels,
new ParallelOptions
{ {
await progressContext.StartTaskAsync( MaxDegreeOfParallelism = Math.Max(1, ParallelLimit),
$"{channel.Category.Name} / {channel.Name}", CancellationToken = cancellationToken
async progress => },
{ async (channel, innerCancellationToken) =>
var guild = await Discord.GetGuildAsync(channel.GuildId, innerCancellationToken);
var request = new ExportRequest(
guild,
channel,
OutputPath,
AssetsDirPath,
ExportFormat,
After,
Before,
PartitionLimit,
MessageFilter,
ShouldFormatMarkdown,
ShouldDownloadAssets,
ShouldReuseAssets,
DateFormat
);
await Exporter.ExportChannelAsync(
request,
progress.ToPercentageBased(),
innerCancellationToken
);
}
);
}
catch (DiscordChatExporterException ex) when (!ex.IsFatal)
{ {
errors[channel] = ex.Message; try
} {
} await progressContext.StartTaskAsync(
); channel.GetHierarchicalName(),
}); async progress =>
{
var guild = await Discord.GetGuildAsync(
channel.GuildId,
innerCancellationToken
);
// Print result var request = new ExportRequest(
guild,
channel,
OutputPath,
AssetsDirPath,
ExportFormat,
After,
Before,
PartitionLimit,
MessageFilter,
ShouldFormatMarkdown,
ShouldDownloadAssets,
ShouldReuseAssets,
Locale,
IsUtcNormalizationEnabled
);
await Exporter.ExportChannelAsync(
request,
progress.ToPercentageBased(),
innerCancellationToken
);
}
);
}
catch (DiscordChatExporterException ex) when (!ex.IsFatal)
{
errorsByChannel[channel] = ex.Message;
}
}
);
});
// Print the result
using (console.WithForegroundColor(ConsoleColor.White)) using (console.WithForegroundColor(ConsoleColor.White))
{ {
await console.Output.WriteLineAsync( await console.Output.WriteLineAsync(
$"Successfully exported {channels.Count - errors.Count} channel(s)." $"Successfully exported {channels.Count - errorsByChannel.Count} channel(s)."
); );
} }
// Print errors // Print errors
if (errors.Any()) if (errorsByChannel.Any())
{ {
await console.Output.WriteLineAsync(); await console.Output.WriteLineAsync();
using (console.WithForegroundColor(ConsoleColor.Red)) using (console.WithForegroundColor(ConsoleColor.Red))
{ {
await console.Output.WriteLineAsync( await console.Error.WriteLineAsync(
$"Failed to export {errors.Count} channel(s):" $"Failed to export {errorsByChannel.Count} channel(s):"
); );
} }
foreach (var (channel, error) in errors) foreach (var (channel, error) in errorsByChannel)
{ {
await console.Output.WriteAsync($"{channel.Category.Name} / {channel.Name}: "); await console.Error.WriteAsync($"{channel.GetHierarchicalName()}: ");
using (console.WithForegroundColor(ConsoleColor.Red)) using (console.WithForegroundColor(ConsoleColor.Red))
await console.Output.WriteLineAsync(error); await console.Error.WriteLineAsync(error);
} }
await console.Output.WriteLineAsync(); await console.Error.WriteLineAsync();
} }
// Fail the command only if ALL channels failed to export. // Fail the command only if ALL channels failed to export.
// Having some of the channels fail to export is expected. // If only some channels failed to export, it's okay.
if (errors.Count >= channels.Count) if (errorsByChannel.Count >= channels.Count)
{
throw new CommandException("Export failed."); throw new CommandException("Export failed.");
}
} }
protected async ValueTask ExecuteAsync(IConsole console, IReadOnlyList<Snowflake> channelIds) protected async ValueTask ExportAsync(IConsole console, IReadOnlyList<Snowflake> channelIds)
{ {
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
@@ -275,15 +291,15 @@ public abstract class ExportCommandBase : DiscordCommandBase
var channel = await Discord.GetChannelAsync(channelId, cancellationToken); var channel = await Discord.GetChannelAsync(channelId, cancellationToken);
// Unwrap categories // Unwrap categories
if (channel.Kind == ChannelKind.GuildCategory) if (channel.IsCategory)
{ {
var guildChannels = var guildChannels =
channelsByGuild.GetValueOrDefault(channel.GuildId) ?? channelsByGuild.GetValueOrDefault(channel.GuildId)
await Discord.GetGuildChannelsAsync(channel.GuildId, cancellationToken); ?? await Discord.GetGuildChannelsAsync(channel.GuildId, cancellationToken);
foreach (var guildChannel in guildChannels) foreach (var guildChannel in guildChannels)
{ {
if (guildChannel.Category.Id == channel.Id) if (guildChannel.Parent?.Id == channel.Id)
channels.Add(guildChannel); channels.Add(guildChannel);
} }
@@ -296,26 +312,44 @@ public abstract class ExportCommandBase : DiscordCommandBase
} }
} }
await ExecuteAsync(console, channels); await ExportAsync(console, channels);
} }
public override ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
{ {
// Support Ukraine callout // Support Ukraine callout
if (!IsUkraineSupportMessageDisabled) if (!IsUkraineSupportMessageDisabled)
{ {
console.Output.WriteLine("┌────────────────────────────────────────────────────────────────────┐"); console.Output.WriteLine(
console.Output.WriteLine("│ Thank you for supporting Ukraine <3 │"); "┌────────────────────────────────────────────────────────────────────┐"
console.Output.WriteLine("│ │"); );
console.Output.WriteLine("│ As Russia wages a genocidal war against my country, │"); console.Output.WriteLine(
console.Output.WriteLine("│ I'm grateful to everyone who continues to │"); "│ Thank you for supporting Ukraine <3 │"
console.Output.WriteLine("│ stand with Ukraine in our fight for freedom. │"); );
console.Output.WriteLine("│ │"); console.Output.WriteLine(
console.Output.WriteLine("│ Learn more: https://tyrrrz.me/ukraine │"); "│ │"
console.Output.WriteLine("└────────────────────────────────────────────────────────────────────┘"); );
console.Output.WriteLine(
"│ As Russia wages a genocidal war against my country, │"
);
console.Output.WriteLine(
"│ I'm grateful to everyone who continues to │"
);
console.Output.WriteLine(
"│ stand with Ukraine in our fight for freedom. │"
);
console.Output.WriteLine(
"│ │"
);
console.Output.WriteLine(
"│ Learn more: https://tyrrrz.me/ukraine │"
);
console.Output.WriteLine(
"└────────────────────────────────────────────────────────────────────┘"
);
console.Output.WriteLine(""); console.Output.WriteLine("");
} }
return default; await base.ExecuteAsync(console);
} }
} }
@@ -0,0 +1,22 @@
using System;
using CliFx.Extensibility;
using DiscordChatExporter.Cli.Commands.Shared;
namespace DiscordChatExporter.Cli.Commands.Converters;
internal class ThreadInclusionModeBindingConverter : BindingConverter<ThreadInclusionMode>
{
public override ThreadInclusionMode Convert(string? rawValue)
{
// Empty or unset value is treated as 'active' to match the previous behavior
if (string.IsNullOrWhiteSpace(rawValue))
return ThreadInclusionMode.Active;
// Boolean 'true' is treated as 'active', boolean 'false' is treated as 'none'
if (bool.TryParse(rawValue, out var boolValue))
return boolValue ? ThreadInclusionMode.Active : ThreadInclusionMode.None;
// Otherwise, fall back to regular enum parsing
return Enum.Parse<ThreadInclusionMode>(rawValue, true);
}
}
@@ -0,0 +1,21 @@
using System.Globalization;
using CliFx.Extensibility;
namespace DiscordChatExporter.Cli.Commands.Converters;
internal class TruthyBooleanBindingConverter : BindingConverter<bool>
{
public override bool Convert(string? rawValue)
{
// Empty or unset value is treated as 'true', to match the regular boolean behavior
if (string.IsNullOrWhiteSpace(rawValue))
return true;
// Number '1' is treated as 'true', other numbers are treated as 'false'
if (int.TryParse(rawValue, CultureInfo.InvariantCulture, out var intValue))
return intValue == 1;
// Otherwise, fall back to regular boolean parsing
return bool.Parse(rawValue);
}
}
@@ -6,6 +6,8 @@ using CliFx.Attributes;
using CliFx.Exceptions; using CliFx.Exceptions;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Exceptions;
@@ -13,24 +15,29 @@ using JsonExtensions.Reading;
namespace DiscordChatExporter.Cli.Commands; namespace DiscordChatExporter.Cli.Commands;
[Command("exportall", Description = "Export all accessible channels.")] [Command("exportall", Description = "Exports all accessible channels.")]
public class ExportAllCommand : ExportCommandBase public class ExportAllCommand : ExportCommandBase
{ {
[CommandOption( [CommandOption("include-dm", Description = "Include direct message channels.")]
"include-dm",
Description = "Include direct message channels."
)]
public bool IncludeDirectChannels { get; init; } = true; public bool IncludeDirectChannels { get; init; } = true;
[CommandOption( [CommandOption("include-guilds", Description = "Include guild channels.")]
"include-guilds",
Description = "Include guild channels."
)]
public bool IncludeGuildChannels { get; init; } = true; public bool IncludeGuildChannels { get; init; } = true;
[CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption(
"include-threads",
Description = "Which types of threads should be included.",
Converter = typeof(ThreadInclusionModeBindingConverter)
)]
public ThreadInclusionMode ThreadInclusionMode { get; init; } = ThreadInclusionMode.None;
[CommandOption( [CommandOption(
"data-package", "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." 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; init; } public string? DataPackageFilePath { get; init; }
@@ -48,10 +55,36 @@ public class ExportAllCommand : ExportCommandBase
await foreach (var guild in Discord.GetUserGuildsAsync(cancellationToken)) await foreach (var guild in Discord.GetUserGuildsAsync(cancellationToken))
{ {
await foreach (var channel in Discord.GetGuildChannelsAsync(guild.Id, cancellationToken)) // Regular channels
await foreach (
var channel in Discord.GetGuildChannelsAsync(guild.Id, cancellationToken)
)
{ {
if (channel.IsCategory)
continue;
if (!IncludeVoiceChannels && channel.IsVoice)
continue;
channels.Add(channel); channels.Add(channel);
} }
// Threads
if (ThreadInclusionMode != ThreadInclusionMode.None)
{
await foreach (
var thread in Discord.GetGuildThreadsAsync(
guild.Id,
ThreadInclusionMode == ThreadInclusionMode.All,
Before,
After,
cancellationToken
)
)
{
channels.Add(thread);
}
}
} }
} }
// Pull from the data package // Pull from the data package
@@ -62,7 +95,7 @@ public class ExportAllCommand : ExportCommandBase
var entry = archive.GetEntry("messages/index.json"); var entry = archive.GetEntry("messages/index.json");
if (entry is null) if (entry is null)
throw new CommandException("Cannot find channel index inside the data package."); throw new CommandException("Could not find channel index inside the data package.");
await using var stream = entry.Open(); await using var stream = entry.Open();
using var document = await JsonDocument.ParseAsync(stream, default, cancellationToken); using var document = await JsonDocument.ParseAsync(stream, default, cancellationToken);
@@ -76,7 +109,9 @@ public class ExportAllCommand : ExportCommandBase
if (channelName is null) if (channelName is null)
continue; continue;
await console.Output.WriteLineAsync($"Fetching channel '{channelName}' ({channelId})..."); await console.Output.WriteLineAsync(
$"Fetching channel '{channelName}' ({channelId})..."
);
try try
{ {
@@ -85,17 +120,25 @@ public class ExportAllCommand : ExportCommandBase
} }
catch (DiscordChatExporterException) catch (DiscordChatExporterException)
{ {
await console.Output.WriteLineAsync($"Channel '{channelName}' ({channelId}) is inaccessible."); await console.Error.WriteLineAsync(
$"Channel '{channelName}' ({channelId}) is inaccessible."
);
} }
} }
} }
// Filter out unwanted channels // Filter out unwanted channels
if (!IncludeDirectChannels) if (!IncludeDirectChannels)
channels.RemoveAll(c => c.Kind.IsDirect()); channels.RemoveAll(c => c.IsDirect);
if (!IncludeGuildChannels) if (!IncludeGuildChannels)
channels.RemoveAll(c => c.Kind.IsGuild()); channels.RemoveAll(c => c.IsGuild);
if (!IncludeVoiceChannels)
channels.RemoveAll(c => c.IsVoice);
if (ThreadInclusionMode == ThreadInclusionMode.None)
channels.RemoveAll(c => c.IsThread);
if (ThreadInclusionMode != ThreadInclusionMode.All)
channels.RemoveAll(c => c is { IsThread: true, IsArchived: true });
await base.ExecuteAsync(console, channels); await ExportAsync(console, channels);
} }
} }
@@ -7,20 +7,21 @@ using DiscordChatExporter.Core.Discord;
namespace DiscordChatExporter.Cli.Commands; namespace DiscordChatExporter.Cli.Commands;
[Command("export", Description = "Export one or multiple channels.")] [Command("export", Description = "Exports one or multiple channels.")]
public class ExportChannelsCommand : ExportCommandBase public class ExportChannelsCommand : ExportCommandBase
{ {
// TODO: change this to plural (breaking change) // TODO: change this to plural (breaking change)
[CommandOption( [CommandOption(
"channel", "channel",
'c', 'c',
Description = "Channel ID(s). If provided with category IDs, all channels inside those categories will be exported." Description = "Channel ID(s). "
+ "If provided with category ID(s), all channels inside those categories will be exported."
)] )]
public required IReadOnlyList<Snowflake> ChannelIds { get; init; } public required IReadOnlyList<Snowflake> ChannelIds { get; init; }
public override async ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
{ {
await base.ExecuteAsync(console); await base.ExecuteAsync(console);
await base.ExecuteAsync(console, ChannelIds); await ExportAsync(console, ChannelIds);
} }
} }
@@ -7,7 +7,7 @@ using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands; namespace DiscordChatExporter.Cli.Commands;
[Command("exportdm", Description = "Export all direct message channels.")] [Command("exportdm", Description = "Exports all direct message channels.")]
public class ExportDirectMessagesCommand : ExportCommandBase public class ExportDirectMessagesCommand : ExportCommandBase
{ {
public override async ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
@@ -17,8 +17,11 @@ public class ExportDirectMessagesCommand : ExportCommandBase
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
await console.Output.WriteLineAsync("Fetching channels..."); await console.Output.WriteLineAsync("Fetching channels...");
var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken); var channels = await Discord.GetGuildChannelsAsync(
Guild.DirectMessages.Id,
cancellationToken
);
await base.ExecuteAsync(console, channels); await ExportAsync(console, channels);
} }
} }
@@ -1,36 +1,69 @@
using System.Linq; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands; namespace DiscordChatExporter.Cli.Commands;
[Command("exportguild", Description = "Export all channels within specified guild.")] [Command("exportguild", Description = "Exports all channels within the specified guild.")]
public class ExportGuildCommand : ExportCommandBase public class ExportGuildCommand : ExportCommandBase
{ {
[CommandOption( [CommandOption("guild", 'g', Description = "Guild ID.")]
"guild",
'g',
Description = "Guild ID."
)]
public required Snowflake GuildId { get; init; } public required Snowflake GuildId { get; init; }
[CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption(
"include-threads",
Description = "Which types of threads should be included.",
Converter = typeof(ThreadInclusionModeBindingConverter)
)]
public ThreadInclusionMode ThreadInclusionMode { get; init; } = ThreadInclusionMode.None;
public override async ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
{ {
await base.ExecuteAsync(console); await base.ExecuteAsync(console);
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
var channels = new List<Channel>();
await console.Output.WriteLineAsync("Fetching channels..."); await console.Output.WriteLineAsync("Fetching channels...");
var channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken)) // Regular channels
.Where(c => c.Kind != ChannelKind.GuildCategory) await foreach (var channel in Discord.GetGuildChannelsAsync(GuildId, cancellationToken))
.ToArray(); {
if (channel.IsCategory)
continue;
await base.ExecuteAsync(console, channels); if (!IncludeVoiceChannels && channel.IsVoice)
continue;
channels.Add(channel);
}
// Threads
if (ThreadInclusionMode != ThreadInclusionMode.None)
{
await foreach (
var thread in Discord.GetGuildThreadsAsync(
GuildId,
ThreadInclusionMode == ThreadInclusionMode.All,
Before,
After,
cancellationToken
)
)
{
channels.Add(thread);
}
}
await ExportAsync(console, channels);
} }
} }
@@ -4,6 +4,8 @@ using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Utils.Extensions; using DiscordChatExporter.Core.Utils.Extensions;
@@ -13,37 +15,101 @@ namespace DiscordChatExporter.Cli.Commands;
[Command("channels", Description = "Get the list of channels in a guild.")] [Command("channels", Description = "Get the list of channels in a guild.")]
public class GetChannelsCommand : DiscordCommandBase public class GetChannelsCommand : DiscordCommandBase
{ {
[CommandOption( [CommandOption("guild", 'g', Description = "Guild ID.")]
"guild",
'g',
Description = "Guild ID."
)]
public required Snowflake GuildId { get; init; } public required Snowflake GuildId { get; init; }
[CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption(
"include-threads",
Description = "Which types of threads should be included.",
Converter = typeof(ThreadInclusionModeBindingConverter)
)]
public ThreadInclusionMode ThreadInclusionMode { get; init; } = ThreadInclusionMode.None;
public override async ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
{ {
await base.ExecuteAsync(console);
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
var channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken)) var channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken))
.Where(c => c.Kind != ChannelKind.GuildCategory) .Where(c => !c.IsCategory)
.OrderBy(c => c.Category.Position) .Where(c => IncludeVoiceChannels || !c.IsVoice)
.OrderBy(c => c.Parent?.Position)
.ThenBy(c => c.Name) .ThenBy(c => c.Name)
.ToArray(); .ToArray();
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()
: Array.Empty<Channel>();
foreach (var channel in channels) foreach (var channel in channels)
{ {
// Channel ID // Channel ID
await console.Output.WriteAsync( await console.Output.WriteAsync(
channel.Id.ToString().PadRight(18, ' ') channel.Id.ToString().PadRight(channelIdMaxLength, ' ')
); );
// Separator // Separator
using (console.WithForegroundColor(ConsoleColor.DarkGray)) using (console.WithForegroundColor(ConsoleColor.DarkGray))
await console.Output.WriteAsync(" | "); await console.Output.WriteAsync(" | ");
// Channel category / name // Channel name
using (console.WithForegroundColor(ConsoleColor.White)) using (console.WithForegroundColor(ConsoleColor.White))
await console.Output.WriteLineAsync($"{channel.Category.Name} / {channel.Name}"); 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();
foreach (var channelThread in channelThreads)
{
// 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"
);
}
} }
} }
} }
@@ -9,33 +9,41 @@ using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands; namespace DiscordChatExporter.Cli.Commands;
[Command("dm", Description = "Get the list of direct message channels.")] [Command("dm", Description = "Gets the list of all direct message channels.")]
public class GetDirectChannelsCommand : DiscordCommandBase public class GetDirectChannelsCommand : DiscordCommandBase
{ {
public override async ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
{ {
await base.ExecuteAsync(console);
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
var channels = (await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken)) var channels = (
.Where(c => c.Kind != ChannelKind.GuildCategory) await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken)
)
.OrderByDescending(c => c.LastMessageId) .OrderByDescending(c => c.LastMessageId)
.ThenBy(c => c.Name) .ThenBy(c => c.Name)
.ToArray(); .ToArray();
var channelIdMaxLength = channels
.Select(c => c.Id.ToString().Length)
.OrderDescending()
.FirstOrDefault();
foreach (var channel in channels) foreach (var channel in channels)
{ {
// Channel ID // Channel ID
await console.Output.WriteAsync( await console.Output.WriteAsync(
channel.Id.ToString().PadRight(18, ' ') channel.Id.ToString().PadRight(channelIdMaxLength, ' ')
); );
// Separator // Separator
using (console.WithForegroundColor(ConsoleColor.DarkGray)) using (console.WithForegroundColor(ConsoleColor.DarkGray))
await console.Output.WriteAsync(" | "); await console.Output.WriteAsync(" | ");
// Channel category / name // Channel name
using (console.WithForegroundColor(ConsoleColor.White)) using (console.WithForegroundColor(ConsoleColor.White))
await console.Output.WriteLineAsync($"{channel.Category.Name} / {channel.Name}"); await console.Output.WriteLineAsync(channel.GetHierarchicalName());
} }
} }
} }
@@ -9,24 +9,30 @@ using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands; namespace DiscordChatExporter.Cli.Commands;
[Command("guilds", Description = "Get the list of accessible guilds.")] [Command("guilds", Description = "Gets the list of accessible guilds.")]
public class GetGuildsCommand : DiscordCommandBase public class GetGuildsCommand : DiscordCommandBase
{ {
public override async ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
{ {
await base.ExecuteAsync(console);
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
var guilds = (await Discord.GetUserGuildsAsync(cancellationToken)) var guilds = (await Discord.GetUserGuildsAsync(cancellationToken))
// Show direct messages first // Show direct messages first
.OrderByDescending(g => g.Id == Guild.DirectMessages.Id) .OrderByDescending(g => g.Id == Guild.DirectMessages.Id)
.ThenBy(g => g.Name); .ThenBy(g => g.Name)
.ToArray();
var guildIdMaxLength = guilds
.Select(g => g.Id.ToString().Length)
.OrderDescending()
.FirstOrDefault();
foreach (var guild in guilds) foreach (var guild in guilds)
{ {
// Guild ID // Guild ID
await console.Output.WriteAsync( await console.Output.WriteAsync(guild.Id.ToString().PadRight(guildIdMaxLength, ' '));
guild.Id.ToString().PadRight(18, ' ')
);
// Separator // Separator
using (console.WithForegroundColor(ConsoleColor.DarkGray)) using (console.WithForegroundColor(ConsoleColor.DarkGray))
@@ -37,4 +43,4 @@ public class GetGuildsCommand : DiscordCommandBase
await console.Output.WriteLineAsync(guild.Name); await console.Output.WriteLineAsync(guild.Name);
} }
} }
} }
@@ -6,7 +6,7 @@ using CliFx.Infrastructure;
namespace DiscordChatExporter.Cli.Commands; namespace DiscordChatExporter.Cli.Commands;
[Command("guide", Description = "Explains how to obtain token, guild or channel ID.")] [Command("guide", Description = "Explains how to obtain the token, guild or channel ID.")]
public class GuideCommand : ICommand public class GuideCommand : ICommand
{ {
public ValueTask ExecuteAsync(IConsole console) public ValueTask ExecuteAsync(IConsole console)
@@ -15,14 +15,18 @@ public class GuideCommand : ICommand
using (console.WithForegroundColor(ConsoleColor.White)) using (console.WithForegroundColor(ConsoleColor.White))
console.Output.WriteLine("To get user token:"); console.Output.WriteLine("To get user token:");
console.Output.WriteLine(" * Automating user accounts is technically against TOS — USE AT YOUR OWN RISK!"); console.Output.WriteLine(
" * Automating user accounts is technically against TOS — USE AT YOUR OWN RISK!"
);
console.Output.WriteLine(" 1. Open Discord in your web browser and login"); console.Output.WriteLine(" 1. Open Discord in your web browser and login");
console.Output.WriteLine(" 2. Open any server or direct message channel"); console.Output.WriteLine(" 2. Open any server or direct message channel");
console.Output.WriteLine(" 3. Press Ctrl+Shift+I to show developer tools"); console.Output.WriteLine(" 3. Press Ctrl+Shift+I to show developer tools");
console.Output.WriteLine(" 4. Navigate to the Network tab"); console.Output.WriteLine(" 4. Navigate to the Network tab");
console.Output.WriteLine(" 5. Press Ctrl+R to reload"); console.Output.WriteLine(" 5. Press Ctrl+R to reload");
console.Output.WriteLine(" 6. Switch between random channels to trigger network requests"); console.Output.WriteLine(" 6. Switch between random channels to trigger network requests");
console.Output.WriteLine(" 7. Search for a request containing \"messages?limit=50\" or similar"); console.Output.WriteLine(
" 7. Search for a request containing \"messages?limit=50\" or similar"
);
console.Output.WriteLine(" 8. Select the Headers tab on the right"); console.Output.WriteLine(" 8. Select the Headers tab on the right");
console.Output.WriteLine(" 9. Scroll down to the Request Headers section"); console.Output.WriteLine(" 9. Scroll down to the Request Headers section");
console.Output.WriteLine(" 10. Copy the value of the \"authorization\" header"); console.Output.WriteLine(" 10. Copy the value of the \"authorization\" header");
@@ -36,38 +40,34 @@ public class GuideCommand : ICommand
console.Output.WriteLine(" 2. Open your application's settings"); console.Output.WriteLine(" 2. Open your application's settings");
console.Output.WriteLine(" 3. Navigate to the Bot section on the left"); console.Output.WriteLine(" 3. Navigate to the Bot section on the left");
console.Output.WriteLine(" 4. Under Token click Copy"); console.Output.WriteLine(" 4. Under Token click Copy");
console.Output.WriteLine(" * Your bot needs to have Message Content Intent enabled to read messages"); console.Output.WriteLine(
" * Your bot needs to have Message Content Intent enabled to read messages"
);
console.Output.WriteLine(); console.Output.WriteLine();
// Guild or channel ID // Guild or channel ID
using (console.WithForegroundColor(ConsoleColor.White)) using (console.WithForegroundColor(ConsoleColor.White))
console.Output.WriteLine("To get guild ID or guild channel ID:"); console.Output.WriteLine("To get guild ID or channel ID:");
console.Output.WriteLine(" 1. Open Discord"); console.Output.WriteLine(" 1. Open Discord");
console.Output.WriteLine(" 2. Open Settings"); console.Output.WriteLine(" 2. Open Settings");
console.Output.WriteLine(" 3. Go to Advanced section"); console.Output.WriteLine(" 3. Go to Advanced section");
console.Output.WriteLine(" 4. Enable Developer Mode"); console.Output.WriteLine(" 4. Enable Developer Mode");
console.Output.WriteLine(" 5. Right click on the desired guild or channel and click Copy ID"); console.Output.WriteLine(
console.Output.WriteLine(); " 5. Right-click on the desired guild or channel and click Copy Server ID or Copy Channel ID"
);
// Direct message channel ID
using (console.WithForegroundColor(ConsoleColor.White))
console.Output.WriteLine("To get direct message channel ID:");
console.Output.WriteLine(" 1. Open Discord");
console.Output.WriteLine(" 2. Open the desired direct message channel");
console.Output.WriteLine(" 3. Press Ctrl+Shift+I to show developer tools");
console.Output.WriteLine(" 4. Navigate to the Console tab");
console.Output.WriteLine(" 5. Type \"window.location.href\" and press Enter");
console.Output.WriteLine(" 6. Copy the first long sequence of numbers inside the URL");
console.Output.WriteLine(); console.Output.WriteLine();
// Docs link // Docs link
using (console.WithForegroundColor(ConsoleColor.White)) using (console.WithForegroundColor(ConsoleColor.White))
console.Output.WriteLine("If you have questions or issues, please refer to the documentation:"); console.Output.WriteLine(
"If you have questions or issues, please refer to the documentation:"
);
using (console.WithForegroundColor(ConsoleColor.DarkCyan)) using (console.WithForegroundColor(ConsoleColor.DarkCyan))
console.Output.WriteLine("https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs"); console.Output.WriteLine(
"https://github.com/Tyrrrz/DiscordChatExporter/blob/master/.docs"
);
return default; return default;
} }
} }
@@ -0,0 +1,8 @@
namespace DiscordChatExporter.Cli.Commands.Shared;
public enum ThreadInclusionMode
{
None,
Active,
All
}
@@ -6,11 +6,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CliFx" Version="2.3.1" /> <PackageReference Include="CliFx" Version="2.3.4" />
<PackageReference Include="FuckRussia" Version="1.0.1" PrivateAssets="all" /> <PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
<PackageReference Include="Spectre.Console" Version="0.46.0" /> <PackageReference Include="Deorcify" Version="1.0.2" PrivateAssets="all" />
<PackageReference Include="Gress" Version="2.0.1" /> <PackageReference Include="DotnetRuntimeBootstrapper" Version="2.5.1" PrivateAssets="all" />
<PackageReference Include="DotnetRuntimeBootstrapper" Version="2.4.0" PrivateAssets="all" /> <PackageReference Include="Gress" Version="2.1.1" />
<PackageReference Include="Spectre.Console" Version="0.47.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+1 -4
View File
@@ -1,6 +1,3 @@
using CliFx; using CliFx;
return await new CliApplicationBuilder() return await new CliApplicationBuilder().AddCommandsFromThisAssembly().Build().RunAsync(args);
.AddCommandsFromThisAssembly()
.Build()
.RunAsync(args);
@@ -8,34 +8,38 @@ namespace DiscordChatExporter.Cli.Utils.Extensions;
internal static class ConsoleExtensions internal static class ConsoleExtensions
{ {
public static IAnsiConsole CreateAnsiConsole(this IConsole console) => public static IAnsiConsole CreateAnsiConsole(this IConsole console) =>
AnsiConsole.Create(new AnsiConsoleSettings AnsiConsole.Create(
{ new AnsiConsoleSettings
Ansi = AnsiSupport.Detect, {
ColorSystem = ColorSystemSupport.Detect, Ansi = AnsiSupport.Detect,
Out = new AnsiConsoleOutput(console.Output) ColorSystem = ColorSystemSupport.Detect,
}); Out = new AnsiConsoleOutput(console.Output)
}
public static Progress CreateProgressTicker(this IConsole console) => console
.CreateAnsiConsole()
.Progress()
.AutoClear(false)
.AutoRefresh(true)
.HideCompleted(false)
.Columns(
new TaskDescriptionColumn {Alignment = Justify.Left},
new ProgressBarColumn(),
new PercentageColumn()
); );
public static Progress CreateProgressTicker(this IConsole console) =>
console
.CreateAnsiConsole()
.Progress()
.AutoClear(false)
.AutoRefresh(true)
.HideCompleted(false)
.Columns(
new TaskDescriptionColumn { Alignment = Justify.Left },
new ProgressBarColumn(),
new PercentageColumn()
);
public static async ValueTask StartTaskAsync( public static async ValueTask StartTaskAsync(
this ProgressContext progressContext, this ProgressContext progressContext,
string description, string description,
Func<ProgressTask, ValueTask> performOperationAsync) Func<ProgressTask, ValueTask> performOperationAsync
)
{ {
var progressTask = progressContext.AddTask( var progressTask = progressContext.AddTask(
// Don't recognize random square brackets as style tags // Don't recognize random square brackets as style tags
Markup.Escape(description), Markup.Escape(description),
new ProgressTaskSettings {MaxValue = 1} new ProgressTaskSettings { MaxValue = 1 }
); );
try try
@@ -48,4 +52,4 @@ internal static class ConsoleExtensions
progressTask.StopTask(); progressTask.StopTask();
} }
} }
} }
@@ -15,30 +15,31 @@ public partial record Attachment(
string? Description, string? Description,
int? Width, int? Width,
int? Height, int? Height,
FileSize FileSize) : IHasId FileSize FileSize
) : IHasId
{ {
public string FileExtension => Path.GetExtension(FileName); public string FileExtension => Path.GetExtension(FileName);
public bool IsImage => public bool IsImage =>
string.Equals(FileExtension, ".jpg", StringComparison.OrdinalIgnoreCase) || string.Equals(FileExtension, ".jpg", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".jpeg", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".jpeg", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".png", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".png", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".gif", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".gif", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".bmp", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".bmp", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".webp", StringComparison.OrdinalIgnoreCase); || string.Equals(FileExtension, ".webp", StringComparison.OrdinalIgnoreCase);
public bool IsVideo => public bool IsVideo =>
string.Equals(FileExtension, ".gifv", StringComparison.OrdinalIgnoreCase) || string.Equals(FileExtension, ".gifv", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".mp4", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".mp4", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".webm", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".webm", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".mov", StringComparison.OrdinalIgnoreCase); || string.Equals(FileExtension, ".mov", StringComparison.OrdinalIgnoreCase);
public bool IsAudio => public bool IsAudio =>
string.Equals(FileExtension, ".mp3", StringComparison.OrdinalIgnoreCase) || string.Equals(FileExtension, ".mp3", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".wav", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".wav", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".ogg", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".ogg", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".flac", StringComparison.OrdinalIgnoreCase) || || string.Equals(FileExtension, ".flac", StringComparison.OrdinalIgnoreCase)
string.Equals(FileExtension, ".m4a", StringComparison.OrdinalIgnoreCase); || string.Equals(FileExtension, ".m4a", StringComparison.OrdinalIgnoreCase);
public bool IsSpoiler => FileName.StartsWith("SPOILER_", StringComparison.Ordinal); public bool IsSpoiler => FileName.StartsWith("SPOILER_", StringComparison.Ordinal);
} }
@@ -57,4 +58,4 @@ public partial record Attachment
return new Attachment(id, url, fileName, description, width, height, fileSize); return new Attachment(id, url, fileName, description, width, height, fileSize);
} }
} }
@@ -1,4 +1,5 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using DiscordChatExporter.Core.Discord.Data.Common; using DiscordChatExporter.Core.Discord.Data.Common;
using DiscordChatExporter.Core.Utils.Extensions; using DiscordChatExporter.Core.Utils.Extensions;
@@ -11,83 +12,104 @@ public partial record Channel(
Snowflake Id, Snowflake Id,
ChannelKind Kind, ChannelKind Kind,
Snowflake GuildId, Snowflake GuildId,
ChannelCategory Category, Channel? Parent,
string Name, string Name,
int? Position, int? Position,
string? IconUrl, string? IconUrl,
string? Topic, string? Topic,
Snowflake? LastMessageId) : IHasId bool IsArchived,
Snowflake? LastMessageId
) : IHasId
{ {
public bool IsDirect => Kind is ChannelKind.DirectTextChat or ChannelKind.DirectGroupTextChat;
public bool IsGuild => !IsDirect;
public bool IsCategory => Kind == ChannelKind.GuildCategory;
public bool IsVoice => Kind is ChannelKind.GuildVoiceChat or ChannelKind.GuildStageVoice; public bool IsVoice => Kind is ChannelKind.GuildVoiceChat or ChannelKind.GuildStageVoice;
public bool IsThread =>
Kind
is ChannelKind.GuildNewsThread
or ChannelKind.GuildPublicThread
or ChannelKind.GuildPrivateThread;
public bool IsEmpty => LastMessageId is null;
public IEnumerable<Channel> GetParents()
{
var current = Parent;
while (current is not null)
{
yield return current;
current = current.Parent;
}
}
public Channel? TryGetRootParent() => GetParents().LastOrDefault();
public string GetHierarchicalName() =>
string.Join(" / ", GetParents().Reverse().Select(c => c.Name).Append(Name));
public bool MayHaveMessagesAfter(Snowflake messageId) => !IsEmpty && messageId < LastMessageId;
public bool MayHaveMessagesBefore(Snowflake messageId) => !IsEmpty && messageId > Id;
} }
public partial record Channel public partial record Channel
{ {
private static ChannelCategory GetFallbackCategory(ChannelKind channelKind) => new( public static Channel Parse(JsonElement json, Channel? parent = null, int? positionHint = null)
Snowflake.Zero,
channelKind switch
{
ChannelKind.GuildTextChat => "Text",
ChannelKind.DirectTextChat => "Private",
ChannelKind.DirectGroupTextChat => "Group",
ChannelKind.GuildNews => "News",
_ => "Default"
},
null
);
public static Channel Parse(JsonElement json, ChannelCategory? categoryHint = null, int? positionHint = null)
{ {
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse); var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
var kind = (ChannelKind)json.GetProperty("type").GetInt32(); var kind = (ChannelKind)json.GetProperty("type").GetInt32();
var guildId = var guildId =
json.GetPropertyOrNull("guild_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse) ?? json.GetPropertyOrNull("guild_id")
Guild.DirectMessages.Id; ?.GetNonWhiteSpaceStringOrNull()
?.Pipe(Snowflake.Parse) ?? Guild.DirectMessages.Id;
var category = categoryHint ?? GetFallbackCategory(kind);
var name = var name =
// Guild channel // Guild channel
json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull() ?? json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull()
// DM channel // DM channel
json.GetPropertyOrNull("recipients")? ?? json.GetPropertyOrNull("recipients")
.EnumerateArrayOrNull()? ?.EnumerateArrayOrNull()
.Select(User.Parse) ?.Select(User.Parse)
.Select(u => u.Name) .Select(u => u.DisplayName)
.Pipe(s => string.Join(", ", s)) ?? .Pipe(s => string.Join(", ", s))
// Fallback // Fallback
id.ToString(); ?? id.ToString();
var position = var position = positionHint ?? json.GetPropertyOrNull("position")?.GetInt32OrNull();
positionHint ??
json.GetPropertyOrNull("position")?.GetInt32OrNull();
// Icons can only be set for group DM channels // Icons can only be set for group DM channels
var iconUrl = json var iconUrl = json.GetPropertyOrNull("icon")
.GetPropertyOrNull("icon")? ?.GetNonWhiteSpaceStringOrNull()
.GetNonWhiteSpaceStringOrNull()? ?.Pipe(h => ImageCdn.GetChannelIconUrl(id, h));
.Pipe(h => ImageCdn.GetChannelIconUrl(id, h));
var topic = json.GetPropertyOrNull("topic")?.GetStringOrNull(); var topic = json.GetPropertyOrNull("topic")?.GetStringOrNull();
var lastMessageId = json var isArchived =
.GetPropertyOrNull("last_message_id")? json.GetPropertyOrNull("thread_metadata")
.GetNonWhiteSpaceStringOrNull()? ?.GetPropertyOrNull("archived")
.Pipe(Snowflake.Parse); ?.GetBooleanOrNull() ?? false;
var lastMessageId = json.GetPropertyOrNull("last_message_id")
?.GetNonWhiteSpaceStringOrNull()
?.Pipe(Snowflake.Parse);
return new Channel( return new Channel(
id, id,
kind, kind,
guildId, guildId,
category, parent,
name, name,
position, position,
iconUrl, iconUrl,
topic, topic,
isArchived,
lastMessageId lastMessageId
); );
} }
} }
@@ -1,24 +0,0 @@
using System.Text.Json;
using DiscordChatExporter.Core.Discord.Data.Common;
using DiscordChatExporter.Core.Utils.Extensions;
using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data;
public record ChannelCategory(Snowflake Id, string Name, int? Position) : IHasId
{
public static ChannelCategory Parse(JsonElement json, int? positionHint = null)
{
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
var name =
json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull() ??
id.ToString();
var position =
positionHint ??
json.GetPropertyOrNull("position")?.GetInt32OrNull();
return new ChannelCategory(id, name, position);
}
}
@@ -1,7 +1,6 @@
namespace DiscordChatExporter.Core.Discord.Data; namespace DiscordChatExporter.Core.Discord.Data;
// https://discord.com/developers/docs/resources/channel#channel-object-channel-types // https://discord.com/developers/docs/resources/channel#channel-object-channel-types
// Order of enum fields needs to match the order in the docs.
public enum ChannelKind public enum ChannelKind
{ {
GuildTextChat = 0, GuildTextChat = 0,
@@ -17,12 +16,3 @@ public enum ChannelKind
GuildDirectory = 14, GuildDirectory = 14,
GuildForum = 15 GuildForum = 15
} }
public static class ChannelKindExtensions
{
public static bool IsDirect(this ChannelKind kind) =>
kind is ChannelKind.DirectTextChat or ChannelKind.DirectGroupTextChat;
public static bool IsGuild(this ChannelKind kind) =>
!kind.IsDirect();
}
@@ -41,10 +41,13 @@ public readonly partial record struct FileSize(long TotalBytes)
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public override string ToString() => public override string ToString() =>
string.Create(CultureInfo.InvariantCulture, $"{GetLargestWholeNumberValue():0.##} {GetLargestWholeNumberSymbol()}"); string.Create(
CultureInfo.InvariantCulture,
$"{GetLargestWholeNumberValue():0.##} {GetLargestWholeNumberSymbol()}"
);
} }
public partial record struct FileSize public partial record struct FileSize
{ {
public static FileSize FromBytes(long bytes) => new(bytes); public static FileSize FromBytes(long bytes) => new(bytes);
} }
@@ -3,4 +3,4 @@
public interface IHasId public interface IHasId
{ {
Snowflake Id { get; } Snowflake Id { get; }
} }
@@ -1,12 +0,0 @@
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Discord.Data.Common;
public class IdBasedEqualityComparer : IEqualityComparer<IHasId>
{
public static IdBasedEqualityComparer Instance { get; } = new();
public bool Equals(IHasId? x, IHasId? y) => x?.Id == y?.Id;
public int GetHashCode(IHasId obj) => obj.Id.GetHashCode();
}
@@ -1,4 +1,5 @@
using System; using System;
using System.Globalization;
using System.Linq; using System.Linq;
using DiscordChatExporter.Core.Utils.Extensions; using DiscordChatExporter.Core.Utils.Extensions;
@@ -21,7 +22,7 @@ public static class ImageCdn
var twemojiId = string.Join( var twemojiId = string.Join(
"-", "-",
filteredRunes.Select(r => r.Value.ToString("x")) filteredRunes.Select(r => r.Value.ToString("x", CultureInfo.InvariantCulture))
); );
return $"https://cdn.jsdelivr.net/gh/twitter/twemoji@latest/assets/svg/{twemojiId}.svg"; return $"https://cdn.jsdelivr.net/gh/twitter/twemoji@latest/assets/svg/{twemojiId}.svg";
@@ -47,14 +48,19 @@ public static class ImageCdn
? $"https://cdn.discordapp.com/avatars/{userId}/{avatarHash}.gif?size={size}" ? $"https://cdn.discordapp.com/avatars/{userId}/{avatarHash}.gif?size={size}"
: $"https://cdn.discordapp.com/avatars/{userId}/{avatarHash}.png?size={size}"; : $"https://cdn.discordapp.com/avatars/{userId}/{avatarHash}.png?size={size}";
public static string GetFallbackUserAvatarUrl(int discriminator) => public static string GetFallbackUserAvatarUrl(int index = 0) =>
$"https://cdn.discordapp.com/embed/avatars/{discriminator % 5}.png"; $"https://cdn.discordapp.com/embed/avatars/{index}.png";
public static string GetMemberAvatarUrl(Snowflake guildId, Snowflake userId, string avatarHash, int size = 512) => public static string GetMemberAvatarUrl(
Snowflake guildId,
Snowflake userId,
string avatarHash,
int size = 512
) =>
avatarHash.StartsWith("a_", StringComparison.Ordinal) avatarHash.StartsWith("a_", StringComparison.Ordinal)
? $"https://cdn.discordapp.com/guilds/{guildId}/users/{userId}/avatars/{avatarHash}.gif?size={size}" ? $"https://cdn.discordapp.com/guilds/{guildId}/users/{userId}/avatars/{avatarHash}.gif?size={size}"
: $"https://cdn.discordapp.com/guilds/{guildId}/users/{userId}/avatars/{avatarHash}.png?size={size}"; : $"https://cdn.discordapp.com/guilds/{guildId}/users/{userId}/avatars/{avatarHash}.png?size={size}";
public static string GetStickerUrl(Snowflake stickerId, string format = "png") => public static string GetStickerUrl(Snowflake stickerId, string format = "png") =>
$"https://cdn.discordapp.com/stickers/{stickerId}.{format}"; $"https://cdn.discordapp.com/stickers/{stickerId}.{format}";
} }
@@ -21,7 +21,8 @@ public partial record Embed(
EmbedImage? Thumbnail, EmbedImage? Thumbnail,
IReadOnlyList<EmbedImage> Images, IReadOnlyList<EmbedImage> Images,
EmbedVideo? Video, EmbedVideo? Video,
EmbedFooter? Footer) EmbedFooter? Footer
)
{ {
// Embeds can only have one image according to the API model, // Embeds can only have one image according to the API model,
// but the client can render multiple images in some cases. // but the client can render multiple images in some cases.
@@ -41,24 +42,25 @@ public partial record Embed
var title = json.GetPropertyOrNull("title")?.GetStringOrNull(); var title = json.GetPropertyOrNull("title")?.GetStringOrNull();
var kind = var kind =
json.GetPropertyOrNull("type")?.GetStringOrNull()?.ParseEnumOrNull<EmbedKind>() ?? json.GetPropertyOrNull("type")?.GetStringOrNull()?.ParseEnumOrNull<EmbedKind>()
EmbedKind.Rich; ?? EmbedKind.Rich;
var url = json.GetPropertyOrNull("url")?.GetNonWhiteSpaceStringOrNull(); var url = json.GetPropertyOrNull("url")?.GetNonWhiteSpaceStringOrNull();
var timestamp = json.GetPropertyOrNull("timestamp")?.GetDateTimeOffset(); var timestamp = json.GetPropertyOrNull("timestamp")?.GetDateTimeOffsetOrNull();
var color = json var color = json.GetPropertyOrNull("color")
.GetPropertyOrNull("color")? ?.GetInt32OrNull()
.GetInt32OrNull()? ?.Pipe(System.Drawing.Color.FromArgb)
.Pipe(System.Drawing.Color.FromArgb)
.ResetAlpha(); .ResetAlpha();
var author = json.GetPropertyOrNull("author")?.Pipe(EmbedAuthor.Parse); var author = json.GetPropertyOrNull("author")?.Pipe(EmbedAuthor.Parse);
var description = json.GetPropertyOrNull("description")?.GetStringOrNull(); var description = json.GetPropertyOrNull("description")?.GetStringOrNull();
var fields = var fields =
json.GetPropertyOrNull("fields")?.EnumerateArrayOrNull()?.Select(EmbedField.Parse).ToArray() ?? json.GetPropertyOrNull("fields")
Array.Empty<EmbedField>(); ?.EnumerateArrayOrNull()
?.Select(EmbedField.Parse)
.ToArray() ?? Array.Empty<EmbedField>();
var thumbnail = json.GetPropertyOrNull("thumbnail")?.Pipe(EmbedImage.Parse); var thumbnail = json.GetPropertyOrNull("thumbnail")?.Pipe(EmbedImage.Parse);
@@ -70,8 +72,10 @@ public partial record Embed
// with this by merging related embeds at the end of the message parsing process. // with this by merging related embeds at the end of the message parsing process.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/695 // https://github.com/Tyrrrz/DiscordChatExporter/issues/695
var images = var images =
json.GetPropertyOrNull("image")?.Pipe(EmbedImage.Parse).Enumerate().ToArray() ?? json.GetPropertyOrNull("image")
Array.Empty<EmbedImage>(); ?.Pipe(EmbedImage.Parse)
.ToSingletonEnumerable()
.ToArray() ?? Array.Empty<EmbedImage>();
var video = json.GetPropertyOrNull("video")?.Pipe(EmbedVideo.Parse); var video = json.GetPropertyOrNull("video")?.Pipe(EmbedVideo.Parse);
@@ -92,4 +96,4 @@ public partial record Embed
footer footer
); );
} }
} }
@@ -4,11 +4,7 @@ using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data.Embeds; namespace DiscordChatExporter.Core.Discord.Data.Embeds;
// https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure // https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure
public record EmbedAuthor( public record EmbedAuthor(string? Name, string? Url, string? IconUrl, string? IconProxyUrl)
string? Name,
string? Url,
string? IconUrl,
string? IconProxyUrl)
{ {
public static EmbedAuthor Parse(JsonElement json) public static EmbedAuthor Parse(JsonElement json)
{ {
@@ -19,4 +15,4 @@ public record EmbedAuthor(
return new EmbedAuthor(name, url, iconUrl, iconProxyUrl); return new EmbedAuthor(name, url, iconUrl, iconProxyUrl);
} }
} }
@@ -4,10 +4,7 @@ using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data.Embeds; namespace DiscordChatExporter.Core.Discord.Data.Embeds;
// https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure // https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
public record EmbedField( public record EmbedField(string Name, string Value, bool IsInline)
string Name,
string Value,
bool IsInline)
{ {
public static EmbedField Parse(JsonElement json) public static EmbedField Parse(JsonElement json)
{ {
@@ -17,4 +14,4 @@ public record EmbedField(
return new EmbedField(name, value, isInline); return new EmbedField(name, value, isInline);
} }
} }
@@ -4,10 +4,7 @@ using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data.Embeds; namespace DiscordChatExporter.Core.Discord.Data.Embeds;
// https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure // https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure
public record EmbedFooter( public record EmbedFooter(string Text, string? IconUrl, string? IconProxyUrl)
string Text,
string? IconUrl,
string? IconProxyUrl)
{ {
public static EmbedFooter Parse(JsonElement json) public static EmbedFooter Parse(JsonElement json)
{ {
@@ -17,4 +14,4 @@ public record EmbedFooter(
return new EmbedFooter(text, iconUrl, iconProxyUrl); return new EmbedFooter(text, iconUrl, iconProxyUrl);
} }
} }
@@ -4,11 +4,7 @@ using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data.Embeds; namespace DiscordChatExporter.Core.Discord.Data.Embeds;
// https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure // https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure
public record EmbedImage( public record EmbedImage(string? Url, string? ProxyUrl, int? Width, int? Height)
string? Url,
string? ProxyUrl,
int? Width,
int? Height)
{ {
public static EmbedImage Parse(JsonElement json) public static EmbedImage Parse(JsonElement json)
{ {
@@ -19,4 +15,4 @@ public record EmbedImage(
return new EmbedImage(url, proxyUrl, width, height); return new EmbedImage(url, proxyUrl, width, height);
} }
} }
@@ -8,4 +8,4 @@ public enum EmbedKind
Video, Video,
Gifv, Gifv,
Link Link
} }
@@ -4,11 +4,7 @@ using System.Text.Json;
namespace DiscordChatExporter.Core.Discord.Data.Embeds; namespace DiscordChatExporter.Core.Discord.Data.Embeds;
// https://discord.com/developers/docs/resources/channel#embed-object-embed-video-structure // https://discord.com/developers/docs/resources/channel#embed-object-embed-video-structure
public record EmbedVideo( public record EmbedVideo(string? Url, string? ProxyUrl, int? Width, int? Height)
string? Url,
string? ProxyUrl,
int? Width,
int? Height)
{ {
public static EmbedVideo Parse(JsonElement json) public static EmbedVideo Parse(JsonElement json)
{ {
@@ -19,4 +15,4 @@ public record EmbedVideo(
return new EmbedVideo(url, proxyUrl, width, height); return new EmbedVideo(url, proxyUrl, width, height);
} }
} }
@@ -12,7 +12,9 @@ public partial record SpotifyTrackEmbedProjection
private static string? TryParseTrackId(string embedUrl) private static string? TryParseTrackId(string embedUrl)
{ {
// https://open.spotify.com/track/1LHZMWefF9502NPfArRfvP?si=3efac6ce9be04f0a // https://open.spotify.com/track/1LHZMWefF9502NPfArRfvP?si=3efac6ce9be04f0a
var trackId = Regex.Match(embedUrl, @"spotify\.com/track/(.*?)(?:\?|&|/|$)").Groups[1].Value; var trackId = Regex.Match(embedUrl, @"spotify\.com/track/(.*?)(?:\?|&|/|$)").Groups[
1
].Value;
if (!string.IsNullOrWhiteSpace(trackId)) if (!string.IsNullOrWhiteSpace(trackId))
return trackId; return trackId;
@@ -33,4 +35,4 @@ public partial record SpotifyTrackEmbedProjection
return new SpotifyTrackEmbedProjection(trackId); return new SpotifyTrackEmbedProjection(trackId);
} }
} }
@@ -1,6 +1,4 @@
using System.Text.RegularExpressions; namespace DiscordChatExporter.Core.Discord.Data.Embeds;
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
public partial record YouTubeVideoEmbedProjection(string VideoId) public partial record YouTubeVideoEmbedProjection(string VideoId)
{ {
@@ -9,37 +7,6 @@ public partial record YouTubeVideoEmbedProjection(string VideoId)
public partial record YouTubeVideoEmbedProjection public partial record YouTubeVideoEmbedProjection
{ {
// Adapted from YoutubeExplode
// https://github.com/Tyrrrz/YoutubeExplode/blob/bc700b631bd105d0be208a88116347034bdca88b/YoutubeExplode/Videos/VideoId.cs#L40-L62
private static string? TryParseVideoId(string embedUrl)
{
// Regular URL
// https://www.youtube.com/watch?v=yIVRs6YSbOM
var regularMatch = Regex.Match(embedUrl, @"youtube\..+?/watch.*?v=(.*?)(?:&|/|$)").Groups[1].Value;
if (!string.IsNullOrWhiteSpace(regularMatch))
return regularMatch;
// Short URL
// https://youtu.be/yIVRs6YSbOM
var shortMatch = Regex.Match(embedUrl, @"youtu\.be/(.*?)(?:\?|&|/|$)").Groups[1].Value;
if (!string.IsNullOrWhiteSpace(shortMatch))
return shortMatch;
// Embed URL
// https://www.youtube.com/embed/yIVRs6YSbOM
var embedMatch = Regex.Match(embedUrl, @"youtube\..+?/embed/(.*?)(?:\?|&|/|$)").Groups[1].Value;
if (!string.IsNullOrWhiteSpace(embedMatch))
return embedMatch;
// Shorts URL
// https://www.youtube.com/shorts/sKL1vjP0tIo
var shortsMatch = Regex.Match(embedUrl, @"youtube\..+?/shorts/(.*?)(?:\?|&|/|$)").Groups[1].Value;
if (!string.IsNullOrWhiteSpace(shortsMatch))
return shortsMatch;
return null;
}
public static YouTubeVideoEmbedProjection? TryResolve(Embed embed) public static YouTubeVideoEmbedProjection? TryResolve(Embed embed)
{ {
if (embed.Kind != EmbedKind.Video) if (embed.Kind != EmbedKind.Video)
@@ -48,10 +15,10 @@ public partial record YouTubeVideoEmbedProjection
if (string.IsNullOrWhiteSpace(embed.Url)) if (string.IsNullOrWhiteSpace(embed.Url))
return null; return null;
var videoId = TryParseVideoId(embed.Url); var videoId = YoutubeExplode.Videos.VideoId.TryParse(embed.Url);
if (string.IsNullOrWhiteSpace(videoId)) if (videoId is null)
return null; return null;
return new YouTubeVideoEmbedProjection(videoId); return new YouTubeVideoEmbedProjection(videoId);
} }
} }
+11 -15
View File
@@ -14,12 +14,11 @@ public partial record Emoji(
// Name of a custom emoji (e.g. LUL) or actual representation of a standard emoji (e.g. 🙂) // Name of a custom emoji (e.g. LUL) or actual representation of a standard emoji (e.g. 🙂)
string Name, string Name,
bool IsAnimated, bool IsAnimated,
string ImageUrl) string ImageUrl
)
{ {
// Name of a custom emoji (e.g. LUL) or name of a standard emoji (e.g. slight_smile) // Name of a custom emoji (e.g. LUL) or name of a standard emoji (e.g. slight_smile)
public string Code => Id is not null public string Code => Id is not null ? Name : EmojiIndex.TryGetCode(Name) ?? Name;
? Name
: EmojiIndex.TryGetCode(Name) ?? Name;
} }
public partial record Emoji public partial record Emoji
@@ -34,25 +33,22 @@ public partial record Emoji
if (!string.IsNullOrWhiteSpace(name)) if (!string.IsNullOrWhiteSpace(name))
return ImageCdn.GetStandardEmojiUrl(name); return ImageCdn.GetStandardEmojiUrl(name);
// Either ID or name should be set throw new InvalidOperationException("Either the emoji ID or name should be provided.");
throw new ApplicationException("Emoji has neither ID nor name set.");
} }
public static Emoji Parse(JsonElement json) public static Emoji Parse(JsonElement json)
{ {
var id = json.GetPropertyOrNull("id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse); var id = json.GetPropertyOrNull("id")
?.GetNonWhiteSpaceStringOrNull()
?.Pipe(Snowflake.Parse);
// Names may be missing on custom emoji within reactions // Names may be missing on custom emoji within reactions
var name = json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull() ?? "Unknown Emoji"; var name =
json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull() ?? "Unknown Emoji";
var isAnimated = json.GetPropertyOrNull("animated")?.GetBooleanOrNull() ?? false; var isAnimated = json.GetPropertyOrNull("animated")?.GetBooleanOrNull() ?? false;
var imageUrl = GetImageUrl(id, name, isAnimated); var imageUrl = GetImageUrl(id, name, isAnimated);
return new Emoji( return new Emoji(id, name, isAnimated, imageUrl);
id,
name,
isAnimated,
imageUrl
);
} }
} }
File diff suppressed because it is too large Load Diff
+12 -12
View File
@@ -6,14 +6,16 @@ using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data; namespace DiscordChatExporter.Core.Discord.Data;
// https://discord.com/developers/docs/resources/guild#guild-object // https://discord.com/developers/docs/resources/guild#guild-object
public record Guild(Snowflake Id, string Name, string IconUrl) : IHasId public partial record Guild(Snowflake Id, string Name, string IconUrl) : IHasId
{
public bool IsDirect => Id == DirectMessages.Id;
}
public partial record Guild
{ {
// Direct messages are encapsulated within a special pseudo-guild for consistency // Direct messages are encapsulated within a special pseudo-guild for consistency
public static Guild DirectMessages { get; } = new( public static Guild DirectMessages { get; } =
Snowflake.Zero, new(Snowflake.Zero, "Direct Messages", ImageCdn.GetFallbackUserAvatarUrl());
"Direct Messages",
ImageCdn.GetFallbackUserAvatarUrl(0)
);
public static Guild Parse(JsonElement json) public static Guild Parse(JsonElement json)
{ {
@@ -21,12 +23,10 @@ public record Guild(Snowflake Id, string Name, string IconUrl) : IHasId
var name = json.GetProperty("name").GetNonNullString(); var name = json.GetProperty("name").GetNonNullString();
var iconUrl = var iconUrl =
json json.GetPropertyOrNull("icon")
.GetPropertyOrNull("icon")? ?.GetNonWhiteSpaceStringOrNull()
.GetNonWhiteSpaceStringOrNull()? ?.Pipe(h => ImageCdn.GetGuildIconUrl(id, h)) ?? ImageCdn.GetFallbackUserAvatarUrl();
.Pipe(h => ImageCdn.GetGuildIconUrl(id, h)) ??
ImageCdn.GetFallbackUserAvatarUrl(0);
return new Guild(id, name, iconUrl); return new Guild(id, name, iconUrl);
} }
} }
@@ -15,4 +15,4 @@ public record Interaction(Snowflake Id, string Name, User User)
return new Interaction(id, name, user); return new Interaction(id, name, user);
} }
} }
@@ -6,10 +6,7 @@ using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data; namespace DiscordChatExporter.Core.Discord.Data;
// https://discord.com/developers/docs/resources/invite#invite-object // https://discord.com/developers/docs/resources/invite#invite-object
public record Invite( public record Invite(string Code, Guild Guild, Channel? Channel)
string Code,
Guild Guild,
Channel? Channel)
{ {
public static string? TryGetCodeFromUrl(string url) => public static string? TryGetCodeFromUrl(string url) =>
Regex.Match(url, @"^https?://discord\.gg/(\w+)/?$").Groups[1].Value.NullIfWhiteSpace(); Regex.Match(url, @"^https?://discord\.gg/(\w+)/?$").Groups[1].Value.NullIfWhiteSpace();
@@ -22,4 +19,4 @@ public record Invite(
return new Invite(code, guild, channel); return new Invite(code, guild, channel);
} }
} }
+18 -20
View File
@@ -11,39 +11,37 @@ namespace DiscordChatExporter.Core.Discord.Data;
// https://discord.com/developers/docs/resources/guild#guild-member-object // https://discord.com/developers/docs/resources/guild#guild-member-object
public partial record Member( public partial record Member(
User User, User User,
string? Nick, string? DisplayName,
string? AvatarUrl, string? AvatarUrl,
IReadOnlyList<Snowflake> RoleIds) : IHasId IReadOnlyList<Snowflake> RoleIds
) : IHasId
{ {
public Snowflake Id => User.Id; public Snowflake Id => User.Id;
} }
public partial record Member public partial record Member
{ {
public static Member CreateFallback(User user) =>
new(user, null, null, Array.Empty<Snowflake>());
public static Member Parse(JsonElement json, Snowflake? guildId = null) public static Member Parse(JsonElement json, Snowflake? guildId = null)
{ {
var user = json.GetProperty("user").Pipe(User.Parse); var user = json.GetProperty("user").Pipe(User.Parse);
var nick = json.GetPropertyOrNull("nick")?.GetNonWhiteSpaceStringOrNull(); var displayName = json.GetPropertyOrNull("nick")?.GetNonWhiteSpaceStringOrNull();
var roleIds = json var roleIds =
.GetPropertyOrNull("roles")? json.GetPropertyOrNull("roles")
.EnumerateArray() ?.EnumerateArray()
.Select(j => j.GetNonWhiteSpaceString()) .Select(j => j.GetNonWhiteSpaceString())
.Select(Snowflake.Parse) .Select(Snowflake.Parse)
.ToArray() ?? Array.Empty<Snowflake>(); .ToArray() ?? Array.Empty<Snowflake>();
var avatarUrl = guildId is not null var avatarUrl = guildId is not null
? json ? json.GetPropertyOrNull("avatar")
.GetPropertyOrNull("avatar")? ?.GetNonWhiteSpaceStringOrNull()
.GetNonWhiteSpaceStringOrNull()? ?.Pipe(h => ImageCdn.GetMemberAvatarUrl(guildId.Value, user.Id, h))
.Pipe(h => ImageCdn.GetMemberAvatarUrl(guildId.Value, user.Id, h))
: null; : null;
return new Member( return new Member(user, displayName, avatarUrl, roleIds);
user,
nick,
avatarUrl,
roleIds
);
} }
} }
@@ -27,9 +27,16 @@ public partial record Message(
IReadOnlyList<User> MentionedUsers, IReadOnlyList<User> MentionedUsers,
MessageReference? Reference, MessageReference? Reference,
Message? ReferencedMessage, Message? ReferencedMessage,
Interaction? Interaction) : IHasId Interaction? Interaction
) : IHasId
{ {
public bool IsReplyLike => Kind == MessageKind.Reply || Interaction is not null; public bool IsSystemNotification =>
Kind is >= MessageKind.RecipientAdd and <= MessageKind.ThreadCreated;
public bool IsReply => Kind == MessageKind.Reply;
// App interactions are rendered as replies in the Discord client, but they are not actually replies
public bool IsReplyLike => IsReply || Interaction is not null;
public IEnumerable<User> GetReferencedUsers() public IEnumerable<User> GetReferencedUsers()
{ {
@@ -70,22 +77,26 @@ public partial record Message
// Find embeds with the same URL that only contain a single image and nothing else // Find embeds with the same URL that only contain a single image and nothing else
var trailingEmbeds = embeds var trailingEmbeds = embeds
.Skip(i + 1) .Skip(i + 1)
.TakeWhile(e => .TakeWhile(
e.Url == embed.Url && e =>
e.Timestamp is null && e.Url == embed.Url
e.Author is null && && e.Timestamp is null
e.Color is null && && e.Author is null
string.IsNullOrWhiteSpace(e.Description) && && e.Color is null
!e.Fields.Any() && && string.IsNullOrWhiteSpace(e.Description)
e.Images.Count == 1 && && !e.Fields.Any()
e.Footer is null && e.Images.Count == 1
&& e.Footer is null
) )
.ToArray(); .ToArray();
if (trailingEmbeds.Any()) if (trailingEmbeds.Any())
{ {
// Concatenate all images into one embed // Concatenate all images into one embed
var images = embed.Images.Concat(trailingEmbeds.SelectMany(e => e.Images)).ToArray(); var images = embed.Images
.Concat(trailingEmbeds.SelectMany(e => e.Images))
.ToArray();
normalizedEmbeds.Add(embed with { Images = images }); normalizedEmbeds.Add(embed with { Images = images });
i += trailingEmbeds.Length; i += trailingEmbeds.Length;
@@ -108,41 +119,49 @@ public partial record Message
{ {
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse); var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
var kind = (MessageKind)json.GetProperty("type").GetInt32(); var kind = (MessageKind)json.GetProperty("type").GetInt32();
var flags = (MessageFlags?)json.GetPropertyOrNull("flags")?.GetInt32() ?? MessageFlags.None; var flags =
(MessageFlags?)json.GetPropertyOrNull("flags")?.GetInt32OrNull() ?? MessageFlags.None;
var author = json.GetProperty("author").Pipe(User.Parse); var author = json.GetProperty("author").Pipe(User.Parse);
var timestamp = json.GetProperty("timestamp").GetDateTimeOffset(); var timestamp = json.GetProperty("timestamp").GetDateTimeOffset();
var editedTimestamp = json.GetPropertyOrNull("edited_timestamp")?.GetDateTimeOffset(); var editedTimestamp = json.GetPropertyOrNull("edited_timestamp")?.GetDateTimeOffsetOrNull();
var callEndedTimestamp = json
.GetPropertyOrNull("call")? var callEndedTimestamp = json.GetPropertyOrNull("call")
.GetPropertyOrNull("ended_timestamp")? ?.GetPropertyOrNull("ended_timestamp")
.GetDateTimeOffset(); ?.GetDateTimeOffsetOrNull();
var isPinned = json.GetPropertyOrNull("pinned")?.GetBooleanOrNull() ?? false; var isPinned = json.GetPropertyOrNull("pinned")?.GetBooleanOrNull() ?? false;
var content = json.GetPropertyOrNull("content")?.GetStringOrNull() ?? ""; var content = json.GetPropertyOrNull("content")?.GetStringOrNull() ?? "";
var attachments = var attachments =
json.GetPropertyOrNull("attachments")?.EnumerateArrayOrNull()?.Select(Attachment.Parse).ToArray() ?? json.GetPropertyOrNull("attachments")
Array.Empty<Attachment>(); ?.EnumerateArrayOrNull()
?.Select(Attachment.Parse)
.ToArray() ?? Array.Empty<Attachment>();
var embeds = NormalizeEmbeds( var embeds = NormalizeEmbeds(
json.GetPropertyOrNull("embeds")?.EnumerateArrayOrNull()?.Select(Embed.Parse).ToArray() ?? json.GetPropertyOrNull("embeds")?.EnumerateArrayOrNull()?.Select(Embed.Parse).ToArray()
Array.Empty<Embed>() ?? Array.Empty<Embed>()
); );
var stickers = var stickers =
json.GetPropertyOrNull("sticker_items")?.EnumerateArrayOrNull()?.Select(Sticker.Parse).ToArray() ?? json.GetPropertyOrNull("sticker_items")
Array.Empty<Sticker>(); ?.EnumerateArrayOrNull()
?.Select(Sticker.Parse)
.ToArray() ?? Array.Empty<Sticker>();
var reactions = var reactions =
json.GetPropertyOrNull("reactions")?.EnumerateArrayOrNull()?.Select(Reaction.Parse).ToArray() ?? json.GetPropertyOrNull("reactions")
Array.Empty<Reaction>(); ?.EnumerateArrayOrNull()
?.Select(Reaction.Parse)
.ToArray() ?? Array.Empty<Reaction>();
var mentionedUsers = var mentionedUsers =
json.GetPropertyOrNull("mentions")?.EnumerateArrayOrNull()?.Select(User.Parse).ToArray() ?? json.GetPropertyOrNull("mentions")?.EnumerateArrayOrNull()?.Select(User.Parse).ToArray()
Array.Empty<User>(); ?? Array.Empty<User>();
var messageReference = json.GetPropertyOrNull("message_reference")?.Pipe(MessageReference.Parse); var messageReference = json.GetPropertyOrNull("message_reference")
?.Pipe(MessageReference.Parse);
var referencedMessage = json.GetPropertyOrNull("referenced_message")?.Pipe(Parse); var referencedMessage = json.GetPropertyOrNull("referenced_message")?.Pipe(Parse);
var interaction = json.GetPropertyOrNull("interaction")?.Pipe(Interaction.Parse); var interaction = json.GetPropertyOrNull("interaction")?.Pipe(Interaction.Parse);
@@ -166,4 +185,4 @@ public partial record Message
interaction interaction
); );
} }
} }
@@ -15,4 +15,4 @@ public enum MessageFlags
HasThread = 32, HasThread = 32,
Ephemeral = 64, Ephemeral = 64,
Loading = 128 Loading = 128
} }
@@ -14,8 +14,3 @@ public enum MessageKind
ThreadCreated = 18, ThreadCreated = 18,
Reply = 19 Reply = 19
} }
public static class MessageKindExtensions
{
public static bool IsSystemNotification(this MessageKind c) => (int)c is >= 1 and <= 18;
}
@@ -9,21 +9,18 @@ public record MessageReference(Snowflake? MessageId, Snowflake? ChannelId, Snowf
{ {
public static MessageReference Parse(JsonElement json) public static MessageReference Parse(JsonElement json)
{ {
var messageId = json var messageId = json.GetPropertyOrNull("message_id")
.GetPropertyOrNull("message_id")? ?.GetNonWhiteSpaceStringOrNull()
.GetNonWhiteSpaceStringOrNull()? ?.Pipe(Snowflake.Parse);
.Pipe(Snowflake.Parse);
var channelId = json var channelId = json.GetPropertyOrNull("channel_id")
.GetPropertyOrNull("channel_id")? ?.GetNonWhiteSpaceStringOrNull()
.GetNonWhiteSpaceStringOrNull()? ?.Pipe(Snowflake.Parse);
.Pipe(Snowflake.Parse);
var guildId = json var guildId = json.GetPropertyOrNull("guild_id")
.GetPropertyOrNull("guild_id")? ?.GetNonWhiteSpaceStringOrNull()
.GetNonWhiteSpaceStringOrNull()? ?.Pipe(Snowflake.Parse);
.Pipe(Snowflake.Parse);
return new MessageReference(messageId, channelId, guildId); return new MessageReference(messageId, channelId, guildId);
} }
} }
@@ -13,4 +13,4 @@ public record Reaction(Emoji Emoji, int Count)
return new Reaction(emoji, count); return new Reaction(emoji, count);
} }
} }
@@ -15,13 +15,12 @@ public record Role(Snowflake Id, string Name, int Position, Color? Color) : IHas
var name = json.GetProperty("name").GetNonNullString(); var name = json.GetProperty("name").GetNonNullString();
var position = json.GetProperty("position").GetInt32(); var position = json.GetProperty("position").GetInt32();
var color = json var color = json.GetPropertyOrNull("color")
.GetPropertyOrNull("color")? ?.GetInt32OrNull()
.GetInt32() ?.Pipe(System.Drawing.Color.FromArgb)
.Pipe(System.Drawing.Color.FromArgb)
.ResetAlpha() .ResetAlpha()
.NullIf(c => c.ToRgb() <= 0); .NullIf(c => c.ToRgb() <= 0);
return new Role(id, name, position, color); return new Role(id, name, position, color);
} }
} }
@@ -15,14 +15,17 @@ public record Sticker(Snowflake Id, string Name, StickerFormat Format, string So
var name = json.GetProperty("name").GetNonNullString(); var name = json.GetProperty("name").GetNonNullString();
var format = (StickerFormat)json.GetProperty("format_type").GetInt32(); var format = (StickerFormat)json.GetProperty("format_type").GetInt32();
var sourceUrl = ImageCdn.GetStickerUrl(id, format switch var sourceUrl = ImageCdn.GetStickerUrl(
{ id,
StickerFormat.Png => "png", format switch
StickerFormat.Apng => "png", {
StickerFormat.Lottie => "json", StickerFormat.Png => "png",
_ => throw new InvalidOperationException($"Unknown sticker format '{format}'.") StickerFormat.Apng => "png",
}); StickerFormat.Lottie => "json",
_ => throw new InvalidOperationException($"Unknown sticker format '{format}'.")
}
);
return new Sticker(id, name, format, sourceUrl); return new Sticker(id, name, format, sourceUrl);
} }
} }
@@ -5,4 +5,4 @@ public enum StickerFormat
Png = 1, Png = 1,
Apng = 2, Apng = 2,
Lottie = 3 Lottie = 3
} }
+29 -12
View File
@@ -9,13 +9,22 @@ namespace DiscordChatExporter.Core.Discord.Data;
public partial record User( public partial record User(
Snowflake Id, Snowflake Id,
bool IsBot, bool IsBot,
int Discriminator, // Remove after Discord migrates all accounts to the new system.
// With that, also remove the DiscriminatorFormatted and FullName properties.
// Replace existing calls to FullName with Name (not DisplayName).
int? Discriminator,
string Name, string Name,
string AvatarUrl) : IHasId string DisplayName,
string AvatarUrl
) : IHasId
{ {
public string DiscriminatorFormatted => $"{Discriminator:0000}"; public string DiscriminatorFormatted =>
Discriminator is not null ? $"{Discriminator:0000}" : "0000";
public string FullName => $"{Name}#{DiscriminatorFormatted}"; // This effectively represents the user's true identity.
// In the old system, this is formed from the username and discriminator.
// In the new system, the username is already the user's unique identifier.
public string FullName => Discriminator is not null ? $"{Name}#{DiscriminatorFormatted}" : Name;
} }
public partial record User public partial record User
@@ -24,16 +33,24 @@ public partial record User
{ {
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse); var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
var isBot = json.GetPropertyOrNull("bot")?.GetBooleanOrNull() ?? false; var isBot = json.GetPropertyOrNull("bot")?.GetBooleanOrNull() ?? false;
var discriminator = json.GetProperty("discriminator").GetNonWhiteSpaceString().Pipe(int.Parse);
var discriminator = json.GetPropertyOrNull("discriminator")
?.GetNonWhiteSpaceStringOrNull()
?.Pipe(int.Parse)
.NullIfDefault();
var name = json.GetProperty("username").GetNonNullString(); var name = json.GetProperty("username").GetNonNullString();
var displayName =
json.GetPropertyOrNull("global_name")?.GetNonWhiteSpaceStringOrNull() ?? name;
var avatarIndex = discriminator % 5 ?? (int)((id.Value >> 22) % 6);
var avatarUrl = var avatarUrl =
json json.GetPropertyOrNull("avatar")
.GetPropertyOrNull("avatar")? ?.GetNonWhiteSpaceStringOrNull()
.GetNonWhiteSpaceStringOrNull()? ?.Pipe(h => ImageCdn.GetUserAvatarUrl(id, h))
.Pipe(h => ImageCdn.GetUserAvatarUrl(id, h)) ?? ?? ImageCdn.GetFallbackUserAvatarUrl(avatarIndex);
ImageCdn.GetFallbackUserAvatarUrl(discriminator);
return new User(id, isBot, discriminator, name, avatarUrl); return new User(id, isBot, discriminator, name, displayName, avatarUrl);
} }
} }
+407 -127
View File
@@ -30,48 +30,46 @@ public class DiscordClient
private async ValueTask<HttpResponseMessage> GetResponseAsync( private async ValueTask<HttpResponseMessage> GetResponseAsync(
string url, string url,
TokenKind tokenKind, TokenKind tokenKind,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
return await Http.ResponseResiliencePolicy.ExecuteAsync(async innerCancellationToken => return await Http.ResponseResiliencePolicy.ExecuteAsync(
{ async innerCancellationToken =>
using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(_baseUri, url));
// Don't validate because the token can have special characters
// https://github.com/Tyrrrz/DiscordChatExporter/issues/828
request.Headers.TryAddWithoutValidation(
"Authorization",
tokenKind == TokenKind.Bot
? $"Bot {_token}"
: _token
);
var response = await Http.Client.SendAsync(
request,
HttpCompletionOption.ResponseHeadersRead,
innerCancellationToken
);
// If this was the last request available before hitting the rate limit,
// wait out the reset time so that future requests can succeed.
// This may add an unnecessary delay in case the user doesn't intend to
// make any more requests, but implementing a smarter solution would
// require properly keeping track of Discord's global/per-route/per-resource
// rate limits and that's just way too much effort.
// https://discord.com/developers/docs/topics/rate-limits
var remainingRequestCount = response
.Headers
.TryGetValue("X-RateLimit-Remaining")?
.Pipe(s => int.Parse(s, CultureInfo.InvariantCulture));
var resetAfterDelay = response
.Headers
.TryGetValue("X-RateLimit-Reset-After")?
.Pipe(s => double.Parse(s, CultureInfo.InvariantCulture))
.Pipe(TimeSpan.FromSeconds);
if (remainingRequestCount <= 0 && resetAfterDelay is not null)
{ {
var delay = using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(_baseUri, url));
// Don't validate because the token can have special characters
// https://github.com/Tyrrrz/DiscordChatExporter/issues/828
request.Headers.TryAddWithoutValidation(
"Authorization",
tokenKind == TokenKind.Bot ? $"Bot {_token}" : _token
);
var response = await Http.Client.SendAsync(
request,
HttpCompletionOption.ResponseHeadersRead,
innerCancellationToken
);
// If this was the last request available before hitting the rate limit,
// wait out the reset time so that future requests can succeed.
// This may add an unnecessary delay in case the user doesn't intend to
// make any more requests, but implementing a smarter solution would
// require properly keeping track of Discord's global/per-route/per-resource
// rate limits and that's just way too much effort.
// https://discord.com/developers/docs/topics/rate-limits
var remainingRequestCount = response.Headers
.TryGetValue("X-RateLimit-Remaining")
?.Pipe(s => int.Parse(s, CultureInfo.InvariantCulture));
var resetAfterDelay = response.Headers
.TryGetValue("X-RateLimit-Reset-After")
?.Pipe(s => double.Parse(s, CultureInfo.InvariantCulture))
.Pipe(TimeSpan.FromSeconds);
if (remainingRequestCount <= 0 && resetAfterDelay is not null)
{
var delay =
// Adding a small buffer to the reset time reduces the chance of getting // Adding a small buffer to the reset time reduces the chance of getting
// rate limited again, because it allows for more requests to be released. // rate limited again, because it allows for more requests to be released.
(resetAfterDelay.Value + TimeSpan.FromSeconds(1)) (resetAfterDelay.Value + TimeSpan.FromSeconds(1))
@@ -79,14 +77,18 @@ public class DiscordClient
// is not actually enforced by the server. So we cap it at a reasonable value. // is not actually enforced by the server. So we cap it at a reasonable value.
.Clamp(TimeSpan.Zero, TimeSpan.FromSeconds(60)); .Clamp(TimeSpan.Zero, TimeSpan.FromSeconds(60));
await Task.Delay(delay, innerCancellationToken); await Task.Delay(delay, innerCancellationToken);
} }
return response; return response;
}, cancellationToken); },
cancellationToken
);
} }
private async ValueTask<TokenKind> GetTokenKindAsync(CancellationToken cancellationToken = default) private async ValueTask<TokenKind> GetTokenKindAsync(
CancellationToken cancellationToken = default
)
{ {
// Try authenticating as a user // Try authenticating as a user
using var userResponse = await GetResponseAsync( using var userResponse = await GetResponseAsync(
@@ -108,12 +110,13 @@ public class DiscordClient
if (botResponse.StatusCode != HttpStatusCode.Unauthorized) if (botResponse.StatusCode != HttpStatusCode.Unauthorized)
return TokenKind.Bot; return TokenKind.Bot;
throw DiscordChatExporterException.Unauthorized(); throw new DiscordChatExporterException("Authentication token is invalid.", true);
} }
private async ValueTask<HttpResponseMessage> GetResponseAsync( private async ValueTask<HttpResponseMessage> GetResponseAsync(
string url, string url,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
var tokenKind = _resolvedTokenKind ??= await GetTokenKindAsync(cancellationToken); var tokenKind = _resolvedTokenKind ??= await GetTokenKindAsync(cancellationToken);
return await GetResponseAsync(url, tokenKind, cancellationToken); return await GetResponseAsync(url, tokenKind, cancellationToken);
@@ -121,7 +124,8 @@ public class DiscordClient
private async ValueTask<JsonElement> GetJsonResponseAsync( private async ValueTask<JsonElement> GetJsonResponseAsync(
string url, string url,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
using var response = await GetResponseAsync(url, cancellationToken); using var response = await GetResponseAsync(url, cancellationToken);
@@ -129,10 +133,30 @@ public class DiscordClient
{ {
throw response.StatusCode switch throw response.StatusCode switch
{ {
HttpStatusCode.Unauthorized => DiscordChatExporterException.Unauthorized(), HttpStatusCode.Unauthorized
HttpStatusCode.Forbidden => DiscordChatExporterException.Forbidden(), => throw new DiscordChatExporterException(
HttpStatusCode.NotFound => DiscordChatExporterException.NotFound(url), "Authentication token is invalid.",
_ => DiscordChatExporterException.FailedHttpRequest(response) true
),
HttpStatusCode.Forbidden
=> throw new DiscordChatExporterException(
$"Request to '{url}' failed: forbidden."
),
HttpStatusCode.NotFound
=> throw new DiscordChatExporterException(
$"Request to '{url}' failed: not found."
),
_
=> throw new DiscordChatExporterException(
$"""
Request to '{url}' failed: {response.StatusCode.ToString().ToSpaceSeparatedWords().ToLowerInvariant()}.
Response content: {await response.Content.ReadAsStringAsync(cancellationToken)}
""",
true
)
}; };
} }
@@ -141,22 +165,31 @@ public class DiscordClient
private async ValueTask<JsonElement?> TryGetJsonResponseAsync( private async ValueTask<JsonElement?> TryGetJsonResponseAsync(
string url, string url,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
using var response = await GetResponseAsync(url, cancellationToken); using var response = await GetResponseAsync(url, cancellationToken);
return response.IsSuccessStatusCode return response.IsSuccessStatusCode
? await response.Content.ReadAsJsonAsync(cancellationToken) ? await response.Content.ReadAsJsonAsync(cancellationToken)
: null; : null;
} }
public async ValueTask<User?> TryGetUserAsync(
Snowflake userId,
CancellationToken cancellationToken = default
)
{
var response = await TryGetJsonResponseAsync($"users/{userId}", cancellationToken);
return response?.Pipe(User.Parse);
}
public async IAsyncEnumerable<Guild> GetUserGuildsAsync( public async IAsyncEnumerable<Guild> GetUserGuildsAsync(
[EnumeratorCancellation] CancellationToken cancellationToken = default) [EnumeratorCancellation] CancellationToken cancellationToken = default
)
{ {
yield return Guild.DirectMessages; yield return Guild.DirectMessages;
var currentAfter = Snowflake.Zero; var currentAfter = Snowflake.Zero;
while (true) while (true)
{ {
var url = new UrlBuilder() var url = new UrlBuilder()
@@ -167,23 +200,25 @@ public class DiscordClient
var response = await GetJsonResponseAsync(url, cancellationToken); var response = await GetJsonResponseAsync(url, cancellationToken);
var isEmpty = true; var count = 0;
foreach (var guild in response.EnumerateArray().Select(Guild.Parse)) foreach (var guildJson in response.EnumerateArray())
{ {
var guild = Guild.Parse(guildJson);
yield return guild; yield return guild;
currentAfter = guild.Id; currentAfter = guild.Id;
isEmpty = false; count++;
} }
if (isEmpty) if (count <= 0)
yield break; yield break;
} }
} }
public async ValueTask<Guild> GetGuildAsync( public async ValueTask<Guild> GetGuildAsync(
Snowflake guildId, Snowflake guildId,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
if (guildId == Guild.DirectMessages.Id) if (guildId == Guild.DirectMessages.Id)
return Guild.DirectMessages; return Guild.DirectMessages;
@@ -194,7 +229,8 @@ public class DiscordClient
public async IAsyncEnumerable<Channel> GetGuildChannelsAsync( public async IAsyncEnumerable<Channel> GetGuildChannelsAsync(
Snowflake guildId, Snowflake guildId,
[EnumeratorCancellation] CancellationToken cancellationToken = default) [EnumeratorCancellation] CancellationToken cancellationToken = default
)
{ {
if (guildId == Guild.DirectMessages.Id) if (guildId == Guild.DirectMessages.Id)
{ {
@@ -204,49 +240,243 @@ public class DiscordClient
} }
else else
{ {
var response = await GetJsonResponseAsync($"guilds/{guildId}/channels", cancellationToken); var response = await GetJsonResponseAsync(
$"guilds/{guildId}/channels",
cancellationToken
);
var responseOrdered = response var channelsJson = response
.EnumerateArray() .EnumerateArray()
.OrderBy(j => j.GetProperty("position").GetInt32()) .OrderBy(j => j.GetProperty("position").GetInt32())
.ThenBy(j => j.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse)) .ThenBy(j => j.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse))
.ToArray(); .ToArray();
var categories = responseOrdered var parentsById = channelsJson
.Where(j => j.GetProperty("type").GetInt32() == (int) ChannelKind.GuildCategory) .Where(j => j.GetProperty("type").GetInt32() == (int)ChannelKind.GuildCategory)
.Select((j, index) => ChannelCategory.Parse(j, index + 1)) .Select((j, i) => Channel.Parse(j, null, i + 1))
.ToDictionary(j => j.Id.ToString(), StringComparer.Ordinal); .ToDictionary(j => j.Id);
// Discord positions are not deterministic, so we need to normalize them // Discord channel positions are relative, so we need to normalize them
// because the user may refer to the channel position via file name template. // so that the user may refer to them more easily in file name templates.
var position = 0; var position = 0;
foreach (var channelJson in responseOrdered) foreach (var channelJson in channelsJson)
{ {
var parentId = channelJson.GetPropertyOrNull("parent_id")?.GetNonWhiteSpaceStringOrNull(); var parent = channelJson
.GetPropertyOrNull("parent_id")
var category = !string.IsNullOrWhiteSpace(parentId) ?.GetNonWhiteSpaceStringOrNull()
? categories.GetValueOrDefault(parentId) ?.Pipe(Snowflake.Parse)
: null; .Pipe(parentsById.GetValueOrDefault);
var channel = Channel.Parse(channelJson, category, position);
yield return Channel.Parse(channelJson, parent, position);
position++; position++;
}
}
}
yield return channel; public async IAsyncEnumerable<Channel> GetGuildThreadsAsync(
Snowflake guildId,
bool includeArchived = false,
Snowflake? before = null,
Snowflake? after = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default
)
{
if (guildId == Guild.DirectMessages.Id)
yield break;
var tokenKind = _resolvedTokenKind ??= await GetTokenKindAsync(cancellationToken);
var channels = (await GetGuildChannelsAsync(guildId, cancellationToken))
// Categories cannot have threads
.Where(c => !c.IsCategory)
// Voice channels cannot have threads
.Where(c => !c.IsVoice)
// Empty channels cannot have threads
.Where(c => !c.IsEmpty)
// If the 'before' boundary is specified, skip channels that don't have messages
// for that range, because thread-start event should always be accompanied by a message.
// Note that we don't perform a similar check for the 'after' boundary, because
// threads may have messages in range, even if the parent channel doesn't.
.Where(c => before is null || c.MayHaveMessagesBefore(before.Value))
.ToArray();
// User accounts can only fetch threads using the search endpoint
if (tokenKind == TokenKind.User)
{
// Active threads
foreach (var channel in channels)
{
var currentOffset = 0;
while (true)
{
var url = new UrlBuilder()
.SetPath($"channels/{channel.Id}/threads/search")
.SetQueryParameter("sort_by", "last_message_time")
.SetQueryParameter("sort_order", "desc")
.SetQueryParameter("archived", "false")
.SetQueryParameter("offset", currentOffset.ToString())
.Build();
// Can be null on channels that the user cannot access or channels without threads
var response = await TryGetJsonResponseAsync(url, cancellationToken);
if (response is null)
break;
var breakOuter = false;
foreach (
var threadJson in response.Value.GetProperty("threads").EnumerateArray()
)
{
var thread = Channel.Parse(threadJson, channel);
// If the 'after' boundary is specified, we can break early,
// because threads are sorted by last message time.
if (after is not null && !thread.MayHaveMessagesAfter(after.Value))
{
breakOuter = true;
break;
}
yield return thread;
currentOffset++;
}
if (breakOuter)
break;
if (!response.Value.GetProperty("has_more").GetBoolean())
break;
}
}
// Archived threads
if (includeArchived)
{
foreach (var channel in channels)
{
var currentOffset = 0;
while (true)
{
var url = new UrlBuilder()
.SetPath($"channels/{channel.Id}/threads/search")
.SetQueryParameter("sort_by", "last_message_time")
.SetQueryParameter("sort_order", "desc")
.SetQueryParameter("archived", "true")
.SetQueryParameter("offset", currentOffset.ToString())
.Build();
// Can be null on channels that the user cannot access or channels without threads
var response = await TryGetJsonResponseAsync(url, cancellationToken);
if (response is null)
break;
var breakOuter = false;
foreach (
var threadJson in response.Value.GetProperty("threads").EnumerateArray()
)
{
var thread = Channel.Parse(threadJson, channel);
// If the 'after' boundary is specified, we can break early,
// because threads are sorted by last message time.
if (after is not null && !thread.MayHaveMessagesAfter(after.Value))
{
breakOuter = true;
break;
}
yield return thread;
currentOffset++;
}
if (breakOuter)
break;
if (!response.Value.GetProperty("has_more").GetBoolean())
break;
}
}
}
}
// Bot accounts can only fetch threads using the threads endpoint
else
{
// Active threads
{
var parentsById = channels.ToDictionary(c => c.Id);
var response = await GetJsonResponseAsync(
$"guilds/{guildId}/threads/active",
cancellationToken
);
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
{
var parent = threadJson
.GetPropertyOrNull("parent_id")
?.GetNonWhiteSpaceStringOrNull()
?.Pipe(Snowflake.Parse)
.Pipe(parentsById.GetValueOrDefault);
yield return Channel.Parse(threadJson, parent);
}
}
// Archived threads
if (includeArchived)
{
foreach (var channel in channels)
{
// Public archived threads
{
// Can be null on certain channels
var response = await TryGetJsonResponseAsync(
$"channels/{channel.Id}/threads/archived/public",
cancellationToken
);
if (response is null)
continue;
foreach (
var threadJson in response.Value.GetProperty("threads").EnumerateArray()
)
yield return Channel.Parse(threadJson, channel);
}
// Private archived threads
{
// Can be null on certain channels
var response = await TryGetJsonResponseAsync(
$"channels/{channel.Id}/threads/archived/private",
cancellationToken
);
if (response is null)
continue;
foreach (
var threadJson in response.Value.GetProperty("threads").EnumerateArray()
)
yield return Channel.Parse(threadJson, channel);
}
}
} }
} }
} }
public async IAsyncEnumerable<Role> GetGuildRolesAsync( public async IAsyncEnumerable<Role> GetGuildRolesAsync(
Snowflake guildId, Snowflake guildId,
[EnumeratorCancellation] CancellationToken cancellationToken = default) [EnumeratorCancellation] CancellationToken cancellationToken = default
)
{ {
if (guildId == Guild.DirectMessages.Id) if (guildId == Guild.DirectMessages.Id)
yield break; yield break;
var response = await GetJsonResponseAsync($"guilds/{guildId}/roles", cancellationToken); var response = await GetJsonResponseAsync($"guilds/{guildId}/roles", cancellationToken);
foreach (var roleJson in response.EnumerateArray()) foreach (var roleJson in response.EnumerateArray())
yield return Role.Parse(roleJson); yield return Role.Parse(roleJson);
} }
@@ -254,59 +484,62 @@ public class DiscordClient
public async ValueTask<Member?> TryGetGuildMemberAsync( public async ValueTask<Member?> TryGetGuildMemberAsync(
Snowflake guildId, Snowflake guildId,
Snowflake memberId, Snowflake memberId,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
if (guildId == Guild.DirectMessages.Id) if (guildId == Guild.DirectMessages.Id)
return null; return null;
var response = await TryGetJsonResponseAsync($"guilds/{guildId}/members/{memberId}", cancellationToken); var response = await TryGetJsonResponseAsync(
$"guilds/{guildId}/members/{memberId}",
cancellationToken
);
return response?.Pipe(j => Member.Parse(j, guildId)); return response?.Pipe(j => Member.Parse(j, guildId));
} }
public async ValueTask<Invite?> TryGetGuildInviteAsync( public async ValueTask<Invite?> TryGetInviteAsync(
string code, string code,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
var response = await TryGetJsonResponseAsync($"invites/{code}", cancellationToken); var response = await TryGetJsonResponseAsync($"invites/{code}", cancellationToken);
return response?.Pipe(Invite.Parse); return response?.Pipe(Invite.Parse);
} }
public async ValueTask<ChannelCategory> GetChannelCategoryAsync(
Snowflake channelId,
CancellationToken cancellationToken = default)
{
try
{
var response = await GetJsonResponseAsync($"channels/{channelId}", cancellationToken);
return ChannelCategory.Parse(response);
}
// In some cases, the Discord API returns an empty body when requesting channel category.
// Instead, we use an empty channel category as a fallback.
catch (DiscordChatExporterException)
{
return new ChannelCategory(channelId, "Unknown Category", 0);
}
}
public async ValueTask<Channel> GetChannelAsync( public async ValueTask<Channel> GetChannelAsync(
Snowflake channelId, Snowflake channelId,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
var response = await GetJsonResponseAsync($"channels/{channelId}", cancellationToken); var response = await GetJsonResponseAsync($"channels/{channelId}", cancellationToken);
var parentId = response.GetPropertyOrNull("parent_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse); var parentId = response
.GetPropertyOrNull("parent_id")
?.GetNonWhiteSpaceStringOrNull()
?.Pipe(Snowflake.Parse);
var category = parentId is not null try
? await GetChannelCategoryAsync(parentId.Value, cancellationToken) {
: null; var parent = parentId is not null
? await GetChannelAsync(parentId.Value, cancellationToken)
: null;
return Channel.Parse(response, category); return Channel.Parse(response, parent);
}
// It's possible for the parent channel to be inaccessible, despite the
// child channel being accessible.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/1108
catch (DiscordChatExporterException)
{
return Channel.Parse(response);
}
} }
private async ValueTask<Message?> TryGetLastMessageAsync( private async ValueTask<Message?> TryGetLastMessageAsync(
Snowflake channelId, Snowflake channelId,
Snowflake? before = null, Snowflake? before = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
var url = new UrlBuilder() var url = new UrlBuilder()
.SetPath($"channels/{channelId}/messages") .SetPath($"channels/{channelId}/messages")
@@ -323,20 +556,21 @@ public class DiscordClient
Snowflake? after = null, Snowflake? after = null,
Snowflake? before = null, Snowflake? before = null,
IProgress<Percentage>? progress = null, IProgress<Percentage>? progress = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default) [EnumeratorCancellation] CancellationToken cancellationToken = default
)
{ {
// Get the last message in the specified range. // Get the last message in the specified range, so we can later calculate the
// This snapshots the boundaries, which means that messages posted after the export started // progress based on the difference between message timestamps.
// will not appear in the output. // This also snapshots the boundaries, which means that messages posted after
// Additionally, it provides the date of the last message, which is used to calculate progress. // the export started will not appear in the output.
var lastMessage = await TryGetLastMessageAsync(channelId, before, cancellationToken); var lastMessage = await TryGetLastMessageAsync(channelId, before, cancellationToken);
if (lastMessage is null || lastMessage.Timestamp < after?.ToDate()) if (lastMessage is null || lastMessage.Timestamp < after?.ToDate())
yield break; yield break;
// Keep track of first message in range in order to calculate progress // Keep track of the first message in range in order to calculate the progress
var firstMessage = default(Message); var firstMessage = default(Message);
var currentAfter = after ?? Snowflake.Zero;
var currentAfter = after ?? Snowflake.Zero;
while (true) while (true)
{ {
var url = new UrlBuilder() var url = new UrlBuilder()
@@ -350,7 +584,8 @@ public class DiscordClient
var messages = response var messages = response
.EnumerateArray() .EnumerateArray()
.Select(Message.Parse) .Select(Message.Parse)
.Reverse() // reverse because messages appear newest first // Messages are returned from newest to oldest, so we need to reverse them
.Reverse()
.ToArray(); .ToArray();
// Break if there are no messages (can happen if messages are deleted during execution) // Break if there are no messages (can happen if messages are deleted during execution)
@@ -361,23 +596,25 @@ public class DiscordClient
{ {
firstMessage ??= message; firstMessage ??= message;
// Ensure messages are in range (take into account that last message could have been deleted) // Ensure that the messages are in range
if (message.Timestamp > lastMessage.Timestamp) if (message.Timestamp > lastMessage.Timestamp)
yield break; yield break;
// Report progress based on the duration of exported messages divided by total // Report progress based on timestamps
if (progress is not null) if (progress is not null)
{ {
var exportedDuration = (message.Timestamp - firstMessage.Timestamp).Duration(); var exportedDuration = (message.Timestamp - firstMessage.Timestamp).Duration();
var totalDuration = (lastMessage.Timestamp - firstMessage.Timestamp).Duration(); var totalDuration = (lastMessage.Timestamp - firstMessage.Timestamp).Duration();
progress.Report(Percentage.FromFraction( progress.Report(
// Avoid division by zero if all messages have the exact same timestamp Percentage.FromFraction(
// (which may be the case if there's only one message in the channel) // Avoid division by zero if all messages have the exact same timestamp
totalDuration > TimeSpan.Zero // (which happens when there's only one message in the channel)
? exportedDuration / totalDuration totalDuration > TimeSpan.Zero
: 1 ? exportedDuration / totalDuration
)); : 1
)
);
} }
yield return message; yield return message;
@@ -385,4 +622,47 @@ public class DiscordClient
} }
} }
} }
}
public async IAsyncEnumerable<User> GetMessageReactionsAsync(
Snowflake channelId,
Snowflake messageId,
Emoji emoji,
[EnumeratorCancellation] CancellationToken cancellationToken = default
)
{
var reactionName = emoji.Id is not null
// Custom emoji
? emoji.Name + ':' + emoji.Id
// Standard emoji
: emoji.Name;
var currentAfter = Snowflake.Zero;
while (true)
{
var url = new UrlBuilder()
.SetPath(
$"channels/{channelId}/messages/{messageId}/reactions/{Uri.EscapeDataString(reactionName)}"
)
.SetQueryParameter("limit", "100")
.SetQueryParameter("after", currentAfter.ToString())
.Build();
var response = await GetJsonResponseAsync(url, cancellationToken);
var count = 0;
foreach (var userJson in response.EnumerateArray())
{
var user = User.Parse(userJson);
yield return user;
currentAfter = user.Id;
count++;
}
// Each batch can contain up to 100 users.
// If we got fewer, then it's definitely the last batch.
if (count < 100)
yield break;
}
}
}
+11 -11
View File
@@ -6,9 +6,10 @@ namespace DiscordChatExporter.Core.Discord;
public readonly partial record struct Snowflake(ulong Value) public readonly partial record struct Snowflake(ulong Value)
{ {
public DateTimeOffset ToDate() => DateTimeOffset.FromUnixTimeMilliseconds( public DateTimeOffset ToDate() =>
(long)((Value >> 22) + 1420070400000UL) DateTimeOffset
).ToLocalTime(); .FromUnixTimeMilliseconds((long)((Value >> 22) + 1420070400000UL))
.ToLocalTime();
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public override string ToString() => Value.ToString(CultureInfo.InvariantCulture); public override string ToString() => Value.ToString(CultureInfo.InvariantCulture);
@@ -18,9 +19,8 @@ public partial record struct Snowflake
{ {
public static Snowflake Zero { get; } = new(0); public static Snowflake Zero { get; } = new(0);
public static Snowflake FromDate(DateTimeOffset instant) => new( public static Snowflake FromDate(DateTimeOffset instant) =>
((ulong)instant.ToUnixTimeMilliseconds() - 1420070400000UL) << 22 new(((ulong)instant.ToUnixTimeMilliseconds() - 1420070400000UL) << 22);
);
public static Snowflake? TryParse(string? str, IFormatProvider? formatProvider = null) public static Snowflake? TryParse(string? str, IFormatProvider? formatProvider = null)
{ {
@@ -29,15 +29,11 @@ public partial record struct Snowflake
// As number // As number
if (ulong.TryParse(str, NumberStyles.None, formatProvider, out var value)) if (ulong.TryParse(str, NumberStyles.None, formatProvider, out var value))
{
return new Snowflake(value); return new Snowflake(value);
}
// As date // As date
if (DateTimeOffset.TryParse(str, formatProvider, DateTimeStyles.None, out var instant)) if (DateTimeOffset.TryParse(str, formatProvider, DateTimeStyles.None, out var instant))
{
return FromDate(instant); return FromDate(instant);
}
return null; return null;
} }
@@ -59,4 +55,8 @@ public partial record struct Snowflake : IComparable<Snowflake>, IComparable
return Value.CompareTo(other.Value); return Value.CompareTo(other.Value);
} }
}
public static bool operator >(Snowflake left, Snowflake right) => left.CompareTo(right) > 0;
public static bool operator <(Snowflake left, Snowflake right) => left.CompareTo(right) < 0;
}
@@ -4,4 +4,4 @@ public enum TokenKind
{ {
User, User,
Bot Bot
} }
@@ -1,17 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="6.1.1" /> <PackageReference Include="AsyncKeyedLock" Version="6.2.1" />
<PackageReference Include="Gress" Version="2.0.1" /> <PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
<PackageReference Include="Gress" Version="2.1.1" />
<PackageReference Include="JsonExtensions" Version="1.2.0" /> <PackageReference Include="JsonExtensions" Version="1.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="RazorBlade" Version="0.4.2" /> <PackageReference Include="RazorBlade" Version="0.4.3" />
<PackageReference Include="Superpower" Version="3.0.0" /> <PackageReference Include="Superpower" Version="3.0.0" />
<PackageReference Include="WebMarkupMin.Core" Version="2.13.8" /> <PackageReference Include="WebMarkupMin.Core" Version="2.14.0" />
<PackageReference Include="YoutubeExplode" Version="6.3.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -1,9 +1,8 @@
using System; using System;
using System.Net.Http;
namespace DiscordChatExporter.Core.Exceptions; namespace DiscordChatExporter.Core.Exceptions;
public partial class DiscordChatExporterException : Exception public class DiscordChatExporterException : Exception
{ {
public bool IsFatal { get; } public bool IsFatal { get; }
@@ -13,32 +12,3 @@ public partial class DiscordChatExporterException : Exception
IsFatal = isFatal; IsFatal = isFatal;
} }
} }
public partial class DiscordChatExporterException
{
internal static DiscordChatExporterException FailedHttpRequest(HttpResponseMessage response)
{
var message = $@"
Failed to perform an HTTP request.
[Request]
{response.RequestMessage}
[Response]
{response}";
return new DiscordChatExporterException(message.Trim(), true);
}
internal static DiscordChatExporterException Unauthorized() =>
new("Authentication token is invalid.", true);
internal static DiscordChatExporterException Forbidden() =>
new("Access is forbidden.");
internal static DiscordChatExporterException NotFound(string resourceId) =>
new($"Requested resource ({resourceId}) does not exist.");
internal static DiscordChatExporterException ChannelIsEmpty() =>
new("Channel is empty or contains no messages for the specified period.");
}
@@ -2,6 +2,7 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Exceptions;
using Gress; using Gress;
@@ -16,25 +17,55 @@ public class ChannelExporter
public async ValueTask ExportChannelAsync( public async ValueTask ExportChannelAsync(
ExportRequest request, ExportRequest request,
IProgress<Percentage>? progress = null, IProgress<Percentage>? progress = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
// Forum channels don't have messages, they are just a list of threads
if (request.Channel.Kind == ChannelKind.GuildForum)
throw new DiscordChatExporterException("Channel is a forum.");
// Check if the channel is empty
if (request.Channel.IsEmpty)
throw new DiscordChatExporterException("Channel does not contain any messages.");
// Check if the 'after' boundary is valid
if (request.After is not null && !request.Channel.MayHaveMessagesAfter(request.After.Value))
{
throw new DiscordChatExporterException(
"Channel does not contain any messages within the specified period."
);
}
// Check if the 'before' boundary is valid
if (
request.Before is not null
&& !request.Channel.MayHaveMessagesBefore(request.Before.Value)
)
{
throw new DiscordChatExporterException(
"Channel does not contain any messages within the specified period."
);
}
// Build context // Build context
var context = new ExportContext(_discord, request); var context = new ExportContext(_discord, request);
await context.PopulateChannelsAndRolesAsync(cancellationToken); await context.PopulateChannelsAndRolesAsync(cancellationToken);
// Export messages // Export messages
await using var messageExporter = new MessageExporter(context); await using var messageExporter = new MessageExporter(context);
await foreach (
await foreach (var message in _discord.GetMessagesAsync( var message in _discord.GetMessagesAsync(
request.Channel.Id, request.Channel.Id,
request.After, request.After,
request.Before, request.Before,
progress, progress,
cancellationToken)) cancellationToken
)
)
{ {
// Resolve members for referenced users // Resolve members for referenced users
foreach (var user in message.GetReferencedUsers()) foreach (var user in message.GetReferencedUsers())
await context.PopulateMemberAsync(user.Id, cancellationToken); await context.PopulateMemberAsync(user, cancellationToken);
// Export the message // Export the message
if (request.MessageFilter.IsMatch(message)) if (request.MessageFilter.IsMatch(message))
@@ -43,6 +74,10 @@ public class ChannelExporter
// Throw if no messages were exported // Throw if no messages were exported
if (messageExporter.MessagesExported <= 0) if (messageExporter.MessagesExported <= 0)
throw DiscordChatExporterException.ChannelIsEmpty(); {
throw new DiscordChatExporterException(
"Channel does not contain any matching messages within the specified period."
);
}
} }
} }
@@ -20,17 +20,20 @@ internal partial class CsvMessageWriter : MessageWriter
private async ValueTask<string> FormatMarkdownAsync( private async ValueTask<string> FormatMarkdownAsync(
string markdown, string markdown,
CancellationToken cancellationToken = default) => CancellationToken cancellationToken = default
) =>
Context.Request.ShouldFormatMarkdown Context.Request.ShouldFormatMarkdown
? await PlainTextMarkdownVisitor.FormatAsync(Context, markdown, cancellationToken) ? await PlainTextMarkdownVisitor.FormatAsync(Context, markdown, cancellationToken)
: markdown; : markdown;
public override async ValueTask WritePreambleAsync(CancellationToken cancellationToken = default) => public override async ValueTask WritePreambleAsync(
await _writer.WriteLineAsync("AuthorID,Author,Date,Content,Attachments,Reactions"); CancellationToken cancellationToken = default
) => await _writer.WriteLineAsync("AuthorID,Author,Date,Content,Attachments,Reactions");
private async ValueTask WriteAttachmentsAsync( private async ValueTask WriteAttachmentsAsync(
IReadOnlyList<Attachment> attachments, IReadOnlyList<Attachment> attachments,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
var buffer = new StringBuilder(); var buffer = new StringBuilder();
@@ -48,7 +51,8 @@ internal partial class CsvMessageWriter : MessageWriter
private async ValueTask WriteReactionsAsync( private async ValueTask WriteReactionsAsync(
IReadOnlyList<Reaction> reactions, IReadOnlyList<Reaction> reactions,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
var buffer = new StringBuilder(); var buffer = new StringBuilder();
@@ -70,7 +74,8 @@ internal partial class CsvMessageWriter : MessageWriter
public override async ValueTask WriteMessageAsync( public override async ValueTask WriteMessageAsync(
Message message, Message message,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default
)
{ {
await base.WriteMessageAsync(message, cancellationToken); await base.WriteMessageAsync(message, cancellationToken);
@@ -87,13 +92,15 @@ internal partial class CsvMessageWriter : MessageWriter
await _writer.WriteAsync(','); await _writer.WriteAsync(',');
// Message content // Message content
if (message.Kind.IsSystemNotification()) if (message.IsSystemNotification)
{ {
await _writer.WriteAsync(CsvEncode(message.GetFallbackContent())); await _writer.WriteAsync(CsvEncode(message.GetFallbackContent()));
} }
else else
{ {
await _writer.WriteAsync(CsvEncode(await FormatMarkdownAsync(message.Content, cancellationToken))); await _writer.WriteAsync(
CsvEncode(await FormatMarkdownAsync(message.Content, cancellationToken))
);
} }
await _writer.WriteAsync(','); await _writer.WriteAsync(',');
@@ -123,4 +130,4 @@ internal partial class CsvMessageWriter
value = value.Replace("\"", "\"\""); value = value.Replace("\"", "\"\"");
return $"\"{value}\""; return $"\"{value}\"";
} }
} }
@@ -15,17 +15,18 @@ namespace DiscordChatExporter.Core.Exporting;
internal partial class ExportAssetDownloader internal partial class ExportAssetDownloader
{ {
private static readonly AsyncKeyedLocker<string> Locker = new(o => private static readonly AsyncKeyedLocker<string> Locker =
{ new(o =>
o.PoolSize = 20; {
o.PoolInitialFill = 1; o.PoolSize = 20;
}); o.PoolInitialFill = 1;
});
private readonly string _workingDirPath; private readonly string _workingDirPath;
private readonly bool _reuse; private readonly bool _reuse;
// File paths of the previously downloaded assets // File paths of the previously downloaded assets
private readonly Dictionary<string, string> _pathCache = new(StringComparer.Ordinal); private readonly Dictionary<string, string> _previousPathsByUrl = new(StringComparer.Ordinal);
public ExportAssetDownloader(string workingDirPath, bool reuse) public ExportAssetDownloader(string workingDirPath, bool reuse)
{ {
@@ -33,72 +34,82 @@ internal partial class ExportAssetDownloader
_reuse = reuse; _reuse = reuse;
} }
public async ValueTask<string> DownloadAsync(string url, CancellationToken cancellationToken = default) public async ValueTask<string> DownloadAsync(
string url,
CancellationToken cancellationToken = default
)
{ {
var fileName = GetFileNameFromUrl(url); var fileName = GetFileNameFromUrl(url);
var filePath = Path.Combine(_workingDirPath, fileName); var filePath = Path.Combine(_workingDirPath, fileName);
using (await Locker.LockAsync(filePath, cancellationToken)) using var _ = await Locker.LockAsync(filePath, cancellationToken);
if (_previousPathsByUrl.TryGetValue(url, out var cachedFilePath))
return cachedFilePath;
// Reuse existing files if we're allowed to
if (_reuse && File.Exists(filePath))
return _previousPathsByUrl[url] = filePath;
Directory.CreateDirectory(_workingDirPath);
await Http.ResiliencePolicy.ExecuteAsync(async () =>
{ {
if (_pathCache.TryGetValue(url, out var cachedFilePath)) // Download the file
return cachedFilePath; using var response = await Http.Client.GetAsync(url, cancellationToken);
await using (var output = File.Create(filePath))
await response.Content.CopyToAsync(output, cancellationToken);
// Reuse existing files if we're allowed to // Try to set the file date according to the last-modified header
if (_reuse && File.Exists(filePath)) try
return _pathCache[url] = filePath;
Directory.CreateDirectory(_workingDirPath);
await Http.ResiliencePolicy.ExecuteAsync(async () =>
{ {
// Download the file var lastModified = response.Content.Headers
using var response = await Http.Client.GetAsync(url, cancellationToken); .TryGetValue("Last-Modified")
await using (var output = File.Create(filePath)) ?.Pipe(
await response.Content.CopyToAsync(output, cancellationToken); s =>
DateTimeOffset.TryParse(
// Try to set the file date according to the last-modified header s,
try CultureInfo.InvariantCulture,
{ DateTimeStyles.None,
var lastModified = response.Content.Headers.TryGetValue("Last-Modified")?.Pipe(s => out var instant
DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out var instant) )
? instant ? instant
: (DateTimeOffset?)null : (DateTimeOffset?)null
); );
if (lastModified is not null) if (lastModified is not null)
{
File.SetCreationTimeUtc(filePath, lastModified.Value.UtcDateTime);
File.SetLastWriteTimeUtc(filePath, lastModified.Value.UtcDateTime);
File.SetLastAccessTimeUtc(filePath, lastModified.Value.UtcDateTime);
}
}
catch
{ {
// This can apparently fail for some reason. File.SetCreationTimeUtc(filePath, lastModified.Value.UtcDateTime);
// https://github.com/Tyrrrz/DiscordChatExporter/issues/585 File.SetLastWriteTimeUtc(filePath, lastModified.Value.UtcDateTime);
// Updating file dates is not a critical task, so we'll just File.SetLastAccessTimeUtc(filePath, lastModified.Value.UtcDateTime);
// ignore exceptions thrown here.
} }
}); }
catch
{
// This can apparently fail for some reason.
// Updating the file date is not a critical task, so we'll just ignore exceptions thrown here.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/585
}
});
return _pathCache[url] = filePath; return _previousPathsByUrl[url] = filePath;
}
} }
} }
internal partial class ExportAssetDownloader internal partial class ExportAssetDownloader
{ {
private static string GetUrlHash(string url) => SHA256 private static string GetUrlHash(string url) =>
.HashData(Encoding.UTF8.GetBytes(url)) SHA256
.ToHex() .HashData(Encoding.UTF8.GetBytes(url))
// 5 chars ought to be enough for anybody .ToHex()
.Truncate(5); // 5 chars ought to be enough for anybody
.Truncate(5);
private static string GetFileNameFromUrl(string url) private static string GetFileNameFromUrl(string url)
{ {
var urlHash = GetUrlHash(url); var urlHash = GetUrlHash(url);
// Try to extract file name from URL // Try to extract the file name from URL
var fileName = Regex.Match(url, @".+/([^?]*)").Groups[1].Value; var fileName = Regex.Match(url, @".+/([^?]*)").Groups[1].Value;
// If it's not there, just use the URL hash as the file name // If it's not there, just use the URL hash as the file name
@@ -110,13 +121,15 @@ internal partial class ExportAssetDownloader
var fileExtension = Path.GetExtension(fileName); var fileExtension = Path.GetExtension(fileName);
// Probably not a file extension, just a dot in a long file name // Probably not a file extension, just a dot in a long file name
// https://github.com/Tyrrrz/DiscordChatExporter/issues/708 // https://github.com/Tyrrrz/DiscordChatExporter/pull/812
if (fileExtension.Length > 41) if (fileExtension.Length > 41)
{ {
fileNameWithoutExtension = fileName; fileNameWithoutExtension = fileName;
fileExtension = ""; fileExtension = "";
} }
return PathEx.EscapeFileName(fileNameWithoutExtension.Truncate(42) + '-' + urlHash + fileExtension); return PathEx.EscapeFileName(
fileNameWithoutExtension.Truncate(42) + '-' + urlHash + fileExtension
);
} }
} }
@@ -14,16 +14,16 @@ namespace DiscordChatExporter.Core.Exporting;
internal class ExportContext internal class ExportContext
{ {
private readonly Dictionary<Snowflake, Member?> _members = new(); private readonly Dictionary<Snowflake, Member?> _membersById = new();
private readonly Dictionary<Snowflake, Channel> _channels = new(); private readonly Dictionary<Snowflake, Channel> _channelsById = new();
private readonly Dictionary<Snowflake, Role> _roles = new(); private readonly Dictionary<Snowflake, Role> _rolesById = new();
private readonly ExportAssetDownloader _assetDownloader; private readonly ExportAssetDownloader _assetDownloader;
public DiscordClient Discord { get; } public DiscordClient Discord { get; }
public ExportRequest Request { get; } public ExportRequest Request { get; }
public ExportContext(DiscordClient discord, public ExportContext(DiscordClient discord, ExportRequest request)
ExportRequest request)
{ {
Discord = discord; Discord = discord;
Request = request; Request = request;
@@ -34,58 +34,86 @@ internal class ExportContext
); );
} }
public async ValueTask PopulateChannelsAndRolesAsync(CancellationToken cancellationToken = default) public DateTimeOffset NormalizeDate(DateTimeOffset instant) =>
Request.IsUtcNormalizationEnabled ? instant.ToUniversalTime() : instant.ToLocalTime();
public string FormatDate(DateTimeOffset instant, string format = "g") =>
NormalizeDate(instant).ToString(format, Request.CultureInfo);
public async ValueTask PopulateChannelsAndRolesAsync(
CancellationToken cancellationToken = default
)
{ {
await foreach (var channel in Discord.GetGuildChannelsAsync(Request.Guild.Id, cancellationToken)) await foreach (
_channels[channel.Id] = channel; var channel in Discord.GetGuildChannelsAsync(Request.Guild.Id, cancellationToken)
)
{
_channelsById[channel.Id] = channel;
}
await foreach (var role in Discord.GetGuildRolesAsync(Request.Guild.Id, cancellationToken)) await foreach (var role in Discord.GetGuildRolesAsync(Request.Guild.Id, cancellationToken))
_roles[role.Id] = role; {
_rolesById[role.Id] = role;
}
} }
// Because members are not pulled in bulk, we need to populate them on demand // Because members cannot be pulled in bulk, we need to populate them on demand
public async ValueTask PopulateMemberAsync(Snowflake id, CancellationToken cancellationToken = default) private async ValueTask PopulateMemberAsync(
Snowflake id,
User? fallbackUser,
CancellationToken cancellationToken = default
)
{ {
if (_members.ContainsKey(id)) if (_membersById.ContainsKey(id))
return; return;
var member = await Discord.TryGetGuildMemberAsync(Request.Guild.Id, id, cancellationToken); var member = await Discord.TryGetGuildMemberAsync(Request.Guild.Id, id, cancellationToken);
// User may have left the guild since they were mentioned.
// Create a dummy member object based on the user info.
if (member is null)
{
var user = fallbackUser ?? await Discord.TryGetUserAsync(id, cancellationToken);
// User may have been deleted since they were mentioned
if (user is not null)
member = Member.CreateFallback(user);
}
// Store the result even if it's null, to avoid re-fetching non-existing members // Store the result even if it's null, to avoid re-fetching non-existing members
_members[id] = member; _membersById[id] = member;
} }
public string FormatDate(DateTimeOffset instant) => Request.DateFormat switch public async ValueTask PopulateMemberAsync(
{ Snowflake id,
"unix" => instant.ToUnixTimeSeconds().ToString(), CancellationToken cancellationToken = default
"unixms" => instant.ToUnixTimeMilliseconds().ToString(), ) => await PopulateMemberAsync(id, null, cancellationToken);
var format => instant.ToLocalString(format)
};
public Member? TryGetMember(Snowflake id) => _members.GetValueOrDefault(id); public async ValueTask PopulateMemberAsync(
User user,
CancellationToken cancellationToken = default
) => await PopulateMemberAsync(user.Id, user, cancellationToken);
public Channel? TryGetChannel(Snowflake id) => _channels.GetValueOrDefault(id); public Member? TryGetMember(Snowflake id) => _membersById.GetValueOrDefault(id);
public Role? TryGetRole(Snowflake id) => _roles.GetValueOrDefault(id); public Channel? TryGetChannel(Snowflake id) => _channelsById.GetValueOrDefault(id);
public Color? TryGetUserColor(Snowflake id) public Role? TryGetRole(Snowflake id) => _rolesById.GetValueOrDefault(id);
{
var member = TryGetMember(id);
var memberRoles = member? public IReadOnlyList<Role> GetUserRoles(Snowflake id) =>
.RoleIds TryGetMember(id)?.RoleIds
.Select(TryGetRole) .Select(TryGetRole)
.WhereNotNull() .WhereNotNull()
.ToArray();
return memberRoles?
.Where(r => r.Color is not null)
.OrderByDescending(r => r.Position) .OrderByDescending(r => r.Position)
.Select(r => r.Color) .ToArray() ?? Array.Empty<Role>();
.FirstOrDefault();
}
public async ValueTask<string> ResolveAssetUrlAsync(string url, CancellationToken cancellationToken = default) public Color? TryGetUserColor(Snowflake id) =>
GetUserRoles(id).Where(r => r.Color is not null).Select(r => r.Color).FirstOrDefault();
public async ValueTask<string> ResolveAssetUrlAsync(
string url,
CancellationToken cancellationToken = default
)
{ {
if (!Request.ShouldDownloadAssets) if (!Request.ShouldDownloadAssets)
return url; return url;
@@ -96,10 +124,16 @@ internal class ExportContext
var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath); var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath);
// Prefer relative paths so that the output files can be copied around without breaking references. // Prefer relative paths so that the output files can be copied around without breaking references.
// If the assets path is outside of the export directory, use the absolute path instead. // If the asset directory is outside of the export directory, use an absolute path instead.
var optimalFilePath = var optimalFilePath =
relativeFilePath.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) || relativeFilePath.StartsWith(
relativeFilePath.StartsWith(".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal) ".." + Path.DirectorySeparatorChar,
StringComparison.Ordinal
)
|| relativeFilePath.StartsWith(
".." + Path.AltDirectorySeparatorChar,
StringComparison.Ordinal
)
? filePath ? filePath
: relativeFilePath; : relativeFilePath;
@@ -117,9 +151,9 @@ internal class ExportContext
// https://github.com/Tyrrrz/DiscordChatExporter/issues/372 // https://github.com/Tyrrrz/DiscordChatExporter/issues/372
catch (Exception ex) when (ex is HttpRequestException or OperationCanceledException) catch (Exception ex) when (ex is HttpRequestException or OperationCanceledException)
{ {
// TODO: add logging so we can be more liberal with catching exceptions // We don't want this to crash the exporting process in case of failure.
// We don't want this to crash the exporting process in case of failure // TODO: add logging so we can be more liberal with catching exceptions.
return url; return url;
} }
} }
} }
@@ -13,23 +13,25 @@ public enum ExportFormat
public static class ExportFormatExtensions public static class ExportFormatExtensions
{ {
public static string GetFileExtension(this ExportFormat format) => format switch public static string GetFileExtension(this ExportFormat format) =>
{ format switch
ExportFormat.PlainText => "txt", {
ExportFormat.HtmlDark => "html", ExportFormat.PlainText => "txt",
ExportFormat.HtmlLight => "html", ExportFormat.HtmlDark => "html",
ExportFormat.Csv => "csv", ExportFormat.HtmlLight => "html",
ExportFormat.Json => "json", ExportFormat.Csv => "csv",
_ => throw new ArgumentOutOfRangeException(nameof(format)) ExportFormat.Json => "json",
}; _ => throw new ArgumentOutOfRangeException(nameof(format))
};
public static string GetDisplayName(this ExportFormat format) => format switch public static string GetDisplayName(this ExportFormat format) =>
{ format switch
ExportFormat.PlainText => "TXT", {
ExportFormat.HtmlDark => "HTML (Dark)", ExportFormat.PlainText => "TXT",
ExportFormat.HtmlLight => "HTML (Light)", ExportFormat.HtmlDark => "HTML (Dark)",
ExportFormat.Csv => "CSV", ExportFormat.HtmlLight => "HTML (Light)",
ExportFormat.Json => "JSON", ExportFormat.Csv => "CSV",
_ => throw new ArgumentOutOfRangeException(nameof(format)) ExportFormat.Json => "JSON",
}; _ => throw new ArgumentOutOfRangeException(nameof(format))
} };
}
@@ -1,4 +1,5 @@
using System; using System;
using System.Globalization;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@@ -38,7 +39,11 @@ public partial class ExportRequest
public bool ShouldReuseAssets { get; } public bool ShouldReuseAssets { get; }
public string DateFormat { get; } public string Locale { get; }
public CultureInfo CultureInfo { get; }
public bool IsUtcNormalizationEnabled { get; }
public ExportRequest( public ExportRequest(
Guild guild, Guild guild,
@@ -53,7 +58,9 @@ public partial class ExportRequest
bool shouldFormatMarkdown, bool shouldFormatMarkdown,
bool shouldDownloadAssets, bool shouldDownloadAssets,
bool shouldReuseAssets, bool shouldReuseAssets,
string dateFormat) string locale,
bool isUtcNormalizationEnabled
)
{ {
Guild = guild; Guild = guild;
Channel = channel; Channel = channel;
@@ -65,28 +72,18 @@ public partial class ExportRequest
ShouldFormatMarkdown = shouldFormatMarkdown; ShouldFormatMarkdown = shouldFormatMarkdown;
ShouldDownloadAssets = shouldDownloadAssets; ShouldDownloadAssets = shouldDownloadAssets;
ShouldReuseAssets = shouldReuseAssets; ShouldReuseAssets = shouldReuseAssets;
DateFormat = dateFormat; Locale = locale;
IsUtcNormalizationEnabled = isUtcNormalizationEnabled;
OutputFilePath = GetOutputBaseFilePath( OutputFilePath = GetOutputBaseFilePath(Guild, Channel, outputPath, Format, After, Before);
Guild,
Channel,
outputPath,
Format,
After,
Before
);
OutputDirPath = Path.GetDirectoryName(OutputFilePath)!; OutputDirPath = Path.GetDirectoryName(OutputFilePath)!;
AssetsDirPath = !string.IsNullOrWhiteSpace(assetsDirPath) AssetsDirPath = !string.IsNullOrWhiteSpace(assetsDirPath)
? FormatPath( ? FormatPath(assetsDirPath, Guild, Channel, After, Before)
assetsDirPath,
Guild,
Channel,
After,
Before
)
: $"{OutputFilePath}_Files{Path.DirectorySeparatorChar}"; : $"{OutputFilePath}_Files{Path.DirectorySeparatorChar}";
CultureInfo = CultureInfo.GetCultureInfo(Locale);
} }
} }
@@ -97,12 +94,26 @@ public partial class ExportRequest
Channel channel, Channel channel,
ExportFormat format, ExportFormat format,
Snowflake? after = null, Snowflake? after = null,
Snowflake? before = null) Snowflake? before = null
)
{ {
var buffer = new StringBuilder(); var buffer = new StringBuilder();
// Guild and channel names // Guild name
buffer.Append($"{guild.Name} - {channel.Category.Name} - {channel.Name} [{channel.Id}]"); buffer.Append(guild.Name);
// Parent name
if (channel.Parent is not null)
buffer.Append(" - ").Append(channel.Parent.Name);
// Channel name and ID
buffer
.Append(" - ")
.Append(channel.Name)
.Append(' ')
.Append('[')
.Append(channel.Id)
.Append(']');
// Date range // Date range
if (after is not null || before is not null) if (after is not null || before is not null)
@@ -112,7 +123,9 @@ public partial class ExportRequest
// Both 'after' and 'before' are set // Both 'after' and 'before' are set
if (after is not null && before is not null) if (after is not null && before is not null)
{ {
buffer.Append($"{after.Value.ToDate():yyyy-MM-dd} to {before.Value.ToDate():yyyy-MM-dd}"); buffer.Append(
$"{after.Value.ToDate():yyyy-MM-dd} to {before.Value.ToDate():yyyy-MM-dd}"
);
} }
// Only 'after' is set // Only 'after' is set
else if (after is not null) else if (after is not null)
@@ -139,29 +152,46 @@ public partial class ExportRequest
Guild guild, Guild guild,
Channel channel, Channel channel,
Snowflake? after, Snowflake? after,
Snowflake? before) Snowflake? before
{ ) =>
return Regex.Replace( Regex.Replace(
path, path,
"%.", "%.",
m => PathEx.EscapeFileName(m.Value switch m =>
{ PathEx.EscapeFileName(
"%g" => guild.Id.ToString(), m.Value switch
"%G" => guild.Name, {
"%t" => channel.Category.Id.ToString(), "%g" => guild.Id.ToString(),
"%T" => channel.Category.Name, "%G" => guild.Name,
"%c" => channel.Id.ToString(),
"%C" => channel.Name, "%t" => channel.Parent?.Id.ToString() ?? "",
"%p" => channel.Position?.ToString() ?? "0", "%T" => channel.Parent?.Name ?? "",
"%P" => channel.Category.Position?.ToString() ?? "0",
"%a" => after?.ToDate().ToString("yyyy-MM-dd") ?? "", "%c" => channel.Id.ToString(),
"%b" => before?.ToDate().ToString("yyyy-MM-dd") ?? "", "%C" => channel.Name,
"%d" => DateTimeOffset.Now.ToString("yyyy-MM-dd"),
"%%" => "%", "%p" => channel.Position?.ToString(CultureInfo.InvariantCulture) ?? "0",
_ => m.Value "%P"
}) => channel.Parent?.Position?.ToString(CultureInfo.InvariantCulture)
?? "0",
"%a"
=> after?.ToDate().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
?? "",
"%b"
=> before?.ToDate().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
?? "",
"%d"
=> DateTimeOffset.Now.ToString(
"yyyy-MM-dd",
CultureInfo.InvariantCulture
),
"%%" => "%",
_ => m.Value
}
)
); );
}
private static string GetOutputBaseFilePath( private static string GetOutputBaseFilePath(
Guild guild, Guild guild,
@@ -169,12 +199,16 @@ public partial class ExportRequest
string outputPath, string outputPath,
ExportFormat format, ExportFormat format,
Snowflake? after = null, Snowflake? after = null,
Snowflake? before = null) Snowflake? before = null
)
{ {
var actualOutputPath = FormatPath(outputPath, guild, channel, after, before); var actualOutputPath = FormatPath(outputPath, guild, channel, after, before);
// Output is a directory // Output is a directory
if (Directory.Exists(actualOutputPath) || string.IsNullOrWhiteSpace(Path.GetExtension(actualOutputPath))) if (
Directory.Exists(actualOutputPath)
|| string.IsNullOrWhiteSpace(Path.GetExtension(actualOutputPath))
)
{ {
var fileName = GetDefaultOutputFileName(guild, channel, format, after, before); var fileName = GetDefaultOutputFileName(guild, channel, format, after, before);
return Path.Combine(actualOutputPath, fileName); return Path.Combine(actualOutputPath, fileName);
@@ -183,4 +217,4 @@ public partial class ExportRequest
// Output is a file // Output is a file
return actualOutputPath; return actualOutputPath;
} }
} }
@@ -4,4 +4,4 @@ internal enum BinaryExpressionKind
{ {
Or, Or,
And And
} }
@@ -9,17 +9,22 @@ internal class BinaryExpressionMessageFilter : MessageFilter
private readonly MessageFilter _second; private readonly MessageFilter _second;
private readonly BinaryExpressionKind _kind; private readonly BinaryExpressionKind _kind;
public BinaryExpressionMessageFilter(MessageFilter first, MessageFilter second, BinaryExpressionKind kind) public BinaryExpressionMessageFilter(
MessageFilter first,
MessageFilter second,
BinaryExpressionKind kind
)
{ {
_first = first; _first = first;
_second = second; _second = second;
_kind = kind; _kind = kind;
} }
public override bool IsMatch(Message message) => _kind switch public override bool IsMatch(Message message) =>
{ _kind switch
BinaryExpressionKind.Or => _first.IsMatch(message) || _second.IsMatch(message), {
BinaryExpressionKind.And => _first.IsMatch(message) && _second.IsMatch(message), BinaryExpressionKind.Or => _first.IsMatch(message) || _second.IsMatch(message),
_ => throw new InvalidOperationException($"Unknown binary expression kind '{_kind}'.") BinaryExpressionKind.And => _first.IsMatch(message) && _second.IsMatch(message),
}; _ => throw new InvalidOperationException($"Unknown binary expression kind '{_kind}'.")
} };
}
@@ -17,25 +17,21 @@ internal class ContainsMessageFilter : MessageFilter
// parentheses are not considered word characters. // parentheses are not considered word characters.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/909 // https://github.com/Tyrrrz/DiscordChatExporter/issues/909
private bool IsMatch(string? content) => private bool IsMatch(string? content) =>
!string.IsNullOrWhiteSpace(content) && !string.IsNullOrWhiteSpace(content)
Regex.IsMatch( && Regex.IsMatch(
content, content,
@"(?:\b|\s|^)" + @"(?:\b|\s|^)" + Regex.Escape(_text) + @"(?:\b|\s|$)",
Regex.Escape(_text) +
@"(?:\b|\s|$)",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
); );
public override bool IsMatch(Message message) => public override bool IsMatch(Message message) =>
IsMatch(message.Content) || IsMatch(message.Content)
message.Embeds.Any(e => || message.Embeds.Any(
IsMatch(e.Title) || e =>
IsMatch(e.Author?.Name) || IsMatch(e.Title)
IsMatch(e.Description) || || IsMatch(e.Author?.Name)
IsMatch(e.Footer?.Text) || || IsMatch(e.Description)
e.Fields.Any(f => || IsMatch(e.Footer?.Text)
IsMatch(f.Name) || || e.Fields.Any(f => IsMatch(f.Name) || IsMatch(f.Value))
IsMatch(f.Value)
)
); );
} }

Some files were not shown because too many files have changed in this diff Show More