mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-07-07 14:44:39 +02:00
Compare commits
150 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f7f6ac9494 | |||
| 481958f4d3 | |||
| 5e3502cbc5 | |||
| 3b03c30865 | |||
| c9e16a7f74 | |||
| f9b6793d47 | |||
| cfbd267d3a | |||
| 2b6f01bf6f | |||
| a44d43eefc | |||
| 0e138bac7d | |||
| 71876febf8 | |||
| 489c786c1e | |||
| eb0c61e173 | |||
| 9341c21963 | |||
| 704c483074 | |||
| 1f0e667085 | |||
| 0b72b73d1d | |||
| 32dd1689d0 | |||
| 2e4d19671a | |||
| 5b4d88ca26 | |||
| 65cc3954b6 | |||
| ffe5544f9c | |||
| bf1ad07a9c | |||
| 338566c1d6 | |||
| 91ea7a443c | |||
| aea96d5eba | |||
| e29f08264c | |||
| ad84ecf6a4 | |||
| ae57a0ff7c | |||
| 1daff4178d | |||
| 3a2b119618 | |||
| 5f27426f44 | |||
| a893ebda68 | |||
| 9221966171 | |||
| 67325b610a | |||
| 3186ce90c8 | |||
| 168d7c8ec9 | |||
| daa1b3f6b3 | |||
| e2780d6666 | |||
| b996215e36 | |||
| 08d9af534b | |||
| bef110a193 | |||
| 12555e576f | |||
| afb4bc67ae | |||
| f80a5fe644 | |||
| b7dca87fb9 | |||
| 8ecc2940b0 | |||
| 407c2bd2ae | |||
| 5b563d4f00 | |||
| ecb615ecf1 | |||
| 7c212ef893 | |||
| 36b4a45f3a | |||
| 51cc132e5d | |||
| cf83cbd89c | |||
| d51d0d4872 | |||
| 2467caac9f | |||
| bd98f4cb6a | |||
| fc7191d74c | |||
| 6c484d1f88 | |||
| c36b8df8e7 | |||
| 0408096746 | |||
| 40d833cd57 | |||
| 0de1f47310 | |||
| e31ff55e33 | |||
| 81525a1076 | |||
| 65ab003ff5 | |||
| 2156c6cd0c | |||
| e97151cd19 | |||
| 271a478a0e | |||
| b40a88c4d3 | |||
| f03f5f8072 | |||
| bfa5d10761 | |||
| 7a9e25c1f0 | |||
| 4599e64f0a | |||
| 4389b4e0bc | |||
| 008bb2f591 | |||
| b8567d384f | |||
| a06b448f4a | |||
| ba4ece9325 | |||
| 880f400e2c | |||
| 8e7baee8a5 | |||
| f7c4b80a2d | |||
| a0e7d8e3af | |||
| 72b0bb37f4 | |||
| 6c70ff0963 | |||
| 9def32d67e | |||
| 4974fe5292 | |||
| 1db2bb4170 | |||
| 720e53caab | |||
| da9536bd1b | |||
| d8de3bf779 | |||
| 7c88a21543 | |||
| 2393a6a472 | |||
| 214d5e3bdb | |||
| f456297881 | |||
| 092f0cade9 | |||
| fc6cfcfcc6 | |||
| 46450367e3 | |||
| 21d89afa70 | |||
| 2f3e165988 | |||
| b64dff1eab | |||
| acfe102e7f | |||
| ea31b1b270 | |||
| af11064a85 | |||
| 402b5cc7d0 | |||
| 44f64a2cb3 | |||
| 471d0d01cf | |||
| 67313f2b22 | |||
| 301bca0633 | |||
| 6b8170ab89 | |||
| e849c9516c | |||
| 524d0a1bb2 | |||
| f0a4410ee0 | |||
| a98772e46e | |||
| b6fdea11a2 | |||
| fcca052165 | |||
| ac7ebd6f91 | |||
| 9303383662 | |||
| 7fa908ce34 | |||
| 345dc568c0 | |||
| 5c0b657e0e | |||
| 265cb71dfd | |||
| 0a6fea1ce9 | |||
| ebb36de354 | |||
| 48539084f7 | |||
| 9ec95670c5 | |||
| a449c02dd5 | |||
| 89b1f9924b | |||
| 99f94425c2 | |||
| ab71b035fc | |||
| fdc3c1b012 | |||
| 6fa6df981e | |||
| eec4123b98 | |||
| 6fa7cbe568 | |||
| aae3790a5f | |||
| 28b2039a33 | |||
| f40589dea6 | |||
| bb4db21b87 | |||
| 38bc259ab6 | |||
| 530e2c50a9 | |||
| 63c92047a1 | |||
| 7d9d3ca5fb | |||
| cad6baba48 | |||
| 9c25bceabf | |||
| 28de1053f6 | |||
| ec0494c038 | |||
| d735d2acd3 | |||
| 85d53d0e94 | |||
| daa8c0a735 | |||
| d4eba75036 |
@@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
"version": 1,
|
|
||||||
"isRoot": true,
|
|
||||||
"tools": {
|
|
||||||
"xamlstyler.console": {
|
|
||||||
"version": "3.2003.9",
|
|
||||||
"commands": [
|
|
||||||
"xstyler"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
github: Tyrrrz
|
|
||||||
patreon: Tyrrrz
|
|
||||||
custom: ['buymeacoffee.com/Tyrrrz', 'tyrrrz.me/donate']
|
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
blank_issues_enabled: false
|
blank_issues_enabled: false
|
||||||
contact_links:
|
contact_links:
|
||||||
|
- name: 💬 Discord server
|
||||||
|
url: https://discord.gg/2SUWKFnHSm
|
||||||
|
about: Chat with the project community.
|
||||||
- name: 📖 Wiki
|
- name: 📖 Wiki
|
||||||
url: https://github.com/Tyrrrz/DiscordChatExporter/wiki
|
url: https://github.com/Tyrrrz/DiscordChatExporter/wiki
|
||||||
about: Find usage guides and frequently asked questions.
|
about: Find usage guides and frequently asked questions.
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
<!--
|
|
||||||
|
|
||||||
**Important:**
|
|
||||||
|
|
||||||
Please make sure that there is an existing issue that describes the problem solved by your pull request. If there isn't one, consider creating it first.
|
|
||||||
An open issue offers a good place to iron out requirements, discuss possible solutions, and ask questions.
|
|
||||||
|
|
||||||
Remember to also:
|
|
||||||
|
|
||||||
- Keep your pull request focused and as small as possible. If you want to contribute multiple unrelated changes, please create separate pull requests for them.
|
|
||||||
- Follow the coding style and conventions already established by the project. When in doubt about which style to use, ask in the comments to your pull request.
|
|
||||||
- If you want to start a discussion regarding a specific change you've made, add a review comment to your own code. This can be used to highlight something important or to seek further input from others.
|
|
||||||
|
|
||||||
-->
|
|
||||||
|
|
||||||
<!-- Please specify the issue addressed by this pull request -->
|
|
||||||
Closes #ISSUE_NUMBER
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
name: Docker CD
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2.3.3
|
||||||
|
|
||||||
|
- name: Get release version
|
||||||
|
id: get_version
|
||||||
|
uses: dawidd6/action-get-tag@v1
|
||||||
|
|
||||||
|
- name: Login
|
||||||
|
run: docker login -u tyrrrz -p ${{ secrets.DOCKER_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: docker build -t tyrrrz/discordchatexporter:${{ steps.get_version.outputs.tag }} -t tyrrrz/discordchatexporter:stable .
|
||||||
|
|
||||||
|
- name: Deploy (version)
|
||||||
|
run: docker push tyrrrz/discordchatexporter:${{ steps.get_version.outputs.tag }}
|
||||||
|
|
||||||
|
- name: Deploy (stable)
|
||||||
|
run: docker push tyrrrz/discordchatexporter:stable
|
||||||
@@ -13,15 +13,15 @@ jobs:
|
|||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2.3.3
|
uses: actions/checkout@v2.3.3
|
||||||
|
|
||||||
- name: Install .NET (v3.1)
|
- name: Get release version
|
||||||
uses: actions/setup-dotnet@v1.7.2
|
id: get_version
|
||||||
with:
|
uses: dawidd6/action-get-tag@v1
|
||||||
dotnet-version: 3.1.x
|
|
||||||
|
|
||||||
- name: Install .NET (v5.0)
|
- name: Install .NET
|
||||||
uses: actions/setup-dotnet@v1.7.2
|
uses: actions/setup-dotnet@v1.7.2
|
||||||
with:
|
with:
|
||||||
dotnet-version: 5.0.x
|
# Fixed version, workaround for https://github.com/dotnet/core/issues/7176
|
||||||
|
dotnet-version: 6.0.100
|
||||||
|
|
||||||
- name: Build & publish (CLI)
|
- name: Build & publish (CLI)
|
||||||
run: dotnet publish DiscordChatExporter.Cli/ -o DiscordChatExporter.Cli/bin/Publish/ --configuration Release
|
run: dotnet publish DiscordChatExporter.Cli/ -o DiscordChatExporter.Cli/bin/Publish/ --configuration Release
|
||||||
@@ -69,3 +69,17 @@ jobs:
|
|||||||
asset_path: DiscordChatExporter.Gui/bin/Publish/Archive.zip
|
asset_path: DiscordChatExporter.Gui/bin/Publish/Archive.zip
|
||||||
asset_name: DiscordChatExporter.zip
|
asset_name: DiscordChatExporter.zip
|
||||||
asset_content_type: application/zip
|
asset_content_type: application/zip
|
||||||
|
|
||||||
|
- name: Notify Discord
|
||||||
|
uses: satak/webrequest-action@v1.2.4
|
||||||
|
with:
|
||||||
|
url: ${{ secrets.DISCORD_WEBHOOK }}
|
||||||
|
method: POST
|
||||||
|
headers: |
|
||||||
|
{
|
||||||
|
"ContentType": "application/json; charset=UTF-8"
|
||||||
|
}
|
||||||
|
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>"
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
name: Docker CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2.3.3
|
||||||
|
|
||||||
|
- name: Login
|
||||||
|
run: docker login -u tyrrrz -p ${{ secrets.DOCKER_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: docker build -t tyrrrz/discordchatexporter:latest .
|
||||||
|
|
||||||
|
- name: Deploy (latest)
|
||||||
|
run: docker push tyrrrz/discordchatexporter:latest
|
||||||
@@ -10,15 +10,24 @@ jobs:
|
|||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2.3.3
|
uses: actions/checkout@v2.3.3
|
||||||
|
|
||||||
- name: Install .NET (v3.1)
|
- name: Install .NET
|
||||||
uses: actions/setup-dotnet@v1.7.2
|
uses: actions/setup-dotnet@v1.7.2
|
||||||
with:
|
with:
|
||||||
dotnet-version: 3.1.x
|
# Fixed version, workaround for https://github.com/dotnet/core/issues/7176
|
||||||
|
dotnet-version: 6.0.100
|
||||||
|
|
||||||
- name: Install .NET (v5.0)
|
- name: Build & test
|
||||||
uses: actions/setup-dotnet@v1.7.2
|
# Tests need access to secrets, so we can't run them against PRs because of limited trust
|
||||||
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
|
run: dotnet test --configuration Release --logger GitHubActions --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
|
||||||
|
env:
|
||||||
|
DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }}
|
||||||
|
DISCORD_TOKEN_BOT: true
|
||||||
|
|
||||||
|
- name: Upload coverage
|
||||||
|
uses: codecov/codecov-action@v1.0.5
|
||||||
with:
|
with:
|
||||||
dotnet-version: 5.0.x
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
|
||||||
- name: Build & publish (CLI)
|
- name: Build & publish (CLI)
|
||||||
run: dotnet publish DiscordChatExporter.Cli/ -o DiscordChatExporter.Cli/bin/Publish/ --configuration Release
|
run: dotnet publish DiscordChatExporter.Cli/ -o DiscordChatExporter.Cli/bin/Publish/ --configuration Release
|
||||||
|
|||||||
+4
-1
@@ -19,4 +19,7 @@ bld/
|
|||||||
[Oo]bj/
|
[Oo]bj/
|
||||||
|
|
||||||
# Coverage
|
# Coverage
|
||||||
*.opencover.xml
|
*.opencover.xml
|
||||||
|
|
||||||
|
# Secrets
|
||||||
|
*.secret
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 45 KiB |
@@ -1,3 +1,73 @@
|
|||||||
|
### v2.34 (10-Apr-2022)
|
||||||
|
|
||||||
|
- [HTML] Reworked layout to be more consistent with Discord.
|
||||||
|
- [HTML] Changed user profile picture dimensions from `128x128` to `512x512`. (Thanks [@Chris Helder](https://github.com/TheDude53))
|
||||||
|
- [HTML] Improved image scaling for Chromium-based browsers. This should result in higher quality images across the entire export.
|
||||||
|
- [HTML] Hovering over messages will now show short timestamps on the left, indicating the time that the individual messages of the group were sent.
|
||||||
|
- [HTML] Using "download media" option will now also download scripts, styles, fonts, and other similar resources used by the export.
|
||||||
|
- [HTML] Image attachments will now use the provided description as alt text.
|
||||||
|
- [HTML] Fixed a few layout inconsistencies related to embeds.
|
||||||
|
- [GUI] Added a few clickable links to the usage guide to make the process of locating tokens easier.
|
||||||
|
|
||||||
|
### v2.33.2 (07-Mar-2022)
|
||||||
|
|
||||||
|
- Actually fixed it this time.
|
||||||
|
|
||||||
|
### v2.33.1 (07-Mar-2022)
|
||||||
|
|
||||||
|
- Fixed an issue where the application silently failed to run if the system didn't have .NET Runtime 6.0.2 installed. If you continue seeing this issue, please uninstall all existing .NET runtimes from your computer and then try running the application again.
|
||||||
|
|
||||||
|
### v2.33 (06-Mar-2022)
|
||||||
|
|
||||||
|
- Added messages informing about war in Ukraine and available ways to help.
|
||||||
|
- Added support for rendering stickers in HTML and JSON. Lottie-based stickers currently cannot be displayed in HTML exports (see [#803](https://github.com/Tyrrrz/DiscordChatExporter/issues/803)).
|
||||||
|
- Added a new `reaction:` message filter which can be used to check if someone reacted to a message with a specific emoji. You can either pass the emoji name (e.g. `reaction:LUL`) or its ID (e.g. `reaction:41771983429993937`).
|
||||||
|
- [GUI] Added auto-detection for dark mode. If your system is configured to prefer dark mode in applications, DiscordChatExporter will use it by default instead of light mode.
|
||||||
|
- Fixed an issue which caused the export process to crash when downloading media files with extremely long file extensions. (Thanks [@Tomlacko](https://github.com/Tomlacko))
|
||||||
|
- Fixed an issue which caused the export process to crash on invalid mentions.
|
||||||
|
- [GUI] Fixed an issue where the time pickers used to specify export ranges always displayed time in 12-hour format, instead of respecting the system locale.
|
||||||
|
|
||||||
|
### v2.32 (27-Jan-2022)
|
||||||
|
|
||||||
|
- Token kind (user or bot) is now detected automatically. Removed the button to switch token kind in GUI. Option `-b|--bot` in CLI is now deprecated and does nothing.
|
||||||
|
- Updated user token extraction guide to reflect the fact that devtools are no longer accessible in the desktop version of Discord client. The recommended workaround is to open Discord in browser. (Thanks [@Dhananjay-JSR](https://github.com/Dhananjay-JSR))
|
||||||
|
- Fixed an issue which caused an error when parsing invalid date timestamps. Such timestamps are now rendered as "Invalid date", similarly to how the Discord client does it.
|
||||||
|
- Fixed an issue which caused an error when parsing certain embed fields.
|
||||||
|
- Fixed an issue which caused an error when parsing a mention to a user that has no username.
|
||||||
|
- [GUI] Minor visual fixes.
|
||||||
|
|
||||||
|
### v2.31.1 (15-Dec-2021)
|
||||||
|
|
||||||
|
- Fixed an issue which caused an error when parsing certain custom emoji reactions.
|
||||||
|
- [GUI] Improved user interface.
|
||||||
|
|
||||||
|
### v2.31 (06-Dec-2021)
|
||||||
|
|
||||||
|
- Changed target runtime of the application (both GUI and CLI) from .NET 3.1 to .NET 6. Those using the GUI version will not need to take any action as the application should install the necessary prerequisites automatically. Those using the CLI version will need to download and install .NET 6 runtime manually [from here](https://dotnet.microsoft.com/download/dotnet/6.0) (look for ".NET Runtime 6.0.x" section on the right and choose the distribution appropriate for your system).
|
||||||
|
- [HTML] Updated layout design to match Discord more closely. (Thanks [@Artem](https://github.com/Scarg))
|
||||||
|
- [HTML] Fixed an issue where markdown inside channel topic was not rendered correctly. (Thanks [@Emily](https://github.com/emillly-b))
|
||||||
|
- [HTML] Fixed an issue where the layout was broken for messages containing text in right-to-left languages. (Thanks [@Emily](https://github.com/emillly-b))
|
||||||
|
- Fixed an issue where text filters weren't matching content inside of embeds.
|
||||||
|
- [GUI] Fixed a multitude of issues related to the bootstrapper.
|
||||||
|
|
||||||
|
### v2.30.1 (28-Aug-2021)
|
||||||
|
|
||||||
|
- [HTML] Changed user avatars to render using higher resolution images. (Thanks [@Chris Helder](https://github.com/TheDude53))
|
||||||
|
- Fixed an issue where `has:file` message filter was not recognized. (Thanks [@Lucas LaBuff](https://github.com/96-LB))
|
||||||
|
- Fixed an issue where a unix timestamp marker with an invalid offset crashed the export process.
|
||||||
|
|
||||||
|
### v2.30 (08-Aug-2021)
|
||||||
|
|
||||||
|
- [HTML] Added special casing for Spotify track embeds to render them directly using an embedded player.
|
||||||
|
- [HTML] Added special casing for plain image embeds to render them like image attachments.
|
||||||
|
- [HTML] Added tooltip text for unix timestamp markers which shows the full date represented by the timestamp, regardless of configured date format.
|
||||||
|
- [HTML] Updated the fallback text shown when the referenced message could not be loaded to make it more explicit.
|
||||||
|
- [HTML] Updated styling of quoted text in markdown to more closely resemble how it looks in Discord.
|
||||||
|
- [HTML] Fixed an issue where some file formats were not correctly recognized as image or video attachments, which affected how they were rendered.
|
||||||
|
- [HTML] Fixed an issue where formatting inside named links (e.g. `[**Bold link**](https://example.com)`) was not being parsed.
|
||||||
|
- [HTML] Fixed an issue where inline embed fields were missing horizontal spacing between columns.
|
||||||
|
- [HTML] Fixed other minor styling issues.
|
||||||
|
|
||||||
### v2.29 (19-Jul-2021)
|
### v2.29 (19-Jul-2021)
|
||||||
|
|
||||||
- Added support for message filtering, which is accessible as an additional export parameter in the GUI and as the `--filter` option in the CLI. Message filters use a special syntax similar to Discord's search query syntax to allow specifying which messages should be included in the export. For example, you can limit the export to only include messages that contain image attachments (`has:image`) or messages from a certain user (`from:Tyrrrz`) or messages containing certain words (`hello world`), etc. You can also combine filters together (`hi mentions:Tyrrrz`, `from:Tyrrrz | from:96-LB`, `(mentions:Tyrrrz | from:Tyrrrz) has:image`) or negate them (`-has:link`). More thorough documentation coming soon. (Thanks [@Lucas LaBuff](https://github.com/96-LB))
|
- Added support for message filtering, which is accessible as an additional export parameter in the GUI and as the `--filter` option in the CLI. Message filters use a special syntax similar to Discord's search query syntax to allow specifying which messages should be included in the export. For example, you can limit the export to only include messages that contain image attachments (`has:image`) or messages from a certain user (`from:Tyrrrz`) or messages containing certain words (`hello world`), etc. You can also combine filters together (`hi mentions:Tyrrrz`, `from:Tyrrrz | from:96-LB`, `(mentions:Tyrrrz | from:Tyrrrz) has:image`) or negate them (`-has:link`). More thorough documentation coming soon. (Thanks [@Lucas LaBuff](https://github.com/96-LB))
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
<Project>
|
<Project>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<Version>2.29</Version>
|
<Version>2.34</Version>
|
||||||
<Company>Tyrrrz</Company>
|
<Company>Tyrrrz</Company>
|
||||||
<Copyright>Copyright (c) Alexey Golub</Copyright>
|
<Copyright>Copyright (c) Oleksii Holub</Copyright>
|
||||||
<LangVersion>preview</LangVersion>
|
<LangVersion>preview</LangVersion>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<WarningsAsErrors>nullable</WarningsAsErrors>
|
<WarningsAsErrors>nullable</WarningsAsErrors>
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
|
<NoWarn>$(NoWarn);xUnit1013</NoWarn>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
|
||||||
|
<None Include="*.secret" CopyToOutputDirectory="PreserveNewest" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AngleSharp" Version="0.16.1" />
|
||||||
|
<PackageReference Include="FluentAssertions" Version="6.6.0" />
|
||||||
|
<PackageReference Include="GitHubActionsTestLogger" Version="1.3.0" PrivateAssets="all" />
|
||||||
|
<PackageReference Include="JsonExtensions" Version="1.2.0" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||||
|
<PackageReference Include="System.Reactive" Version="5.0.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
|
||||||
|
<PackageReference Include="coverlet.collector" Version="3.1.2" PrivateAssets="all" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\DiscordChatExporter.Cli\DiscordChatExporter.Cli.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AngleSharp.Dom;
|
||||||
|
using AngleSharp.Html.Dom;
|
||||||
|
using CliFx.Infrastructure;
|
||||||
|
using DiscordChatExporter.Cli.Commands;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Infra;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Utils;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using DiscordChatExporter.Core.Exporting;
|
||||||
|
using JsonExtensions;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
|
||||||
|
public class ExportWrapperFixture : IDisposable
|
||||||
|
{
|
||||||
|
private string DirPath { get; } = Path.Combine(
|
||||||
|
Path.GetDirectoryName(typeof(ExportWrapperFixture).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||||
|
"ExportCache",
|
||||||
|
Guid.NewGuid().ToString()
|
||||||
|
);
|
||||||
|
|
||||||
|
public ExportWrapperFixture() => DirectoryEx.Reset(DirPath);
|
||||||
|
|
||||||
|
private async ValueTask<string> ExportAsync(Snowflake channelId, ExportFormat format)
|
||||||
|
{
|
||||||
|
var fileName = channelId.ToString() + '.' + format.GetFileExtension();
|
||||||
|
var filePath = Path.Combine(DirPath, fileName);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId)
|
||||||
|
{
|
||||||
|
var data = await ExportAsync(channelId, ExportFormat.HtmlDark);
|
||||||
|
return Html.Parse(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<JsonElement> ExportAsJsonAsync(Snowflake channelId)
|
||||||
|
{
|
||||||
|
var data = await ExportAsync(channelId, ExportFormat.Json);
|
||||||
|
return Json.Parse(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<string> ExportAsPlainTextAsync(Snowflake channelId)
|
||||||
|
{
|
||||||
|
var data = await ExportAsync(channelId, ExportFormat.PlainText);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<string> ExportAsCsvAsync(Snowflake channelId)
|
||||||
|
{
|
||||||
|
var data = await ExportAsync(channelId, ExportFormat.Csv);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<IReadOnlyList<IElement>> GetMessagesAsHtmlAsync(Snowflake channelId)
|
||||||
|
{
|
||||||
|
var document = await ExportAsHtmlAsync(channelId);
|
||||||
|
return document.QuerySelectorAll("[data-message-id]").ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<IReadOnlyList<JsonElement>> GetMessagesAsJsonAsync(Snowflake channelId)
|
||||||
|
{
|
||||||
|
var document = await ExportAsJsonAsync(channelId);
|
||||||
|
return document.GetProperty("messages").EnumerateArray().ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<IElement> GetMessageAsHtmlAsync(Snowflake channelId, Snowflake messageId)
|
||||||
|
{
|
||||||
|
var messages = await GetMessagesAsHtmlAsync(channelId);
|
||||||
|
|
||||||
|
var message = messages.SingleOrDefault(e =>
|
||||||
|
string.Equals(
|
||||||
|
e.GetAttribute("data-message-id"),
|
||||||
|
messageId.ToString(),
|
||||||
|
StringComparison.OrdinalIgnoreCase
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (message is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
$"Message '{messageId}' does not exist in export of channel '{channelId}'."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<JsonElement> GetMessageAsJsonAsync(Snowflake channelId, Snowflake messageId)
|
||||||
|
{
|
||||||
|
var messages = await GetMessagesAsJsonAsync(channelId);
|
||||||
|
|
||||||
|
var message = messages.FirstOrDefault(j =>
|
||||||
|
string.Equals(
|
||||||
|
j.GetProperty("id").GetString(),
|
||||||
|
messageId.ToString(),
|
||||||
|
StringComparison.OrdinalIgnoreCase
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (message.ValueKind == JsonValueKind.Undefined)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
$"Message '{messageId}' does not exist in export of channel '{channelId}'."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() => DirectoryEx.DeleteIfExists(DirPath);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Utils;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
|
||||||
|
public class TempOutputFixture : IDisposable
|
||||||
|
{
|
||||||
|
public string DirPath { get; } = Path.Combine(
|
||||||
|
Path.GetDirectoryName(typeof(TempOutputFixture).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||||
|
"Temp",
|
||||||
|
Guid.NewGuid().ToString()
|
||||||
|
);
|
||||||
|
|
||||||
|
public TempOutputFixture() => DirectoryEx.Reset(DirPath);
|
||||||
|
|
||||||
|
public string GetTempFilePath(string fileName) => Path.Combine(DirPath, fileName);
|
||||||
|
|
||||||
|
public string GetTempFilePath() => GetTempFilePath(Guid.NewGuid() + ".tmp");
|
||||||
|
|
||||||
|
public void Dispose() => DirectoryEx.DeleteIfExists(DirPath);
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Infra;
|
||||||
|
|
||||||
|
internal static class Secrets
|
||||||
|
{
|
||||||
|
private static readonly Lazy<string> DiscordTokenLazy = new(() =>
|
||||||
|
{
|
||||||
|
var fromEnvironment = Environment.GetEnvironmentVariable("DISCORD_TOKEN");
|
||||||
|
if (!string.IsNullOrWhiteSpace(fromEnvironment))
|
||||||
|
return fromEnvironment;
|
||||||
|
|
||||||
|
var secretFilePath = Path.Combine(
|
||||||
|
Path.GetDirectoryName(typeof(Secrets).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||||
|
"DiscordToken.secret"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (File.Exists(secretFilePath))
|
||||||
|
return File.ReadAllText(secretFilePath);
|
||||||
|
|
||||||
|
throw new InvalidOperationException("Discord token not provided for tests.");
|
||||||
|
});
|
||||||
|
|
||||||
|
public static string DiscordToken => DiscordTokenLazy.Value;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
# DiscordChatExporter Tests
|
||||||
|
|
||||||
|
This test suite runs against a real Discord server, specifically created to exercise different behaviors required by the test scenarios.
|
||||||
|
In order to run these tests locally, you need to join the test server and configure your authentication token.
|
||||||
|
|
||||||
|
1. Join the server: https://discord.gg/eRV8Vap5bm
|
||||||
|
2. Locate your Discord authentication token
|
||||||
|
3. Specify your token using a file or an environment variable:
|
||||||
|
- **Using a file**: put your token in a new `DiscordToken.secret` file created in this directory
|
||||||
|
- **Using an environment variable**: set `DISCORD_TOKEN` variable to your token
|
||||||
|
4. Run the tests: `dotnet test`
|
||||||
|
|
||||||
|
> If you want to have a new test case or a scenario added, please let me know in your pull request.
|
||||||
|
Currently, it's not possible to add them by yourself.
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.CsvWriting;
|
||||||
|
|
||||||
|
public record ContentSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_are_exported_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var document = await ExportWrapper.ExportAsCsvAsync(ChannelIds.DateRangeTestCases);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
document.Should().ContainAll(
|
||||||
|
"Tyrrrz#5447",
|
||||||
|
"Hello world",
|
||||||
|
"Goodbye world",
|
||||||
|
"Foo bar",
|
||||||
|
"Hurdle Durdle",
|
||||||
|
"One",
|
||||||
|
"Two",
|
||||||
|
"Three",
|
||||||
|
"Yeet"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Infrastructure;
|
||||||
|
using DiscordChatExporter.Cli.Commands;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Infra;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using DiscordChatExporter.Core.Exporting;
|
||||||
|
using FluentAssertions;
|
||||||
|
using JsonExtensions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs;
|
||||||
|
|
||||||
|
public record DateRangeSpecs(TempOutputFixture TempOutput) : IClassFixture<TempOutputFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_filtered_after_specific_date_only_include_messages_sent_after_that_date()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var after = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero);
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.DateRangeTestCases },
|
||||||
|
ExportFormat = ExportFormat.Json,
|
||||||
|
OutputPath = filePath,
|
||||||
|
After = Snowflake.FromDate(after)
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
var data = await File.ReadAllTextAsync(filePath);
|
||||||
|
var document = Json.Parse(data);
|
||||||
|
|
||||||
|
var timestamps = document
|
||||||
|
.GetProperty("messages")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetProperty("timestamp").GetDateTimeOffset())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
timestamps.All(t => t > after).Should().BeTrue();
|
||||||
|
|
||||||
|
timestamps.Should().BeEquivalentTo(new[]
|
||||||
|
{
|
||||||
|
new DateTimeOffset(2021, 07, 24, 13, 49, 13, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 07, 24, 14, 52, 38, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 07, 24, 14, 52, 39, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 07, 24, 14, 52, 40, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 09, 08, 14, 26, 35, TimeSpan.Zero)
|
||||||
|
}, o =>
|
||||||
|
{
|
||||||
|
return o
|
||||||
|
.Using<DateTimeOffset>(ctx =>
|
||||||
|
ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1))
|
||||||
|
)
|
||||||
|
.WhenTypeIs<DateTimeOffset>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_filtered_before_specific_date_only_include_messages_sent_before_that_date()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var before = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero);
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.DateRangeTestCases },
|
||||||
|
ExportFormat = ExportFormat.Json,
|
||||||
|
OutputPath = filePath,
|
||||||
|
Before = Snowflake.FromDate(before)
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
var data = await File.ReadAllTextAsync(filePath);
|
||||||
|
var document = Json.Parse(data);
|
||||||
|
|
||||||
|
var timestamps = document
|
||||||
|
.GetProperty("messages")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetProperty("timestamp").GetDateTimeOffset())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
timestamps.All(t => t < before).Should().BeTrue();
|
||||||
|
|
||||||
|
timestamps.Should().BeEquivalentTo(new[]
|
||||||
|
{
|
||||||
|
new DateTimeOffset(2021, 07, 19, 13, 34, 18, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 07, 19, 15, 58, 48, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 07, 19, 17, 23, 58, TimeSpan.Zero)
|
||||||
|
}, o =>
|
||||||
|
{
|
||||||
|
return o
|
||||||
|
.Using<DateTimeOffset>(ctx =>
|
||||||
|
ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1))
|
||||||
|
)
|
||||||
|
.WhenTypeIs<DateTimeOffset>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_filtered_between_specific_dates_only_include_messages_sent_between_those_dates()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var after = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero);
|
||||||
|
var before = new DateTimeOffset(2021, 08, 01, 0, 0, 0, TimeSpan.Zero);
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.DateRangeTestCases },
|
||||||
|
ExportFormat = ExportFormat.Json,
|
||||||
|
OutputPath = filePath,
|
||||||
|
Before = Snowflake.FromDate(before),
|
||||||
|
After = Snowflake.FromDate(after)
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
var data = await File.ReadAllTextAsync(filePath);
|
||||||
|
var document = Json.Parse(data);
|
||||||
|
|
||||||
|
var timestamps = document
|
||||||
|
.GetProperty("messages")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetProperty("timestamp").GetDateTimeOffset())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
timestamps.All(t => t < before && t > after).Should().BeTrue();
|
||||||
|
|
||||||
|
timestamps.Should().BeEquivalentTo(new[]
|
||||||
|
{
|
||||||
|
new DateTimeOffset(2021, 07, 24, 13, 49, 13, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 07, 24, 14, 52, 38, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 07, 24, 14, 52, 39, TimeSpan.Zero),
|
||||||
|
new DateTimeOffset(2021, 07, 24, 14, 52, 40, TimeSpan.Zero)
|
||||||
|
}, o =>
|
||||||
|
{
|
||||||
|
return o
|
||||||
|
.Using<DateTimeOffset>(ctx =>
|
||||||
|
ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1))
|
||||||
|
)
|
||||||
|
.WhenTypeIs<DateTimeOffset>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Infrastructure;
|
||||||
|
using DiscordChatExporter.Cli.Commands;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Infra;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Exporting;
|
||||||
|
using DiscordChatExporter.Core.Exporting.Filtering;
|
||||||
|
using FluentAssertions;
|
||||||
|
using JsonExtensions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs;
|
||||||
|
|
||||||
|
public record FilterSpecs(TempOutputFixture TempOutput) : IClassFixture<TempOutputFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_filtered_by_text_only_include_messages_that_contain_that_text()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.FilterTestCases },
|
||||||
|
ExportFormat = ExportFormat.Json,
|
||||||
|
OutputPath = filePath,
|
||||||
|
MessageFilter = MessageFilter.Parse("some text")
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
var data = await File.ReadAllTextAsync(filePath);
|
||||||
|
var document = Json.Parse(data);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
document
|
||||||
|
.GetProperty("messages")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetProperty("content").GetString())
|
||||||
|
.Should()
|
||||||
|
.ContainSingle("Some random text");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_filtered_by_author_only_include_messages_sent_by_that_author()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.FilterTestCases },
|
||||||
|
ExportFormat = ExportFormat.Json,
|
||||||
|
OutputPath = filePath,
|
||||||
|
MessageFilter = MessageFilter.Parse("from:Tyrrrz")
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
var data = await File.ReadAllTextAsync(filePath);
|
||||||
|
var document = Json.Parse(data);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
document
|
||||||
|
.GetProperty("messages")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetProperty("author").GetProperty("name").GetString())
|
||||||
|
.Should()
|
||||||
|
.AllBe("Tyrrrz");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_filtered_by_content_only_include_messages_that_have_that_content()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.FilterTestCases },
|
||||||
|
ExportFormat = ExportFormat.Json,
|
||||||
|
OutputPath = filePath,
|
||||||
|
MessageFilter = MessageFilter.Parse("has:image")
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
var data = await File.ReadAllTextAsync(filePath);
|
||||||
|
var document = Json.Parse(data);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
document
|
||||||
|
.GetProperty("messages")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetProperty("content").GetString())
|
||||||
|
.Should()
|
||||||
|
.ContainSingle("This has image");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_filtered_by_mention_only_include_messages_that_have_that_mention()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.FilterTestCases },
|
||||||
|
ExportFormat = ExportFormat.Json,
|
||||||
|
OutputPath = filePath,
|
||||||
|
MessageFilter = MessageFilter.Parse("mentions:Tyrrrz")
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
var data = await File.ReadAllTextAsync(filePath);
|
||||||
|
var document = Json.Parse(data);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
document
|
||||||
|
.GetProperty("messages")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetProperty("content").GetString())
|
||||||
|
.Should()
|
||||||
|
.ContainSingle("This has mention");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using AngleSharp.Dom;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.HtmlWriting;
|
||||||
|
|
||||||
|
public record AttachmentSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_generic_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.AttachmentTestCases,
|
||||||
|
Snowflake.Parse("885587844989612074")
|
||||||
|
);
|
||||||
|
|
||||||
|
var fileUrl = message.QuerySelector("a")?.GetAttribute("href");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Should().ContainAll(
|
||||||
|
"Generic file attachment",
|
||||||
|
"Test.txt",
|
||||||
|
"11 bytes"
|
||||||
|
);
|
||||||
|
|
||||||
|
fileUrl.Should().StartWithEquivalentOf(
|
||||||
|
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_an_image_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.AttachmentTestCases,
|
||||||
|
Snowflake.Parse("885654862656843786")
|
||||||
|
);
|
||||||
|
|
||||||
|
var imageUrl = message.QuerySelector(".chatlog__attachment img")?.GetAttribute("src");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Should().Contain("Image attachment");
|
||||||
|
|
||||||
|
imageUrl.Should().StartWithEquivalentOf(
|
||||||
|
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_video_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.AttachmentTestCases,
|
||||||
|
Snowflake.Parse("885655761919836171")
|
||||||
|
);
|
||||||
|
|
||||||
|
var videoUrl = message.QuerySelector("video source")?.GetAttribute("src");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Should().Contain("Video attachment");
|
||||||
|
|
||||||
|
videoUrl.Should().StartWithEquivalentOf(
|
||||||
|
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_an_audio_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.AttachmentTestCases,
|
||||||
|
Snowflake.Parse("885656175620808734")
|
||||||
|
);
|
||||||
|
|
||||||
|
var audioUrl = message.QuerySelector("audio source")?.GetAttribute("src");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Should().Contain("Audio attachment");
|
||||||
|
|
||||||
|
audioUrl.Should().StartWithEquivalentOf(
|
||||||
|
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AngleSharp.Dom;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.HtmlWriting;
|
||||||
|
|
||||||
|
public record ContentSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_are_exported_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var messages = await ExportWrapper.GetMessagesAsHtmlAsync(ChannelIds.DateRangeTestCases);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
messages.Select(e => e.GetAttribute("data-message-id")).Should().Equal(
|
||||||
|
"866674314627121232",
|
||||||
|
"866710679758045195",
|
||||||
|
"866732113319428096",
|
||||||
|
"868490009366396958",
|
||||||
|
"868505966528835604",
|
||||||
|
"868505969821364245",
|
||||||
|
"868505973294268457",
|
||||||
|
"885169254029213696"
|
||||||
|
);
|
||||||
|
|
||||||
|
messages.Select(e => e.QuerySelector(".chatlog__content")?.Text().Trim()).Should().Equal(
|
||||||
|
"Hello world",
|
||||||
|
"Goodbye world",
|
||||||
|
"Foo bar",
|
||||||
|
"Hurdle Durdle",
|
||||||
|
"One",
|
||||||
|
"Two",
|
||||||
|
"Three",
|
||||||
|
"Yeet"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using AngleSharp.Dom;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.HtmlWriting;
|
||||||
|
|
||||||
|
public record EmbedSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_an_embed_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.EmbedTestCases,
|
||||||
|
Snowflake.Parse("866769910729146400")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Should().ContainAll(
|
||||||
|
"Embed author",
|
||||||
|
"Embed title",
|
||||||
|
"Embed description",
|
||||||
|
"Field 1", "Value 1",
|
||||||
|
"Field 2", "Value 2",
|
||||||
|
"Field 3", "Value 3",
|
||||||
|
"Embed footer"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_Spotify_track_is_rendered_using_an_iframe()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.EmbedTestCases,
|
||||||
|
Snowflake.Parse("867886632203976775")
|
||||||
|
);
|
||||||
|
|
||||||
|
var iframeSrc = message.QuerySelector("iframe")?.GetAttribute("src");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
iframeSrc.Should().StartWithEquivalentOf("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_YouTube_video_is_rendered_using_an_iframe()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.EmbedTestCases,
|
||||||
|
Snowflake.Parse("866472508588294165")
|
||||||
|
);
|
||||||
|
|
||||||
|
var iframeSrc = message.QuerySelector("iframe")?.GetAttribute("src");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
iframeSrc.Should().StartWithEquivalentOf("https://www.youtube.com/embed/qOWW4OlgbvE");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using AngleSharp.Dom;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.HtmlWriting;
|
||||||
|
|
||||||
|
public record MentionSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task User_mention_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.MentionTestCases,
|
||||||
|
Snowflake.Parse("866458840245076028")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Trim().Should().Contain("User mention: @Tyrrrz");
|
||||||
|
message.InnerHtml.Should().Contain("Tyrrrz#5447");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Text_channel_mention_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.MentionTestCases,
|
||||||
|
Snowflake.Parse("866459040480624680")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Trim().Should().Contain("Text channel mention: #mention-tests");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Voice_channel_mention_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.MentionTestCases,
|
||||||
|
Snowflake.Parse("866459175462633503")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Trim().Should().Contain("Voice channel mention: 🔊chaos-vc");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Role_mention_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.MentionTestCases,
|
||||||
|
Snowflake.Parse("866459254693429258")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Trim().Should().Contain("Role mention: @Role 1");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using AngleSharp.Dom;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.HtmlWriting;
|
||||||
|
|
||||||
|
public record ReplySpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Reply_to_a_normal_message_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.ReplyTestCases,
|
||||||
|
Snowflake.Parse("866460738239725598")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Trim().Should().Contain("reply to original");
|
||||||
|
message.QuerySelector(".chatlog__reference-link")?.Text().Trim().Should().Contain("original");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reply_to_a_deleted_message_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.ReplyTestCases,
|
||||||
|
Snowflake.Parse("866460975388819486")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Trim().Should().Contain("reply to deleted");
|
||||||
|
message.QuerySelector(".chatlog__reference-link")?.Text().Trim().Should()
|
||||||
|
.Contain("Original message was deleted or could not be loaded.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reply_to_an_empty_message_with_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.ReplyTestCases,
|
||||||
|
Snowflake.Parse("866462470335627294")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.Text().Trim().Should().Contain("reply to attachment");
|
||||||
|
message.QuerySelector(".chatlog__reference-link")?.Text().Trim().Should()
|
||||||
|
.Contain("Click to see attachment");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.HtmlWriting;
|
||||||
|
|
||||||
|
public record StickerSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_PNG_based_sticker_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.StickerTestCases,
|
||||||
|
Snowflake.Parse("939670623158943754")
|
||||||
|
);
|
||||||
|
|
||||||
|
var container = message.QuerySelector("[title='rock']");
|
||||||
|
var sourceUrl = container?.QuerySelector("img")?.GetAttribute("src");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
container.Should().NotBeNull();
|
||||||
|
sourceUrl.Should().Be("https://discord.com/stickers/904215665597120572.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_Lottie_based_sticker_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsHtmlAsync(
|
||||||
|
ChannelIds.StickerTestCases,
|
||||||
|
Snowflake.Parse("939670526517997590")
|
||||||
|
);
|
||||||
|
|
||||||
|
var container = message.QuerySelector("[title='Yikes']");
|
||||||
|
var sourceUrl = container?.QuerySelector("div[data-source]")?.GetAttribute("data-source");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
container.Should().NotBeNull();
|
||||||
|
sourceUrl.Should().Be("https://discord.com/stickers/816087132447178774.json");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.JsonWriting;
|
||||||
|
|
||||||
|
public record AttachmentSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_generic_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.AttachmentTestCases,
|
||||||
|
Snowflake.Parse("885587844989612074")
|
||||||
|
);
|
||||||
|
|
||||||
|
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.GetProperty("content").GetString().Should().Be("Generic file attachment");
|
||||||
|
|
||||||
|
attachments.Should().HaveCount(1);
|
||||||
|
attachments.Single().GetProperty("url").GetString().Should().StartWithEquivalentOf(
|
||||||
|
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
|
||||||
|
);
|
||||||
|
attachments.Single().GetProperty("fileName").GetString().Should().Be("Test.txt");
|
||||||
|
attachments.Single().GetProperty("fileSizeBytes").GetInt64().Should().Be(11);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_an_image_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.AttachmentTestCases,
|
||||||
|
Snowflake.Parse("885654862656843786")
|
||||||
|
);
|
||||||
|
|
||||||
|
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.GetProperty("content").GetString().Should().Be("Image attachment");
|
||||||
|
|
||||||
|
attachments.Should().HaveCount(1);
|
||||||
|
attachments.Single().GetProperty("url").GetString().Should().StartWithEquivalentOf(
|
||||||
|
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
|
||||||
|
);
|
||||||
|
attachments.Single().GetProperty("fileName").GetString().Should().Be("bird-thumbnail.png");
|
||||||
|
attachments.Single().GetProperty("fileSizeBytes").GetInt64().Should().Be(466335);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_video_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.AttachmentTestCases,
|
||||||
|
Snowflake.Parse("885655761919836171")
|
||||||
|
);
|
||||||
|
|
||||||
|
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.GetProperty("content").GetString().Should().Be("Video attachment");
|
||||||
|
|
||||||
|
attachments.Should().HaveCount(1);
|
||||||
|
attachments.Single().GetProperty("url").GetString().Should().StartWithEquivalentOf(
|
||||||
|
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
|
||||||
|
);
|
||||||
|
attachments.Single().GetProperty("fileName").GetString().Should().Be("file_example_MP4_640_3MG.mp4");
|
||||||
|
attachments.Single().GetProperty("fileSizeBytes").GetInt64().Should().Be(3114374);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_an_audio_attachment_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.AttachmentTestCases,
|
||||||
|
Snowflake.Parse("885656175620808734")
|
||||||
|
);
|
||||||
|
|
||||||
|
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.GetProperty("content").GetString().Should().Be("Audio attachment");
|
||||||
|
|
||||||
|
attachments.Should().HaveCount(1);
|
||||||
|
attachments.Single().GetProperty("url").GetString().Should().StartWithEquivalentOf(
|
||||||
|
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
|
||||||
|
);
|
||||||
|
attachments.Single().GetProperty("fileName").GetString().Should().Be("file_example_MP3_1MG.mp3");
|
||||||
|
attachments.Single().GetProperty("fileSizeBytes").GetInt64().Should().Be(1087849);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.JsonWriting;
|
||||||
|
|
||||||
|
public record ContentSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_are_exported_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var messages = await ExportWrapper.GetMessagesAsJsonAsync(ChannelIds.DateRangeTestCases);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
messages.Select(j => j.GetProperty("id").GetString()).Should().Equal(
|
||||||
|
"866674314627121232",
|
||||||
|
"866710679758045195",
|
||||||
|
"866732113319428096",
|
||||||
|
"868490009366396958",
|
||||||
|
"868505966528835604",
|
||||||
|
"868505969821364245",
|
||||||
|
"868505973294268457",
|
||||||
|
"885169254029213696"
|
||||||
|
);
|
||||||
|
|
||||||
|
messages.Select(j => j.GetProperty("content").GetString()).Should().Equal(
|
||||||
|
"Hello world",
|
||||||
|
"Goodbye world",
|
||||||
|
"Foo bar",
|
||||||
|
"Hurdle Durdle",
|
||||||
|
"One",
|
||||||
|
"Two",
|
||||||
|
"Three",
|
||||||
|
"Yeet"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.JsonWriting;
|
||||||
|
|
||||||
|
public record EmbedSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_an_embed_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.EmbedTestCases,
|
||||||
|
Snowflake.Parse("866769910729146400")
|
||||||
|
);
|
||||||
|
|
||||||
|
var embed = message
|
||||||
|
.GetProperty("embeds")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Single();
|
||||||
|
|
||||||
|
var embedAuthor = embed.GetProperty("author");
|
||||||
|
var embedThumbnail = embed.GetProperty("thumbnail");
|
||||||
|
var embedFooter = embed.GetProperty("footer");
|
||||||
|
var embedFields = embed.GetProperty("fields").EnumerateArray().ToArray();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
embed.GetProperty("title").GetString().Should().Be("Embed title");
|
||||||
|
embed.GetProperty("url").GetString().Should().Be("https://example.com");
|
||||||
|
embed.GetProperty("timestamp").GetString().Should().Be("2021-07-14T21:00:00+00:00");
|
||||||
|
embed.GetProperty("description").GetString().Should().Be("**Embed** _description_");
|
||||||
|
embed.GetProperty("color").GetString().Should().Be("#58B9FF");
|
||||||
|
embedAuthor.GetProperty("name").GetString().Should().Be("Embed author");
|
||||||
|
embedAuthor.GetProperty("url").GetString().Should().Be("https://example.com/author");
|
||||||
|
embedAuthor.GetProperty("iconUrl").GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
embedThumbnail.GetProperty("url").GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
embedThumbnail.GetProperty("width").GetInt32().Should().Be(120);
|
||||||
|
embedThumbnail.GetProperty("height").GetInt32().Should().Be(120);
|
||||||
|
embedFooter.GetProperty("text").GetString().Should().Be("Embed footer");
|
||||||
|
embedFooter.GetProperty("iconUrl").GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
embedFields.Should().HaveCount(3);
|
||||||
|
embedFields[0].GetProperty("name").GetString().Should().Be("Field 1");
|
||||||
|
embedFields[0].GetProperty("value").GetString().Should().Be("Value 1");
|
||||||
|
embedFields[0].GetProperty("isInline").GetBoolean().Should().BeTrue();
|
||||||
|
embedFields[1].GetProperty("name").GetString().Should().Be("Field 2");
|
||||||
|
embedFields[1].GetProperty("value").GetString().Should().Be("Value 2");
|
||||||
|
embedFields[1].GetProperty("isInline").GetBoolean().Should().BeTrue();
|
||||||
|
embedFields[2].GetProperty("name").GetString().Should().Be("Field 3");
|
||||||
|
embedFields[2].GetProperty("value").GetString().Should().Be("Value 3");
|
||||||
|
embedFields[2].GetProperty("isInline").GetBoolean().Should().BeTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.JsonWriting;
|
||||||
|
|
||||||
|
public record MentionSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task User_mention_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.MentionTestCases,
|
||||||
|
Snowflake.Parse("866458840245076028")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.GetProperty("content").GetString().Should().Be("User mention: @Tyrrrz");
|
||||||
|
|
||||||
|
message
|
||||||
|
.GetProperty("mentions")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetProperty("id").GetString())
|
||||||
|
.Should().Contain("128178626683338752");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Text_channel_mention_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.MentionTestCases,
|
||||||
|
Snowflake.Parse("866459040480624680")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.GetProperty("content").GetString().Should().Be("Text channel mention: #mention-tests");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Voice_channel_mention_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.MentionTestCases,
|
||||||
|
Snowflake.Parse("866459175462633503")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.GetProperty("content").GetString().Should().Be("Voice channel mention: #chaos-vc [voice]");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Role_mention_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.MentionTestCases,
|
||||||
|
Snowflake.Parse("866459254693429258")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
message.GetProperty("content").GetString().Should().Be("Role mention: @Role 1");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.JsonWriting;
|
||||||
|
|
||||||
|
public record StickerSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_PNG_based_sticker_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.StickerTestCases,
|
||||||
|
Snowflake.Parse("939670623158943754")
|
||||||
|
);
|
||||||
|
|
||||||
|
var sticker = message
|
||||||
|
.GetProperty("stickers")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Single();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
sticker.GetProperty("id").GetString().Should().Be("904215665597120572");
|
||||||
|
sticker.GetProperty("name").GetString().Should().Be("rock");
|
||||||
|
sticker.GetProperty("format").GetString().Should().Be("PngAnimated");
|
||||||
|
sticker.GetProperty("sourceUrl").GetString().Should().Be("https://discord.com/stickers/904215665597120572.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Message_with_a_Lottie_based_sticker_is_rendered_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var message = await ExportWrapper.GetMessageAsJsonAsync(
|
||||||
|
ChannelIds.StickerTestCases,
|
||||||
|
Snowflake.Parse("939670526517997590")
|
||||||
|
);
|
||||||
|
|
||||||
|
var sticker = message
|
||||||
|
.GetProperty("stickers")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Single();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
sticker.GetProperty("id").GetString().Should().Be("816087132447178774");
|
||||||
|
sticker.GetProperty("name").GetString().Should().Be("Yikes");
|
||||||
|
sticker.GetProperty("format").GetString().Should().Be("Lottie");
|
||||||
|
sticker.GetProperty("sourceUrl").GetString().Should().Be("https://discord.com/stickers/816087132447178774.json");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Infrastructure;
|
||||||
|
using DiscordChatExporter.Cli.Commands;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Infra;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Core.Exporting;
|
||||||
|
using DiscordChatExporter.Core.Exporting.Partitioning;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs;
|
||||||
|
|
||||||
|
public record PartitioningSpecs(TempOutputFixture TempOutput) : IClassFixture<TempOutputFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_partitioned_by_count_are_split_into_multiple_files_correctly()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
|
||||||
|
var dirPath = Path.GetDirectoryName(filePath) ?? Directory.GetCurrentDirectory();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.DateRangeTestCases },
|
||||||
|
ExportFormat = ExportFormat.HtmlDark,
|
||||||
|
OutputPath = filePath,
|
||||||
|
PartitionLimit = PartitionLimit.Parse("3")
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Directory.EnumerateFiles(dirPath, fileNameWithoutExt + "*")
|
||||||
|
.Should()
|
||||||
|
.HaveCount(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_partitioned_by_file_size_are_split_into_multiple_files_correctly()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
|
||||||
|
var dirPath = Path.GetDirectoryName(filePath) ?? Directory.GetCurrentDirectory();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.DateRangeTestCases },
|
||||||
|
ExportFormat = ExportFormat.HtmlDark,
|
||||||
|
OutputPath = filePath,
|
||||||
|
PartitionLimit = PartitionLimit.Parse("20kb")
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Directory.EnumerateFiles(dirPath, fileNameWithoutExt + "*")
|
||||||
|
.Should()
|
||||||
|
.HaveCount(8);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs.PlainTextWriting;
|
||||||
|
|
||||||
|
public record ContentSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture<ExportWrapperFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_are_exported_correctly()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var document = await ExportWrapper.ExportAsPlainTextAsync(ChannelIds.DateRangeTestCases);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
document.Should().ContainAll(
|
||||||
|
"Tyrrrz#5447",
|
||||||
|
"Hello world",
|
||||||
|
"Goodbye world",
|
||||||
|
"Foo bar",
|
||||||
|
"Hurdle Durdle",
|
||||||
|
"One",
|
||||||
|
"Two",
|
||||||
|
"Three",
|
||||||
|
"Yeet"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Infrastructure;
|
||||||
|
using DiscordChatExporter.Cli.Commands;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Fixtures;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Infra;
|
||||||
|
using DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
using DiscordChatExporter.Cli.Tests.Utils;
|
||||||
|
using DiscordChatExporter.Core.Exporting;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Specs;
|
||||||
|
|
||||||
|
public record SelfContainedSpecs(TempOutputFixture TempOutput) : IClassFixture<TempOutputFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Messages_in_self_contained_export_only_reference_local_file_resources()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filePath = TempOutput.GetTempFilePath();
|
||||||
|
var dirPath = Path.GetDirectoryName(filePath) ?? Directory.GetCurrentDirectory();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await new ExportChannelsCommand
|
||||||
|
{
|
||||||
|
Token = Secrets.DiscordToken,
|
||||||
|
ChannelIds = new[] { ChannelIds.SelfContainedTestCases },
|
||||||
|
ExportFormat = ExportFormat.HtmlDark,
|
||||||
|
OutputPath = filePath,
|
||||||
|
ShouldDownloadMedia = true
|
||||||
|
}.ExecuteAsync(new FakeConsole());
|
||||||
|
|
||||||
|
var data = await File.ReadAllTextAsync(filePath);
|
||||||
|
var document = Html.Parse(data);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
document
|
||||||
|
.QuerySelectorAll("body [src]")
|
||||||
|
.Select(e => e.GetAttribute("src")!)
|
||||||
|
.Select(f => Path.GetFullPath(f, dirPath))
|
||||||
|
.All(File.Exists)
|
||||||
|
.Should()
|
||||||
|
.BeTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.TestData;
|
||||||
|
|
||||||
|
public static class ChannelIds
|
||||||
|
{
|
||||||
|
public static Snowflake AttachmentTestCases { get; } = Snowflake.Parse("885587741654536192");
|
||||||
|
|
||||||
|
public static Snowflake DateRangeTestCases { get; } = Snowflake.Parse("866674248747319326");
|
||||||
|
|
||||||
|
public static Snowflake EmbedTestCases { get; } = Snowflake.Parse("866472452459462687");
|
||||||
|
|
||||||
|
public static Snowflake StickerTestCases { get; } = Snowflake.Parse("939668868253769729");
|
||||||
|
|
||||||
|
public static Snowflake FilterTestCases { get; } = Snowflake.Parse("866744075033641020");
|
||||||
|
|
||||||
|
public static Snowflake MentionTestCases { get; } = Snowflake.Parse("866458801389174794");
|
||||||
|
|
||||||
|
public static Snowflake ReplyTestCases { get; } = Snowflake.Parse("866459871934677052");
|
||||||
|
|
||||||
|
public static Snowflake SelfContainedTestCases { get; } = Snowflake.Parse("887441432678379560");
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Utils;
|
||||||
|
|
||||||
|
internal static class DirectoryEx
|
||||||
|
{
|
||||||
|
public static void DeleteIfExists(string dirPath, bool recursive = true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.Delete(dirPath, recursive);
|
||||||
|
}
|
||||||
|
catch (DirectoryNotFoundException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Reset(string dirPath)
|
||||||
|
{
|
||||||
|
DeleteIfExists(dirPath);
|
||||||
|
Directory.CreateDirectory(dirPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using AngleSharp.Html.Dom;
|
||||||
|
using AngleSharp.Html.Parser;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Tests.Utils;
|
||||||
|
|
||||||
|
internal static class Html
|
||||||
|
{
|
||||||
|
private static readonly IHtmlParser Parser = new HtmlParser();
|
||||||
|
|
||||||
|
public static IHtmlDocument Parse(string source) => Parser.ParseDocument(source);
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||||
|
"methodDisplayOptions": "all",
|
||||||
|
"methodDisplay": "method"
|
||||||
|
}
|
||||||
@@ -14,135 +14,168 @@ using DiscordChatExporter.Core.Exceptions;
|
|||||||
using DiscordChatExporter.Core.Exporting;
|
using DiscordChatExporter.Core.Exporting;
|
||||||
using DiscordChatExporter.Core.Exporting.Filtering;
|
using DiscordChatExporter.Core.Exporting.Filtering;
|
||||||
using DiscordChatExporter.Core.Exporting.Partitioning;
|
using DiscordChatExporter.Core.Exporting.Partitioning;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using Gress;
|
||||||
using Tyrrrz.Extensions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands.Base
|
namespace DiscordChatExporter.Cli.Commands.Base;
|
||||||
|
|
||||||
|
public abstract class ExportCommandBase : TokenCommandBase
|
||||||
{
|
{
|
||||||
public abstract class ExportCommandBase : TokenCommandBase
|
[CommandOption("output", 'o', Description = "Output file or directory path.")]
|
||||||
|
public string OutputPath { get; init; } = Directory.GetCurrentDirectory();
|
||||||
|
|
||||||
|
[CommandOption("format", 'f', Description = "Export format.")]
|
||||||
|
public ExportFormat ExportFormat { get; init; } = ExportFormat.HtmlDark;
|
||||||
|
|
||||||
|
[CommandOption("after", Description = "Only include messages sent after this date or message ID.")]
|
||||||
|
public Snowflake? After { get; init; }
|
||||||
|
|
||||||
|
[CommandOption("before", Description = "Only include messages sent before this date or message ID.")]
|
||||||
|
public Snowflake? Before { get; init; }
|
||||||
|
|
||||||
|
[CommandOption("partition", 'p', Description = "Split output into partitions, each limited to this number of messages (e.g. '100') or file size (e.g. '10mb').")]
|
||||||
|
public PartitionLimit PartitionLimit { get; init; } = PartitionLimit.Null;
|
||||||
|
|
||||||
|
[CommandOption("filter", Description = "Only include messages that satisfy this filter (e.g. 'from:foo#1234' or 'has:image').")]
|
||||||
|
public MessageFilter MessageFilter { get; init; } = MessageFilter.Null;
|
||||||
|
|
||||||
|
[CommandOption("parallel", Description = "Limits how many channels can be exported in parallel.")]
|
||||||
|
public int ParallelLimit { get; init; } = 1;
|
||||||
|
|
||||||
|
[CommandOption("media", Description = "Download referenced media content.")]
|
||||||
|
public bool ShouldDownloadMedia { get; init; }
|
||||||
|
|
||||||
|
[CommandOption("reuse-media", Description = "Reuse already existing media content to skip redundant downloads.")]
|
||||||
|
public bool ShouldReuseMedia { get; init; }
|
||||||
|
|
||||||
|
[CommandOption("dateformat", Description = "Format used when writing dates.")]
|
||||||
|
public string DateFormat { get; init; } = "dd-MMM-yy hh:mm tt";
|
||||||
|
|
||||||
|
private ChannelExporter? _channelExporter;
|
||||||
|
protected ChannelExporter Exporter => _channelExporter ??= new ChannelExporter(Discord);
|
||||||
|
|
||||||
|
protected async ValueTask ExecuteAsync(IConsole console, IReadOnlyList<Channel> channels)
|
||||||
{
|
{
|
||||||
[CommandOption("output", 'o', Description = "Output file or directory path.")]
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
public string OutputPath { get; init; } = Directory.GetCurrentDirectory();
|
|
||||||
|
|
||||||
[CommandOption("format", 'f', Description = "Export format.")]
|
if (ShouldReuseMedia && !ShouldDownloadMedia)
|
||||||
public ExportFormat ExportFormat { get; init; } = ExportFormat.HtmlDark;
|
|
||||||
|
|
||||||
[CommandOption("after", Description = "Only include messages sent after this date or message ID.")]
|
|
||||||
public Snowflake? After { get; init; }
|
|
||||||
|
|
||||||
[CommandOption("before", Description = "Only include messages sent before this date or message ID.")]
|
|
||||||
public Snowflake? Before { get; init; }
|
|
||||||
|
|
||||||
[CommandOption("partition", 'p', Description = "Split output into partitions, each limited to this number of messages (e.g. '100') or file size (e.g. '10mb').")]
|
|
||||||
public PartitionLimit PartitionLimit { get; init; } = PartitionLimit.Null;
|
|
||||||
|
|
||||||
[CommandOption("filter", Description = "Only include messages that satisfy this filter (e.g. 'from:foo#1234' or 'has:image').")]
|
|
||||||
public MessageFilter MessageFilter { get; init; } = MessageFilter.Null;
|
|
||||||
|
|
||||||
[CommandOption("parallel", Description = "Limits how many channels can be exported in parallel.")]
|
|
||||||
public int ParallelLimit { get; init; } = 1;
|
|
||||||
|
|
||||||
[CommandOption("media", Description = "Download referenced media content.")]
|
|
||||||
public bool ShouldDownloadMedia { get; init; }
|
|
||||||
|
|
||||||
[CommandOption("reuse-media", Description = "Reuse already existing media content to skip redundant downloads.")]
|
|
||||||
public bool ShouldReuseMedia { get; init; }
|
|
||||||
|
|
||||||
[CommandOption("dateformat", Description = "Format used when writing dates.")]
|
|
||||||
public string DateFormat { get; init; } = "dd-MMM-yy hh:mm tt";
|
|
||||||
|
|
||||||
private ChannelExporter? _channelExporter;
|
|
||||||
protected ChannelExporter Exporter => _channelExporter ??= new ChannelExporter(Discord);
|
|
||||||
|
|
||||||
protected async ValueTask ExportAsync(IConsole console, IReadOnlyList<Channel> channels)
|
|
||||||
{
|
{
|
||||||
await console.Output.WriteLineAsync($"Exporting {channels.Count} channel(s)...");
|
throw new CommandException("Option --reuse-media cannot be used without --media.");
|
||||||
|
}
|
||||||
|
|
||||||
var errors = new ConcurrentDictionary<Channel, string>();
|
var errors = new ConcurrentDictionary<Channel, string>();
|
||||||
|
|
||||||
// Wrap everything in a progress ticker
|
// Export
|
||||||
await console.CreateProgressTicker().StartAsync(async progressContext =>
|
await console.Output.WriteLineAsync($"Exporting {channels.Count} channel(s)...");
|
||||||
{
|
await console.CreateProgressTicker().StartAsync(async progressContext =>
|
||||||
await channels.ParallelForEachAsync(async channel =>
|
{
|
||||||
|
await Parallel.ForEachAsync(
|
||||||
|
channels,
|
||||||
|
new ParallelOptions
|
||||||
|
{
|
||||||
|
MaxDegreeOfParallelism = Math.Max(1, ParallelLimit),
|
||||||
|
CancellationToken = cancellationToken
|
||||||
|
},
|
||||||
|
async (channel, innerCancellationToken) =>
|
||||||
{
|
{
|
||||||
// Export
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await progressContext.StartTaskAsync($"{channel.Category} / {channel.Name}", async progress =>
|
await progressContext.StartTaskAsync($"{channel.Category.Name} / {channel.Name}",
|
||||||
{
|
async progress =>
|
||||||
var guild = await Discord.GetGuildAsync(channel.GuildId);
|
{
|
||||||
|
var guild = await Discord.GetGuildAsync(channel.GuildId, innerCancellationToken);
|
||||||
|
|
||||||
var request = new ExportRequest(
|
var request = new ExportRequest(
|
||||||
guild,
|
guild,
|
||||||
channel,
|
channel,
|
||||||
OutputPath,
|
OutputPath,
|
||||||
ExportFormat,
|
ExportFormat,
|
||||||
After,
|
After,
|
||||||
Before,
|
Before,
|
||||||
PartitionLimit,
|
PartitionLimit,
|
||||||
MessageFilter,
|
MessageFilter,
|
||||||
ShouldDownloadMedia,
|
ShouldDownloadMedia,
|
||||||
ShouldReuseMedia,
|
ShouldReuseMedia,
|
||||||
DateFormat
|
DateFormat
|
||||||
);
|
);
|
||||||
|
|
||||||
await Exporter.ExportChannelAsync(request, progress);
|
await Exporter.ExportChannelAsync(
|
||||||
});
|
request,
|
||||||
|
progress.ToPercentageBased(),
|
||||||
|
innerCancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
catch (DiscordChatExporterException ex) when (!ex.IsCritical)
|
catch (DiscordChatExporterException ex) when (!ex.IsFatal)
|
||||||
{
|
{
|
||||||
errors[channel] = ex.Message;
|
errors[channel] = ex.Message;
|
||||||
}
|
}
|
||||||
}, ParallelLimit.ClampMin(1));
|
}
|
||||||
});
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// Print result
|
// Print result
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
using (console.WithForegroundColor(ConsoleColor.White))
|
||||||
|
{
|
||||||
|
await console.Output.WriteLineAsync(
|
||||||
|
$"Successfully exported {channels.Count - errors.Count} channel(s)."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print errors
|
||||||
|
if (errors.Any())
|
||||||
|
{
|
||||||
|
await console.Output.WriteLineAsync();
|
||||||
|
|
||||||
|
using (console.WithForegroundColor(ConsoleColor.Red))
|
||||||
{
|
{
|
||||||
await console.Output.WriteLineAsync(
|
await console.Output.WriteLineAsync(
|
||||||
$"Successfully exported {channels.Count - errors.Count} channel(s)."
|
$"Failed to export {errors.Count} channel(s):"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print errors
|
foreach (var (channel, error) in errors)
|
||||||
if (errors.Any())
|
|
||||||
{
|
{
|
||||||
await console.Output.WriteLineAsync();
|
await console.Output.WriteAsync($"{channel.Category.Name} / {channel.Name}: ");
|
||||||
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.Red))
|
using (console.WithForegroundColor(ConsoleColor.Red))
|
||||||
{
|
await console.Output.WriteLineAsync(error);
|
||||||
await console.Output.WriteLineAsync(
|
|
||||||
$"Failed to export {errors.Count} channel(s):"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var (channel, error) in errors)
|
|
||||||
{
|
|
||||||
await console.Output.WriteAsync($"{channel.Category} / {channel.Name}: ");
|
|
||||||
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.Red))
|
|
||||||
await console.Output.WriteLineAsync(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
await console.Output.WriteLineAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fail the command if ALL channels failed to export.
|
await console.Output.WriteLineAsync();
|
||||||
// Having some of the channels fail to export is fine and expected.
|
|
||||||
if (errors.Count >= channels.Count)
|
|
||||||
{
|
|
||||||
throw new CommandException("Export failed.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override ValueTask ExecuteAsync(IConsole console)
|
// Fail the command only if ALL channels failed to export.
|
||||||
|
// Having some of the channels fail to export is expected.
|
||||||
|
if (errors.Count >= channels.Count)
|
||||||
{
|
{
|
||||||
if (ShouldReuseMedia && !ShouldDownloadMedia)
|
throw new CommandException("Export failed.");
|
||||||
{
|
|
||||||
throw new CommandException("Option --reuse-media cannot be used without --media.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return default;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async ValueTask ExecuteAsync(IConsole console, IReadOnlyList<Snowflake> channelIds)
|
||||||
|
{
|
||||||
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
|
var channels = new List<Channel>();
|
||||||
|
|
||||||
|
foreach (var channelId in channelIds)
|
||||||
|
{
|
||||||
|
var channel = await Discord.GetChannelAsync(channelId, cancellationToken);
|
||||||
|
channels.Add(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ExecuteAsync(console, channels);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
// War in Ukraine message
|
||||||
|
console.Output.WriteLine("========================================================================");
|
||||||
|
console.Output.WriteLine("|| Ukraine is at war! Support my country in its fight for freedom ||");
|
||||||
|
console.Output.WriteLine("|| Learn more: https://tyrrrz.me ||");
|
||||||
|
console.Output.WriteLine("========================================================================");
|
||||||
|
console.Output.WriteLine("");
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,29 +1,23 @@
|
|||||||
using System.Threading.Tasks;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using CliFx;
|
using CliFx;
|
||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
using CliFx.Infrastructure;
|
using CliFx.Infrastructure;
|
||||||
using DiscordChatExporter.Core.Discord;
|
using DiscordChatExporter.Core.Discord;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands.Base
|
namespace DiscordChatExporter.Cli.Commands.Base;
|
||||||
|
|
||||||
|
public abstract class TokenCommandBase : ICommand
|
||||||
{
|
{
|
||||||
public abstract class TokenCommandBase : ICommand
|
[CommandOption("token", 't', IsRequired = true, EnvironmentVariable = "DISCORD_TOKEN", Description = "Authentication token.")]
|
||||||
{
|
public string Token { get; init; } = "";
|
||||||
[CommandOption("token", 't', IsRequired = true, EnvironmentVariable = "DISCORD_TOKEN", Description = "Authentication token.")]
|
|
||||||
public string TokenValue { get; init; } = "";
|
|
||||||
|
|
||||||
[CommandOption("bot", 'b', EnvironmentVariable = "DISCORD_TOKEN_BOT", Description = "Authenticate as a bot.")]
|
[CommandOption("bot", 'b', EnvironmentVariable = "DISCORD_TOKEN_BOT", Description = "This option doesn't do anything. Kept for backwards compatibility.")]
|
||||||
public bool IsBotToken { get; init; }
|
[Obsolete("This option doesn't do anything. Kept for backwards compatibility.")]
|
||||||
|
public bool IsBotToken { get; init; }
|
||||||
|
|
||||||
private AuthToken GetAuthToken() => new(
|
private DiscordClient? _discordClient;
|
||||||
IsBotToken
|
protected DiscordClient Discord => _discordClient ??= new DiscordClient(Token);
|
||||||
? AuthTokenType.Bot
|
|
||||||
: AuthTokenType.User,
|
|
||||||
TokenValue
|
|
||||||
);
|
|
||||||
|
|
||||||
private DiscordClient? _discordClient;
|
public abstract ValueTask ExecuteAsync(IConsole console);
|
||||||
protected DiscordClient Discord => _discordClient ??= new DiscordClient(GetAuthToken());
|
|
||||||
|
|
||||||
public abstract ValueTask ExecuteAsync(IConsole console);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -5,42 +5,38 @@ using CliFx.Infrastructure;
|
|||||||
using DiscordChatExporter.Cli.Commands.Base;
|
using DiscordChatExporter.Cli.Commands.Base;
|
||||||
using DiscordChatExporter.Core.Discord.Data;
|
using DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands
|
namespace DiscordChatExporter.Cli.Commands;
|
||||||
|
|
||||||
|
[Command("exportall", Description = "Export all accessible channels.")]
|
||||||
|
public class ExportAllCommand : ExportCommandBase
|
||||||
{
|
{
|
||||||
[Command("exportall", Description = "Export all accessible channels.")]
|
[CommandOption("include-dm", Description = "Include direct message channels.")]
|
||||||
public class ExportAllCommand : ExportCommandBase
|
public bool IncludeDirectMessages { get; init; } = true;
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
[CommandOption("include-dm", Description = "Include direct message channels.")]
|
await base.ExecuteAsync(console);
|
||||||
public bool IncludeDirectMessages { get; init; } = true;
|
|
||||||
|
|
||||||
public override async ValueTask ExecuteAsync(IConsole console)
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
|
var channels = new List<Channel>();
|
||||||
|
|
||||||
|
await console.Output.WriteLineAsync("Fetching channels...");
|
||||||
|
await foreach (var guild in Discord.GetUserGuildsAsync(cancellationToken))
|
||||||
{
|
{
|
||||||
await base.ExecuteAsync(console);
|
// Skip DMs if instructed to
|
||||||
|
if (!IncludeDirectMessages && guild.Id == Guild.DirectMessages.Id)
|
||||||
|
continue;
|
||||||
|
|
||||||
// Get channel metadata
|
await foreach (var channel in Discord.GetGuildChannelsAsync(guild.Id, cancellationToken))
|
||||||
await console.Output.WriteLineAsync("Fetching channels...");
|
|
||||||
|
|
||||||
var channels = new List<Channel>();
|
|
||||||
|
|
||||||
// Aggregate channels from all guilds
|
|
||||||
await foreach (var guild in Discord.GetUserGuildsAsync())
|
|
||||||
{
|
{
|
||||||
// Skip DMs if instructed to
|
// Skip non-text channels
|
||||||
if (!IncludeDirectMessages && guild.Id == Guild.DirectMessages.Id)
|
if (!channel.IsTextChannel)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
await foreach (var channel in Discord.GetGuildChannelsAsync(guild.Id))
|
channels.Add(channel);
|
||||||
{
|
|
||||||
// Skip non-text channels
|
|
||||||
if (!channel.IsTextChannel)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
channels.Add(channel);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export
|
|
||||||
await ExportAsync(console, channels);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await base.ExecuteAsync(console, channels);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,34 +5,19 @@ using CliFx.Attributes;
|
|||||||
using CliFx.Infrastructure;
|
using CliFx.Infrastructure;
|
||||||
using DiscordChatExporter.Cli.Commands.Base;
|
using DiscordChatExporter.Cli.Commands.Base;
|
||||||
using DiscordChatExporter.Core.Discord;
|
using DiscordChatExporter.Core.Discord;
|
||||||
using DiscordChatExporter.Core.Discord.Data;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands
|
namespace DiscordChatExporter.Cli.Commands;
|
||||||
|
|
||||||
|
[Command("export", Description = "Export one or multiple channels.")]
|
||||||
|
public class ExportChannelsCommand : ExportCommandBase
|
||||||
{
|
{
|
||||||
[Command("export", Description = "Export one or multiple channels.")]
|
// TODO: change this to plural (breaking change)
|
||||||
public class ExportChannelsCommand : ExportCommandBase
|
[CommandOption("channel", 'c', IsRequired = true, Description = "Channel ID(s).")]
|
||||||
|
public IReadOnlyList<Snowflake> ChannelIds { get; init; } = Array.Empty<Snowflake>();
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
// TODO: change this to plural (breaking change)
|
await base.ExecuteAsync(console);
|
||||||
[CommandOption("channel", 'c', IsRequired = true, Description = "Channel ID(s).")]
|
await base.ExecuteAsync(console, ChannelIds);
|
||||||
public IReadOnlyList<Snowflake> ChannelIds { get; init; } = Array.Empty<Snowflake>();
|
|
||||||
|
|
||||||
public override async ValueTask ExecuteAsync(IConsole console)
|
|
||||||
{
|
|
||||||
await base.ExecuteAsync(console);
|
|
||||||
|
|
||||||
// Get channel metadata
|
|
||||||
await console.Output.WriteLineAsync("Fetching channel(s)...");
|
|
||||||
|
|
||||||
var channels = new List<Channel>();
|
|
||||||
|
|
||||||
foreach (var channelId in ChannelIds)
|
|
||||||
{
|
|
||||||
var channel = await Discord.GetChannelAsync(channelId);
|
|
||||||
channels.Add(channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Export
|
|
||||||
await ExportAsync(console, channels);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,22 +6,21 @@ using DiscordChatExporter.Cli.Commands.Base;
|
|||||||
using DiscordChatExporter.Core.Discord.Data;
|
using DiscordChatExporter.Core.Discord.Data;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands
|
namespace DiscordChatExporter.Cli.Commands;
|
||||||
|
|
||||||
|
[Command("exportdm", Description = "Export all direct message channels.")]
|
||||||
|
public class ExportDirectMessagesCommand : ExportCommandBase
|
||||||
{
|
{
|
||||||
[Command("exportdm", Description = "Export all direct message channels.")]
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
public class ExportDirectMessagesCommand : ExportCommandBase
|
|
||||||
{
|
{
|
||||||
public override async ValueTask ExecuteAsync(IConsole console)
|
await base.ExecuteAsync(console);
|
||||||
{
|
|
||||||
await base.ExecuteAsync(console);
|
|
||||||
|
|
||||||
// Get channel metadata
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
await console.Output.WriteLineAsync("Fetching channels...");
|
|
||||||
var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id);
|
|
||||||
var textChannels = channels.Where(c => c.IsTextChannel).ToArray();
|
|
||||||
|
|
||||||
// Export
|
await console.Output.WriteLineAsync("Fetching channels...");
|
||||||
await ExportAsync(console, textChannels);
|
var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken);
|
||||||
}
|
var textChannels = channels.Where(c => c.IsTextChannel).ToArray();
|
||||||
|
|
||||||
|
await base.ExecuteAsync(console, textChannels);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,25 +6,24 @@ using DiscordChatExporter.Cli.Commands.Base;
|
|||||||
using DiscordChatExporter.Core.Discord;
|
using DiscordChatExporter.Core.Discord;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands
|
namespace DiscordChatExporter.Cli.Commands;
|
||||||
|
|
||||||
|
[Command("exportguild", Description = "Export all channels within specified guild.")]
|
||||||
|
public class ExportGuildCommand : ExportCommandBase
|
||||||
{
|
{
|
||||||
[Command("exportguild", Description = "Export all channels within specified guild.")]
|
[CommandOption("guild", 'g', IsRequired = true, Description = "Guild ID.")]
|
||||||
public class ExportGuildCommand : ExportCommandBase
|
public Snowflake GuildId { get; init; }
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
[CommandOption("guild", 'g', IsRequired = true, Description = "Guild ID.")]
|
await base.ExecuteAsync(console);
|
||||||
public Snowflake GuildId { get; init; }
|
|
||||||
|
|
||||||
public override async ValueTask ExecuteAsync(IConsole console)
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
{
|
|
||||||
await base.ExecuteAsync(console);
|
|
||||||
|
|
||||||
// Get channel metadata
|
await console.Output.WriteLineAsync("Fetching channels...");
|
||||||
await console.Output.WriteLineAsync("Fetching channels...");
|
var channels = await Discord.GetGuildChannelsAsync(GuildId, cancellationToken);
|
||||||
var channels = await Discord.GetGuildChannelsAsync(GuildId);
|
var textChannels = channels.Where(c => c.IsTextChannel).ToArray();
|
||||||
var textChannels = channels.Where(c => c.IsTextChannel).ToArray();
|
|
||||||
|
|
||||||
// Export
|
await base.ExecuteAsync(console, textChannels);
|
||||||
await ExportAsync(console, textChannels);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,39 +7,38 @@ using DiscordChatExporter.Cli.Commands.Base;
|
|||||||
using DiscordChatExporter.Core.Discord;
|
using DiscordChatExporter.Core.Discord;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands
|
namespace DiscordChatExporter.Cli.Commands;
|
||||||
|
|
||||||
|
[Command("channels", Description = "Get the list of channels in a guild.")]
|
||||||
|
public class GetChannelsCommand : TokenCommandBase
|
||||||
{
|
{
|
||||||
[Command("channels", Description = "Get the list of channels in a guild.")]
|
[CommandOption("guild", 'g', IsRequired = true, Description = "Guild ID.")]
|
||||||
public class GetChannelsCommand : TokenCommandBase
|
public Snowflake GuildId { get; init; }
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
[CommandOption("guild", 'g', IsRequired = true, Description = "Guild ID.")]
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
public Snowflake GuildId { get; init; }
|
|
||||||
|
|
||||||
public override async ValueTask ExecuteAsync(IConsole console)
|
var channels = await Discord.GetGuildChannelsAsync(GuildId, cancellationToken);
|
||||||
|
|
||||||
|
var textChannels = channels
|
||||||
|
.Where(c => c.IsTextChannel)
|
||||||
|
.OrderBy(c => c.Category.Position)
|
||||||
|
.ThenBy(c => c.Name)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var channel in textChannels)
|
||||||
{
|
{
|
||||||
var channels = await Discord.GetGuildChannelsAsync(GuildId);
|
// Channel ID
|
||||||
|
await console.Output.WriteAsync(channel.Id.ToString());
|
||||||
|
|
||||||
var textChannels = channels
|
// Separator
|
||||||
.Where(c => c.IsTextChannel)
|
using (console.WithForegroundColor(ConsoleColor.DarkGray))
|
||||||
.OrderBy(c => c.Category.Position)
|
await console.Output.WriteAsync(" | ");
|
||||||
.ThenBy(c => c.Name)
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
foreach (var channel in textChannels)
|
// Channel category / name
|
||||||
{
|
using (console.WithForegroundColor(ConsoleColor.White))
|
||||||
// Channel ID
|
await console.Output.WriteLineAsync($"{channel.Category.Name} / {channel.Name}");
|
||||||
await console.Output.WriteAsync(channel.Id.ToString());
|
|
||||||
|
|
||||||
// Separator
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.DarkGray))
|
|
||||||
await console.Output.WriteAsync(" | ");
|
|
||||||
|
|
||||||
// Channel category / name
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
|
||||||
await console.Output.WriteAsync($"{channel.Category} / {channel.Name}");
|
|
||||||
|
|
||||||
await console.Output.WriteLineAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,36 +7,35 @@ using DiscordChatExporter.Cli.Commands.Base;
|
|||||||
using DiscordChatExporter.Core.Discord.Data;
|
using DiscordChatExporter.Core.Discord.Data;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands
|
namespace DiscordChatExporter.Cli.Commands;
|
||||||
|
|
||||||
|
[Command("dm", Description = "Get the list of direct message channels.")]
|
||||||
|
public class GetDirectMessageChannelsCommand : TokenCommandBase
|
||||||
{
|
{
|
||||||
[Command("dm", Description = "Get the list of direct message channels.")]
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
public class GetDirectMessageChannelsCommand : TokenCommandBase
|
|
||||||
{
|
{
|
||||||
public override async ValueTask ExecuteAsync(IConsole console)
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
|
|
||||||
|
var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id, cancellationToken);
|
||||||
|
|
||||||
|
var textChannels = channels
|
||||||
|
.Where(c => c.IsTextChannel)
|
||||||
|
.OrderBy(c => c.Category.Position)
|
||||||
|
.ThenBy(c => c.Name)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var channel in textChannels)
|
||||||
{
|
{
|
||||||
var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id);
|
// Channel ID
|
||||||
|
await console.Output.WriteAsync(channel.Id.ToString());
|
||||||
|
|
||||||
var textChannels = channels
|
// Separator
|
||||||
.Where(c => c.IsTextChannel)
|
using (console.WithForegroundColor(ConsoleColor.DarkGray))
|
||||||
.OrderBy(c => c.Category.Position)
|
await console.Output.WriteAsync(" | ");
|
||||||
.ThenBy(c => c.Name)
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
foreach (var channel in textChannels)
|
// Channel category / name
|
||||||
{
|
using (console.WithForegroundColor(ConsoleColor.White))
|
||||||
// Channel ID
|
await console.Output.WriteLineAsync($"{channel.Category.Name} / {channel.Name}");
|
||||||
await console.Output.WriteAsync(channel.Id.ToString());
|
|
||||||
|
|
||||||
// Separator
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.DarkGray))
|
|
||||||
await console.Output.WriteAsync(" | ");
|
|
||||||
|
|
||||||
// Channel category / name
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
|
||||||
await console.Output.WriteAsync($"{channel.Category} / {channel.Name}");
|
|
||||||
|
|
||||||
await console.Output.WriteLineAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,30 +6,29 @@ using CliFx.Infrastructure;
|
|||||||
using DiscordChatExporter.Cli.Commands.Base;
|
using DiscordChatExporter.Cli.Commands.Base;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands
|
namespace DiscordChatExporter.Cli.Commands;
|
||||||
|
|
||||||
|
[Command("guilds", Description = "Get the list of accessible guilds.")]
|
||||||
|
public class GetGuildsCommand : TokenCommandBase
|
||||||
{
|
{
|
||||||
[Command("guilds", Description = "Get the list of accessible guilds.")]
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
public class GetGuildsCommand : TokenCommandBase
|
|
||||||
{
|
{
|
||||||
public override async ValueTask ExecuteAsync(IConsole console)
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
|
|
||||||
|
var guilds = await Discord.GetUserGuildsAsync(cancellationToken);
|
||||||
|
|
||||||
|
foreach (var guild in guilds.OrderBy(g => g.Name))
|
||||||
{
|
{
|
||||||
var guilds = await Discord.GetUserGuildsAsync();
|
// Guild ID
|
||||||
|
await console.Output.WriteAsync(guild.Id.ToString());
|
||||||
|
|
||||||
foreach (var guild in guilds.OrderBy(g => g.Name))
|
// Separator
|
||||||
{
|
using (console.WithForegroundColor(ConsoleColor.DarkGray))
|
||||||
// Guild ID
|
await console.Output.WriteAsync(" | ");
|
||||||
await console.Output.WriteAsync(guild.Id.ToString());
|
|
||||||
|
|
||||||
// Separator
|
// Guild name
|
||||||
using (console.WithForegroundColor(ConsoleColor.DarkGray))
|
using (console.WithForegroundColor(ConsoleColor.White))
|
||||||
await console.Output.WriteAsync(" | ");
|
await console.Output.WriteLineAsync(guild.Name);
|
||||||
|
|
||||||
// Guild name
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
|
||||||
await console.Output.WriteAsync(guild.Name);
|
|
||||||
|
|
||||||
await console.Output.WriteLineAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,64 +4,67 @@ using CliFx;
|
|||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
using CliFx.Infrastructure;
|
using CliFx.Infrastructure;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Commands
|
namespace DiscordChatExporter.Cli.Commands;
|
||||||
|
|
||||||
|
[Command("guide", Description = "Explains how to obtain token, guild or channel ID.")]
|
||||||
|
public class GuideCommand : ICommand
|
||||||
{
|
{
|
||||||
[Command("guide", Description = "Explains how to obtain token, guild or channel ID.")]
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
public class GuideCommand : ICommand
|
|
||||||
{
|
{
|
||||||
public ValueTask ExecuteAsync(IConsole console)
|
// User token
|
||||||
{
|
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(" 1. Open Discord");
|
console.Output.WriteLine(" 1. Open Discord in your web browser and login");
|
||||||
console.Output.WriteLine(" 2. Press Ctrl+Shift+I to show developer tools");
|
console.Output.WriteLine(" 2. Press Ctrl+Shift+I to show developer tools");
|
||||||
console.Output.WriteLine(" 3. Press Ctrl+Shift+M to toggle device toolbar");
|
console.Output.WriteLine(" 3. Press Ctrl+Shift+M to toggle device toolbar");
|
||||||
console.Output.WriteLine(" 4. Navigate to the Application tab");
|
console.Output.WriteLine(" 4. Navigate to the Application tab");
|
||||||
console.Output.WriteLine(" 5. On the left, expand Local Storage and select https://discord.com");
|
console.Output.WriteLine(" 5. On the left, expand Local Storage and select https://discord.com");
|
||||||
console.Output.WriteLine(" 6. Type \"token\" into the Filter box");
|
console.Output.WriteLine(" 6. Type \"token\" into the Filter box");
|
||||||
console.Output.WriteLine(" 7. If the token key does not appear, press Ctrl+R to reload");
|
console.Output.WriteLine(" 7. If the token key does not appear, press Ctrl+R to reload");
|
||||||
console.Output.WriteLine(" 8. Copy the value of the token key");
|
console.Output.WriteLine(" 8. Copy the value of the token key");
|
||||||
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();
|
console.Output.WriteLine();
|
||||||
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
// Bot token
|
||||||
console.Output.WriteLine("To get bot token:");
|
using (console.WithForegroundColor(ConsoleColor.White))
|
||||||
|
console.Output.WriteLine("To get bot token:");
|
||||||
|
|
||||||
console.Output.WriteLine(" 1. Go to Discord developer portal");
|
console.Output.WriteLine(" 1. Go to Discord developer portal");
|
||||||
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();
|
console.Output.WriteLine();
|
||||||
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
// Guild or channel ID
|
||||||
console.Output.WriteLine("To get guild ID or guild channel ID:");
|
using (console.WithForegroundColor(ConsoleColor.White))
|
||||||
|
console.Output.WriteLine("To get guild ID or guild 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 Appearance section");
|
console.Output.WriteLine(" 3. Go to Appearance 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(" 5. Right click on the desired guild or channel and click Copy ID");
|
||||||
console.Output.WriteLine();
|
console.Output.WriteLine();
|
||||||
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
// Direct message channel ID
|
||||||
console.Output.WriteLine("To get 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(" 1. Open Discord");
|
||||||
console.Output.WriteLine(" 2. Open the desired direct message channel");
|
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(" 3. Press Ctrl+Shift+I to show developer tools");
|
||||||
console.Output.WriteLine(" 4. Navigate to the Console tab");
|
console.Output.WriteLine(" 4. Navigate to the Console tab");
|
||||||
console.Output.WriteLine(" 5. Type \"window.location.href\" and press Enter");
|
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(" 6. Copy the first long sequence of numbers inside the URL");
|
||||||
console.Output.WriteLine();
|
console.Output.WriteLine();
|
||||||
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
// Wiki link
|
||||||
console.Output.WriteLine("For more information, check out the wiki:");
|
using (console.WithForegroundColor(ConsoleColor.White))
|
||||||
|
console.Output.WriteLine("If you have questions or issues, please refer to the wiki:");
|
||||||
|
using (console.WithForegroundColor(ConsoleColor.DarkCyan))
|
||||||
|
console.Output.WriteLine("https://github.com/Tyrrrz/DiscordChatExporter/wiki");
|
||||||
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.DarkCyan))
|
return default;
|
||||||
console.Output.WriteLine("https://github.com/Tyrrrz/DiscordChatExporter/wiki");
|
|
||||||
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,10 +6,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CliFx" Version="2.0.6" />
|
<PackageReference Include="CliFx" Version="2.2.2" />
|
||||||
<PackageReference Include="Spectre.Console" Version="0.41.0" />
|
<PackageReference Include="Spectre.Console" Version="0.44.0" />
|
||||||
<PackageReference Include="Gress" Version="1.2.0" />
|
<PackageReference Include="Gress" Version="2.0.1" />
|
||||||
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,14 +1,6 @@
|
|||||||
using System.Threading.Tasks;
|
using CliFx;
|
||||||
using CliFx;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli
|
return await new CliApplicationBuilder()
|
||||||
{
|
.AddCommandsFromThisAssembly()
|
||||||
public static class Program
|
.Build()
|
||||||
{
|
.RunAsync(args);
|
||||||
public static async Task<int> Main(string[] args) =>
|
|
||||||
await new CliApplicationBuilder()
|
|
||||||
.AddCommandsFromThisAssembly()
|
|
||||||
.Build()
|
|
||||||
.RunAsync(args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,50 +3,48 @@ using System.Threading.Tasks;
|
|||||||
using CliFx.Infrastructure;
|
using CliFx.Infrastructure;
|
||||||
using Spectre.Console;
|
using Spectre.Console;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Utils.Extensions
|
namespace DiscordChatExporter.Cli.Utils.Extensions;
|
||||||
|
|
||||||
|
internal static class ConsoleExtensions
|
||||||
{
|
{
|
||||||
internal static class ConsoleExtensions
|
public static IAnsiConsole CreateAnsiConsole(this IConsole console) =>
|
||||||
{
|
AnsiConsole.Create(new AnsiConsoleSettings
|
||||||
public static IAnsiConsole CreateAnsiConsole(this IConsole console) => 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
|
public static async ValueTask StartTaskAsync(
|
||||||
.CreateAnsiConsole()
|
this ProgressContext progressContext,
|
||||||
.Progress()
|
string description,
|
||||||
.AutoClear(false)
|
Func<ProgressTask, ValueTask> performOperationAsync)
|
||||||
.AutoRefresh(true)
|
{
|
||||||
.HideCompleted(false)
|
var progressTask = progressContext.AddTask(
|
||||||
.Columns(
|
// Don't recognize random square brackets as style tags
|
||||||
new TaskDescriptionColumn {Alignment = Justify.Left},
|
Markup.Escape(description),
|
||||||
new ProgressBarColumn(),
|
new ProgressTaskSettings {MaxValue = 1}
|
||||||
new PercentageColumn()
|
);
|
||||||
);
|
|
||||||
|
|
||||||
public static async ValueTask StartTaskAsync(
|
try
|
||||||
this ProgressContext progressContext,
|
|
||||||
string description,
|
|
||||||
Func<ProgressTask, ValueTask> performOperationAsync)
|
|
||||||
{
|
{
|
||||||
var progressTask = progressContext.AddTask(
|
await performOperationAsync(progressTask);
|
||||||
// Don't recognize random square brackets as style tags
|
}
|
||||||
Markup.Escape(description),
|
finally
|
||||||
new ProgressTaskSettings {MaxValue = 1}
|
{
|
||||||
);
|
progressTask.StopTask();
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await performOperationAsync(progressTask);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
progressTask.StopTask();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
// ReSharper disable CheckNamespace
|
|
||||||
// TODO: remove after moving to .NET 5
|
|
||||||
|
|
||||||
namespace System.Runtime.CompilerServices
|
|
||||||
{
|
|
||||||
internal static class IsExternalInit
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
using System.Net.Http.Headers;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord
|
|
||||||
{
|
|
||||||
public class AuthToken
|
|
||||||
{
|
|
||||||
public AuthTokenType Type { get; }
|
|
||||||
|
|
||||||
public string Value { get; }
|
|
||||||
|
|
||||||
public AuthToken(AuthTokenType type, string value)
|
|
||||||
{
|
|
||||||
Type = type;
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AuthenticationHeaderValue GetAuthenticationHeader() => Type switch
|
|
||||||
{
|
|
||||||
AuthTokenType.Bot => new AuthenticationHeaderValue("Bot", Value),
|
|
||||||
_ => new AuthenticationHeaderValue(Value)
|
|
||||||
};
|
|
||||||
|
|
||||||
public override string ToString() => Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace DiscordChatExporter.Core.Discord
|
|
||||||
{
|
|
||||||
public enum AuthTokenType
|
|
||||||
{
|
|
||||||
User,
|
|
||||||
Bot
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,78 +1,46 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using DiscordChatExporter.Core.Discord.Data.Common;
|
using DiscordChatExporter.Core.Discord.Data.Common;
|
||||||
|
using DiscordChatExporter.Core.Utils;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#attachment-object
|
||||||
|
public partial record Attachment(
|
||||||
|
Snowflake Id,
|
||||||
|
string Url,
|
||||||
|
string FileName,
|
||||||
|
string? Description,
|
||||||
|
int? Width,
|
||||||
|
int? Height,
|
||||||
|
FileSize FileSize) : IHasId
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/channel#attachment-object
|
public string FileExtension => Path.GetExtension(FileName);
|
||||||
public partial class Attachment : IHasId
|
|
||||||
|
public bool IsImage => FileFormat.IsImage(FileExtension);
|
||||||
|
|
||||||
|
public bool IsVideo => FileFormat.IsVideo(FileExtension);
|
||||||
|
|
||||||
|
public bool IsAudio => FileFormat.IsAudio(FileExtension);
|
||||||
|
|
||||||
|
public bool IsSpoiler => FileName.StartsWith("SPOILER_", StringComparison.Ordinal);
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial record Attachment
|
||||||
|
{
|
||||||
|
public static Attachment Parse(JsonElement json)
|
||||||
{
|
{
|
||||||
public Snowflake Id { get; }
|
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||||
|
var url = json.GetProperty("url").GetNonWhiteSpaceString();
|
||||||
|
var width = json.GetPropertyOrNull("width")?.GetInt32OrNull();
|
||||||
|
var height = json.GetPropertyOrNull("height")?.GetInt32OrNull();
|
||||||
|
var fileName = json.GetProperty("filename").GetNonNullString();
|
||||||
|
var description = json.GetPropertyOrNull("description")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
var fileSize = json.GetProperty("size").GetInt64().Pipe(FileSize.FromBytes);
|
||||||
|
|
||||||
public string Url { get; }
|
return new Attachment(id, url, fileName, description, width, height, fileSize);
|
||||||
|
|
||||||
public string FileName { get; }
|
|
||||||
|
|
||||||
public string FileExtension => Path.GetExtension(FileName);
|
|
||||||
|
|
||||||
public int? Width { get; }
|
|
||||||
|
|
||||||
public int? Height { get; }
|
|
||||||
|
|
||||||
public bool IsImage => ImageFileExtensions.Contains(FileExtension);
|
|
||||||
|
|
||||||
public bool IsVideo => VideoFileExtensions.Contains(FileExtension);
|
|
||||||
|
|
||||||
public bool IsAudio => AudioFileExtensions.Contains(FileExtension);
|
|
||||||
|
|
||||||
public bool IsSpoiler => FileName.StartsWith("SPOILER_", StringComparison.Ordinal);
|
|
||||||
|
|
||||||
public FileSize FileSize { get; }
|
|
||||||
|
|
||||||
public Attachment(
|
|
||||||
Snowflake id,
|
|
||||||
string url,
|
|
||||||
string fileName,
|
|
||||||
int? width,
|
|
||||||
int? height,
|
|
||||||
FileSize fileSize)
|
|
||||||
{
|
|
||||||
Id = id;
|
|
||||||
Url = url;
|
|
||||||
FileName = fileName;
|
|
||||||
Width = width;
|
|
||||||
Height = height;
|
|
||||||
FileSize = fileSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => FileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Attachment
|
|
||||||
{
|
|
||||||
private static readonly HashSet<string> ImageFileExtensions = new(StringComparer.OrdinalIgnoreCase)
|
|
||||||
{".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"};
|
|
||||||
|
|
||||||
private static readonly HashSet<string> VideoFileExtensions = new(StringComparer.OrdinalIgnoreCase)
|
|
||||||
{".mp4", ".webm"};
|
|
||||||
|
|
||||||
private static readonly HashSet<string> AudioFileExtensions = new(StringComparer.OrdinalIgnoreCase)
|
|
||||||
{".mp3", ".wav", ".ogg", ".flac", ".m4a"};
|
|
||||||
|
|
||||||
public static Attachment Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
|
||||||
var url = json.GetProperty("url").GetString();
|
|
||||||
var width = json.GetPropertyOrNull("width")?.GetInt32();
|
|
||||||
var height = json.GetPropertyOrNull("height")?.GetInt32();
|
|
||||||
var fileName = json.GetProperty("filename").GetString();
|
|
||||||
var fileSize = json.GetProperty("size").GetInt64().Pipe(FileSize.FromBytes);
|
|
||||||
|
|
||||||
return new Attachment(id, url, fileName, width, height, fileSize);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,97 +3,76 @@ 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;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
using Tyrrrz.Extensions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#channel-object
|
||||||
|
public partial record Channel(
|
||||||
|
Snowflake Id,
|
||||||
|
ChannelKind Kind,
|
||||||
|
Snowflake GuildId,
|
||||||
|
ChannelCategory Category,
|
||||||
|
string Name,
|
||||||
|
int? Position,
|
||||||
|
string? Topic) : IHasId
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/channel#channel-object
|
public bool IsTextChannel => Kind is
|
||||||
public partial class Channel : IHasId
|
ChannelKind.GuildTextChat or
|
||||||
{
|
ChannelKind.DirectTextChat or
|
||||||
public Snowflake Id { get; }
|
ChannelKind.DirectGroupTextChat or
|
||||||
|
ChannelKind.GuildNews or
|
||||||
|
ChannelKind.GuildStore;
|
||||||
|
|
||||||
public ChannelType Type { get; }
|
public bool IsVoiceChannel => !IsTextChannel;
|
||||||
|
}
|
||||||
|
|
||||||
public bool IsTextChannel => Type is
|
public partial record Channel
|
||||||
ChannelType.GuildTextChat or
|
{
|
||||||
ChannelType.DirectTextChat or
|
private static ChannelCategory GetFallbackCategory(ChannelKind channelKind) => new(
|
||||||
ChannelType.DirectGroupTextChat or
|
Snowflake.Zero,
|
||||||
ChannelType.GuildNews or
|
channelKind switch
|
||||||
ChannelType.GuildStore;
|
|
||||||
|
|
||||||
public bool IsVoiceChannel => !IsTextChannel;
|
|
||||||
|
|
||||||
public Snowflake GuildId { get; }
|
|
||||||
|
|
||||||
public ChannelCategory Category { get; }
|
|
||||||
|
|
||||||
public string Name { get; }
|
|
||||||
|
|
||||||
public int? Position { get; }
|
|
||||||
|
|
||||||
public string? Topic { get; }
|
|
||||||
|
|
||||||
public Channel(
|
|
||||||
Snowflake id,
|
|
||||||
ChannelType type,
|
|
||||||
Snowflake guildId,
|
|
||||||
ChannelCategory category,
|
|
||||||
string name,
|
|
||||||
int? position,
|
|
||||||
string? topic)
|
|
||||||
{
|
{
|
||||||
Id = id;
|
ChannelKind.GuildTextChat => "Text",
|
||||||
Type = type;
|
ChannelKind.DirectTextChat => "Private",
|
||||||
GuildId = guildId;
|
ChannelKind.DirectGroupTextChat => "Group",
|
||||||
Category = category;
|
ChannelKind.GuildNews => "News",
|
||||||
Name = name;
|
ChannelKind.GuildStore => "Store",
|
||||||
Position = position;
|
_ => "Default"
|
||||||
Topic = topic;
|
},
|
||||||
}
|
null
|
||||||
|
);
|
||||||
|
|
||||||
public override string ToString() => Name;
|
public static Channel Parse(JsonElement json, ChannelCategory? category = null, int? positionHint = null)
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Channel
|
|
||||||
{
|
{
|
||||||
private static ChannelCategory GetFallbackCategory(ChannelType channelType) => new(
|
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||||
Snowflake.Zero,
|
var guildId = json.GetPropertyOrNull("guild_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse);
|
||||||
channelType switch
|
var topic = json.GetPropertyOrNull("topic")?.GetStringOrNull();
|
||||||
{
|
var kind = (ChannelKind)json.GetProperty("type").GetInt32();
|
||||||
ChannelType.GuildTextChat => "Text",
|
|
||||||
ChannelType.DirectTextChat => "Private",
|
var name =
|
||||||
ChannelType.DirectGroupTextChat => "Group",
|
// Guild channel
|
||||||
ChannelType.GuildNews => "News",
|
json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull() ??
|
||||||
ChannelType.GuildStore => "Store",
|
|
||||||
_ => "Default"
|
// DM channel
|
||||||
},
|
json.GetPropertyOrNull("recipients")?
|
||||||
null
|
.EnumerateArrayOrNull()?
|
||||||
|
.Select(User.Parse)
|
||||||
|
.Select(u => u.Name)
|
||||||
|
.Pipe(s => string.Join(", ", s)) ??
|
||||||
|
|
||||||
|
// Fallback
|
||||||
|
id.ToString();
|
||||||
|
|
||||||
|
var position = positionHint ?? json.GetPropertyOrNull("position")?.GetInt32OrNull();
|
||||||
|
|
||||||
|
return new Channel(
|
||||||
|
id,
|
||||||
|
kind,
|
||||||
|
guildId ?? Guild.DirectMessages.Id,
|
||||||
|
category ?? GetFallbackCategory(kind),
|
||||||
|
name,
|
||||||
|
position,
|
||||||
|
topic
|
||||||
);
|
);
|
||||||
|
|
||||||
public static Channel Parse(JsonElement json, ChannelCategory? category = null, int? position = null)
|
|
||||||
{
|
|
||||||
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
|
||||||
var guildId = json.GetPropertyOrNull("guild_id")?.GetString().Pipe(Snowflake.Parse);
|
|
||||||
var topic = json.GetPropertyOrNull("topic")?.GetString();
|
|
||||||
var type = (ChannelType) json.GetProperty("type").GetInt32();
|
|
||||||
|
|
||||||
var name =
|
|
||||||
// Guild channel
|
|
||||||
json.GetPropertyOrNull("name")?.GetString() ??
|
|
||||||
// DM channel
|
|
||||||
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
|
|
||||||
// Fallback
|
|
||||||
id.ToString();
|
|
||||||
|
|
||||||
return new Channel(
|
|
||||||
id,
|
|
||||||
type,
|
|
||||||
guildId ?? Guild.DirectMessages.Id,
|
|
||||||
category ?? GetFallbackCategory(type),
|
|
||||||
name,
|
|
||||||
position ?? json.GetPropertyOrNull("position")?.GetInt32(),
|
|
||||||
topic
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,43 +3,21 @@ using DiscordChatExporter.Core.Discord.Data.Common;
|
|||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
public record ChannelCategory(Snowflake Id, string Name, int? Position) : IHasId
|
||||||
{
|
{
|
||||||
public partial class ChannelCategory : IHasId
|
public static ChannelCategory Unknown { get; } = new(Snowflake.Zero, "<unknown category>", 0);
|
||||||
|
|
||||||
|
public static ChannelCategory Parse(JsonElement json, int? positionHint = null)
|
||||||
{
|
{
|
||||||
public Snowflake Id { get; }
|
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||||
|
var position = positionHint ?? json.GetPropertyOrNull("position")?.GetInt32OrNull();
|
||||||
|
|
||||||
public string Name { get; }
|
var name =
|
||||||
|
json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull() ??
|
||||||
|
id.ToString();
|
||||||
|
|
||||||
public int? Position { get; }
|
return new ChannelCategory(id, name, position);
|
||||||
|
|
||||||
public ChannelCategory(Snowflake id, string name, int? position)
|
|
||||||
{
|
|
||||||
Id = id;
|
|
||||||
Name = name;
|
|
||||||
Position = position;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class ChannelCategory
|
|
||||||
{
|
|
||||||
public static ChannelCategory Parse(JsonElement json, int? position = null)
|
|
||||||
{
|
|
||||||
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
|
||||||
|
|
||||||
var name =
|
|
||||||
json.GetPropertyOrNull("name")?.GetString() ??
|
|
||||||
id.ToString();
|
|
||||||
|
|
||||||
return new ChannelCategory(
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
position ?? json.GetPropertyOrNull("position")?.GetInt32()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ChannelCategory Empty { get; } = new(Snowflake.Zero, "<unknown category>", 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// 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
|
||||||
|
{
|
||||||
|
GuildTextChat = 0,
|
||||||
|
DirectTextChat,
|
||||||
|
GuildVoiceChat,
|
||||||
|
DirectGroupTextChat,
|
||||||
|
GuildCategory,
|
||||||
|
GuildNews,
|
||||||
|
GuildStore
|
||||||
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
namespace DiscordChatExporter.Core.Discord.Data
|
|
||||||
{
|
|
||||||
// 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 ChannelType
|
|
||||||
{
|
|
||||||
GuildTextChat = 0,
|
|
||||||
DirectTextChat,
|
|
||||||
GuildVoiceChat,
|
|
||||||
DirectGroupTextChat,
|
|
||||||
GuildCategory,
|
|
||||||
GuildNews,
|
|
||||||
GuildStore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +1,48 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data.Common
|
namespace DiscordChatExporter.Core.Discord.Data.Common;
|
||||||
|
|
||||||
|
// Loosely based on https://github.com/omar/ByteSize (MIT license)
|
||||||
|
public readonly partial record struct FileSize(long TotalBytes)
|
||||||
{
|
{
|
||||||
// Loosely based on https://github.com/omar/ByteSize (MIT license)
|
public double TotalKiloBytes => TotalBytes / 1024.0;
|
||||||
public readonly partial struct FileSize
|
public double TotalMegaBytes => TotalKiloBytes / 1024.0;
|
||||||
|
public double TotalGigaBytes => TotalMegaBytes / 1024.0;
|
||||||
|
|
||||||
|
private double GetLargestWholeNumberValue()
|
||||||
{
|
{
|
||||||
public long TotalBytes { get; }
|
if (Math.Abs(TotalGigaBytes) >= 1)
|
||||||
|
return TotalGigaBytes;
|
||||||
|
|
||||||
public double TotalKiloBytes => TotalBytes / 1024.0;
|
if (Math.Abs(TotalMegaBytes) >= 1)
|
||||||
public double TotalMegaBytes => TotalKiloBytes / 1024.0;
|
return TotalMegaBytes;
|
||||||
public double TotalGigaBytes => TotalMegaBytes / 1024.0;
|
|
||||||
|
|
||||||
public FileSize(long bytes) => TotalBytes = bytes;
|
if (Math.Abs(TotalKiloBytes) >= 1)
|
||||||
|
return TotalKiloBytes;
|
||||||
|
|
||||||
private double GetLargestWholeNumberValue()
|
return TotalBytes;
|
||||||
{
|
|
||||||
if (Math.Abs(TotalGigaBytes) >= 1)
|
|
||||||
return TotalGigaBytes;
|
|
||||||
|
|
||||||
if (Math.Abs(TotalMegaBytes) >= 1)
|
|
||||||
return TotalMegaBytes;
|
|
||||||
|
|
||||||
if (Math.Abs(TotalKiloBytes) >= 1)
|
|
||||||
return TotalKiloBytes;
|
|
||||||
|
|
||||||
return TotalBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetLargestWholeNumberSymbol()
|
|
||||||
{
|
|
||||||
if (Math.Abs(TotalGigaBytes) >= 1)
|
|
||||||
return "GB";
|
|
||||||
|
|
||||||
if (Math.Abs(TotalMegaBytes) >= 1)
|
|
||||||
return "MB";
|
|
||||||
|
|
||||||
if (Math.Abs(TotalKiloBytes) >= 1)
|
|
||||||
return "KB";
|
|
||||||
|
|
||||||
return "bytes";
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => $"{GetLargestWholeNumberValue():0.##} {GetLargestWholeNumberSymbol()}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial struct FileSize
|
private string GetLargestWholeNumberSymbol()
|
||||||
{
|
{
|
||||||
public static FileSize FromBytes(long bytes) => new(bytes);
|
if (Math.Abs(TotalGigaBytes) >= 1)
|
||||||
|
return "GB";
|
||||||
|
|
||||||
|
if (Math.Abs(TotalMegaBytes) >= 1)
|
||||||
|
return "MB";
|
||||||
|
|
||||||
|
if (Math.Abs(TotalKiloBytes) >= 1)
|
||||||
|
return "KB";
|
||||||
|
|
||||||
|
return "bytes";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
|
public override string ToString() => $"{GetLargestWholeNumberValue():0.##} {GetLargestWholeNumberSymbol()}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial record struct FileSize
|
||||||
|
{
|
||||||
|
public static FileSize FromBytes(long bytes) => new(bytes);
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
namespace DiscordChatExporter.Core.Discord.Data.Common
|
namespace DiscordChatExporter.Core.Discord.Data.Common;
|
||||||
|
|
||||||
|
public interface IHasId
|
||||||
{
|
{
|
||||||
public interface IHasId
|
Snowflake Id { get; }
|
||||||
{
|
|
||||||
Snowflake Id { get; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,12 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data.Common
|
namespace DiscordChatExporter.Core.Discord.Data.Common;
|
||||||
|
|
||||||
|
public class IdBasedEqualityComparer : IEqualityComparer<IHasId>
|
||||||
{
|
{
|
||||||
public partial 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();
|
public bool Equals(IHasId? x, IHasId? y) => x?.Id == y?.Id;
|
||||||
}
|
|
||||||
|
|
||||||
public partial class IdBasedEqualityComparer
|
public int GetHashCode(IHasId obj) => obj.Id.GetHashCode();
|
||||||
{
|
|
||||||
public static IdBasedEqualityComparer Instance { get; } = new();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Drawing;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text.Json;
|
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
|
||||||
using JsonExtensions.Reading;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
|
||||||
{
|
|
||||||
// https://discord.com/developers/docs/resources/channel#embed-object
|
|
||||||
public partial class Embed
|
|
||||||
{
|
|
||||||
public string? Title { get; }
|
|
||||||
|
|
||||||
public string? Url { get; }
|
|
||||||
|
|
||||||
public DateTimeOffset? Timestamp { get; }
|
|
||||||
|
|
||||||
public Color? Color { get; }
|
|
||||||
|
|
||||||
public EmbedAuthor? Author { get; }
|
|
||||||
|
|
||||||
public string? Description { get; }
|
|
||||||
|
|
||||||
public IReadOnlyList<EmbedField> Fields { get; }
|
|
||||||
|
|
||||||
public EmbedImage? Thumbnail { get; }
|
|
||||||
|
|
||||||
public EmbedImage? Image { get; }
|
|
||||||
|
|
||||||
public EmbedFooter? Footer { get; }
|
|
||||||
|
|
||||||
public Embed(
|
|
||||||
string? title,
|
|
||||||
string? url,
|
|
||||||
DateTimeOffset? timestamp,
|
|
||||||
Color? color,
|
|
||||||
EmbedAuthor? author,
|
|
||||||
string? description,
|
|
||||||
IReadOnlyList<EmbedField> fields,
|
|
||||||
EmbedImage? thumbnail,
|
|
||||||
EmbedImage? image,
|
|
||||||
EmbedFooter? footer)
|
|
||||||
{
|
|
||||||
Title = title;
|
|
||||||
Url = url;
|
|
||||||
Timestamp = timestamp;
|
|
||||||
Color = color;
|
|
||||||
Author = author;
|
|
||||||
Description = description;
|
|
||||||
Fields = fields;
|
|
||||||
Thumbnail = thumbnail;
|
|
||||||
Image = image;
|
|
||||||
Footer = footer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public YouTubeVideoEmbedProjection? TryGetYouTubeVideo() => YouTubeVideoEmbedProjection.TryResolve(this);
|
|
||||||
|
|
||||||
public override string ToString() => Title ?? "<untitled embed>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Embed
|
|
||||||
{
|
|
||||||
public static Embed Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var title = json.GetPropertyOrNull("title")?.GetString();
|
|
||||||
var url = json.GetPropertyOrNull("url")?.GetString();
|
|
||||||
var timestamp = json.GetPropertyOrNull("timestamp")?.GetDateTimeOffset();
|
|
||||||
var color = json.GetPropertyOrNull("color")?.GetInt32().Pipe(System.Drawing.Color.FromArgb).ResetAlpha();
|
|
||||||
var description = json.GetPropertyOrNull("description")?.GetString();
|
|
||||||
|
|
||||||
var author = json.GetPropertyOrNull("author")?.Pipe(EmbedAuthor.Parse);
|
|
||||||
var thumbnail = json.GetPropertyOrNull("thumbnail")?.Pipe(EmbedImage.Parse);
|
|
||||||
var image = json.GetPropertyOrNull("image")?.Pipe(EmbedImage.Parse);
|
|
||||||
var footer = json.GetPropertyOrNull("footer")?.Pipe(EmbedFooter.Parse);
|
|
||||||
|
|
||||||
var fields =
|
|
||||||
json.GetPropertyOrNull("fields")?.EnumerateArray().Select(EmbedField.Parse).ToArray() ??
|
|
||||||
Array.Empty<EmbedField>();
|
|
||||||
|
|
||||||
return new Embed(
|
|
||||||
title,
|
|
||||||
url,
|
|
||||||
timestamp,
|
|
||||||
color,
|
|
||||||
author,
|
|
||||||
description,
|
|
||||||
fields,
|
|
||||||
thumbnail,
|
|
||||||
image,
|
|
||||||
footer
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using JsonExtensions.Reading;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
|
||||||
{
|
|
||||||
// https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure
|
|
||||||
public partial class EmbedAuthor
|
|
||||||
{
|
|
||||||
public string? Name { get; }
|
|
||||||
|
|
||||||
public string? Url { get; }
|
|
||||||
|
|
||||||
public string? IconUrl { get; }
|
|
||||||
|
|
||||||
public string? IconProxyUrl { get; }
|
|
||||||
|
|
||||||
public EmbedAuthor(string? name, string? url, string? iconUrl, string? iconProxyUrl)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
Url = url;
|
|
||||||
IconUrl = iconUrl;
|
|
||||||
IconProxyUrl = iconProxyUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Name ?? "<unnamed author>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class EmbedAuthor
|
|
||||||
{
|
|
||||||
public static EmbedAuthor Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var name = json.GetPropertyOrNull("name")?.GetString();
|
|
||||||
var url = json.GetPropertyOrNull("url")?.GetString();
|
|
||||||
var iconUrl = json.GetPropertyOrNull("icon_url")?.GetString();
|
|
||||||
var iconProxyUrl = json.GetPropertyOrNull("proxy_icon_url")?.GetString();
|
|
||||||
|
|
||||||
return new EmbedAuthor(name, url, iconUrl, iconProxyUrl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using JsonExtensions.Reading;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
|
||||||
{
|
|
||||||
// https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
|
|
||||||
public partial class EmbedField
|
|
||||||
{
|
|
||||||
public string Name { get; }
|
|
||||||
|
|
||||||
public string Value { get; }
|
|
||||||
|
|
||||||
public bool IsInline { get; }
|
|
||||||
|
|
||||||
public EmbedField(string name, string value, bool isInline)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
Value = value;
|
|
||||||
IsInline = isInline;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => $"{Name} | {Value}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class EmbedField
|
|
||||||
{
|
|
||||||
public static EmbedField Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var name = json.GetProperty("name").GetString();
|
|
||||||
var value = json.GetProperty("value").GetString();
|
|
||||||
var isInline = json.GetPropertyOrNull("inline")?.GetBoolean() ?? false;
|
|
||||||
|
|
||||||
return new EmbedField(name, value, isInline);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using JsonExtensions.Reading;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
|
||||||
{
|
|
||||||
// https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure
|
|
||||||
public partial class EmbedFooter
|
|
||||||
{
|
|
||||||
public string Text { get; }
|
|
||||||
|
|
||||||
public string? IconUrl { get; }
|
|
||||||
|
|
||||||
public string? IconProxyUrl { get; }
|
|
||||||
|
|
||||||
public EmbedFooter(string text, string? iconUrl, string? iconProxyUrl)
|
|
||||||
{
|
|
||||||
Text = text;
|
|
||||||
IconUrl = iconUrl;
|
|
||||||
IconProxyUrl = iconProxyUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class EmbedFooter
|
|
||||||
{
|
|
||||||
public static EmbedFooter Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var text = json.GetProperty("text").GetString();
|
|
||||||
var iconUrl = json.GetPropertyOrNull("icon_url")?.GetString();
|
|
||||||
var iconProxyUrl = json.GetPropertyOrNull("proxy_icon_url")?.GetString();
|
|
||||||
|
|
||||||
return new EmbedFooter(text, iconUrl, iconProxyUrl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using JsonExtensions.Reading;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
|
||||||
{
|
|
||||||
// https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure
|
|
||||||
public partial class EmbedImage
|
|
||||||
{
|
|
||||||
public string? Url { get; }
|
|
||||||
|
|
||||||
public string? ProxyUrl { get; }
|
|
||||||
|
|
||||||
public int? Width { get; }
|
|
||||||
|
|
||||||
public int? Height { get; }
|
|
||||||
|
|
||||||
public EmbedImage(string? url, string? proxyUrl, int? width, int? height)
|
|
||||||
{
|
|
||||||
Url = url;
|
|
||||||
ProxyUrl = proxyUrl;
|
|
||||||
Height = height;
|
|
||||||
Width = width;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class EmbedImage
|
|
||||||
{
|
|
||||||
public static EmbedImage Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var url = json.GetPropertyOrNull("url")?.GetString();
|
|
||||||
var proxyUrl = json.GetPropertyOrNull("proxy_url")?.GetString();
|
|
||||||
var width = json.GetPropertyOrNull("width")?.GetInt32();
|
|
||||||
var height = json.GetPropertyOrNull("height")?.GetInt32();
|
|
||||||
|
|
||||||
return new EmbedImage(url, proxyUrl, width, height);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#embed-object
|
||||||
|
public partial record Embed(
|
||||||
|
string? Title,
|
||||||
|
string? Url,
|
||||||
|
DateTimeOffset? Timestamp,
|
||||||
|
Color? Color,
|
||||||
|
EmbedAuthor? Author,
|
||||||
|
string? Description,
|
||||||
|
IReadOnlyList<EmbedField> Fields,
|
||||||
|
EmbedImage? Thumbnail,
|
||||||
|
EmbedImage? Image,
|
||||||
|
EmbedFooter? Footer)
|
||||||
|
{
|
||||||
|
public PlainImageEmbedProjection? TryGetPlainImage() =>
|
||||||
|
PlainImageEmbedProjection.TryResolve(this);
|
||||||
|
|
||||||
|
public SpotifyTrackEmbedProjection? TryGetSpotifyTrack() =>
|
||||||
|
SpotifyTrackEmbedProjection.TryResolve(this);
|
||||||
|
|
||||||
|
public YouTubeVideoEmbedProjection? TryGetYouTubeVideo() =>
|
||||||
|
YouTubeVideoEmbedProjection.TryResolve(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial record Embed
|
||||||
|
{
|
||||||
|
public static Embed Parse(JsonElement json)
|
||||||
|
{
|
||||||
|
var title = json.GetPropertyOrNull("title")?.GetStringOrNull();
|
||||||
|
var url = json.GetPropertyOrNull("url")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
var timestamp = json.GetPropertyOrNull("timestamp")?.GetDateTimeOffset();
|
||||||
|
var color = json.GetPropertyOrNull("color")?.GetInt32OrNull()?.Pipe(System.Drawing.Color.FromArgb).ResetAlpha();
|
||||||
|
var description = json.GetPropertyOrNull("description")?.GetStringOrNull();
|
||||||
|
|
||||||
|
var author = json.GetPropertyOrNull("author")?.Pipe(EmbedAuthor.Parse);
|
||||||
|
var thumbnail = json.GetPropertyOrNull("thumbnail")?.Pipe(EmbedImage.Parse);
|
||||||
|
var image = json.GetPropertyOrNull("image")?.Pipe(EmbedImage.Parse);
|
||||||
|
var footer = json.GetPropertyOrNull("footer")?.Pipe(EmbedFooter.Parse);
|
||||||
|
|
||||||
|
var fields =
|
||||||
|
json.GetPropertyOrNull("fields")?.EnumerateArrayOrNull()?.Select(EmbedField.Parse).ToArray() ??
|
||||||
|
Array.Empty<EmbedField>();
|
||||||
|
|
||||||
|
return new Embed(
|
||||||
|
title,
|
||||||
|
url,
|
||||||
|
timestamp,
|
||||||
|
color,
|
||||||
|
author,
|
||||||
|
description,
|
||||||
|
fields,
|
||||||
|
thumbnail,
|
||||||
|
image,
|
||||||
|
footer
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure
|
||||||
|
public record EmbedAuthor(
|
||||||
|
string? Name,
|
||||||
|
string? Url,
|
||||||
|
string? IconUrl,
|
||||||
|
string? IconProxyUrl)
|
||||||
|
{
|
||||||
|
public static EmbedAuthor Parse(JsonElement json)
|
||||||
|
{
|
||||||
|
var name = json.GetPropertyOrNull("name")?.GetStringOrNull();
|
||||||
|
var url = json.GetPropertyOrNull("url")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
var iconUrl = json.GetPropertyOrNull("icon_url")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
var iconProxyUrl = json.GetPropertyOrNull("proxy_icon_url")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
|
||||||
|
return new EmbedAuthor(name, url, iconUrl, iconProxyUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
|
||||||
|
public record EmbedField(
|
||||||
|
string Name,
|
||||||
|
string Value,
|
||||||
|
bool IsInline)
|
||||||
|
{
|
||||||
|
public static EmbedField Parse(JsonElement json)
|
||||||
|
{
|
||||||
|
var name = json.GetProperty("name").GetNonNullString();
|
||||||
|
var value = json.GetProperty("value").GetNonNullString();
|
||||||
|
var isInline = json.GetPropertyOrNull("inline")?.GetBooleanOrNull() ?? false;
|
||||||
|
|
||||||
|
return new EmbedField(name, value, isInline);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure
|
||||||
|
public record EmbedFooter(
|
||||||
|
string Text,
|
||||||
|
string? IconUrl,
|
||||||
|
string? IconProxyUrl)
|
||||||
|
{
|
||||||
|
public static EmbedFooter Parse(JsonElement json)
|
||||||
|
{
|
||||||
|
var text = json.GetProperty("text").GetNonNullString();
|
||||||
|
var iconUrl = json.GetPropertyOrNull("icon_url")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
var iconProxyUrl = json.GetPropertyOrNull("proxy_icon_url")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
|
||||||
|
return new EmbedFooter(text, iconUrl, iconProxyUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure
|
||||||
|
public record EmbedImage(
|
||||||
|
string? Url,
|
||||||
|
string? ProxyUrl,
|
||||||
|
int? Width,
|
||||||
|
int? Height)
|
||||||
|
{
|
||||||
|
public static EmbedImage Parse(JsonElement json)
|
||||||
|
{
|
||||||
|
var url = json.GetPropertyOrNull("url")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
var proxyUrl = json.GetPropertyOrNull("proxy_url")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
var width = json.GetPropertyOrNull("width")?.GetInt32OrNull();
|
||||||
|
var height = json.GetPropertyOrNull("height")?.GetInt32OrNull();
|
||||||
|
|
||||||
|
return new EmbedImage(url, proxyUrl, width, height);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using DiscordChatExporter.Core.Utils;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
|
||||||
|
|
||||||
|
public record PlainImageEmbedProjection(string Url)
|
||||||
|
{
|
||||||
|
public static PlainImageEmbedProjection? TryResolve(Embed embed)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(embed.Url))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Has to be an embed without any data (except URL and image)
|
||||||
|
if (!string.IsNullOrWhiteSpace(embed.Title) ||
|
||||||
|
embed.Timestamp is not null ||
|
||||||
|
embed.Author is not null ||
|
||||||
|
!string.IsNullOrWhiteSpace(embed.Description) ||
|
||||||
|
embed.Fields.Any() ||
|
||||||
|
embed.Footer is not null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Has to be an image file
|
||||||
|
var fileName = Regex.Match(embed.Url, @".+/([^?]*)").Groups[1].Value;
|
||||||
|
if (string.IsNullOrWhiteSpace(fileName) || !FileFormat.IsImage(Path.GetExtension(fileName)))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new PlainImageEmbedProjection(embed.Url);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
|
||||||
|
|
||||||
|
public partial record SpotifyTrackEmbedProjection(string TrackId)
|
||||||
|
{
|
||||||
|
public string Url => $"https://open.spotify.com/embed/track/{TrackId}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial record SpotifyTrackEmbedProjection
|
||||||
|
{
|
||||||
|
private static string? TryParseTrackId(string embedUrl)
|
||||||
|
{
|
||||||
|
// https://open.spotify.com/track/1LHZMWefF9502NPfArRfvP?si=3efac6ce9be04f0a
|
||||||
|
var trackId = Regex.Match(embedUrl, @"spotify\.com/track/(.*?)(?:\?|&|/|$)").Groups[1].Value;
|
||||||
|
if (!string.IsNullOrWhiteSpace(trackId))
|
||||||
|
return trackId;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SpotifyTrackEmbedProjection? TryResolve(Embed embed)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(embed.Url))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var trackId = TryParseTrackId(embed.Url);
|
||||||
|
if (string.IsNullOrWhiteSpace(trackId))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new SpotifyTrackEmbedProjection(trackId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data.Embeds;
|
||||||
|
|
||||||
|
public partial record YouTubeVideoEmbedProjection(string VideoId)
|
||||||
|
{
|
||||||
|
public string Url => $"https://www.youtube.com/embed/{VideoId}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial record YouTubeVideoEmbedProjection
|
||||||
|
{
|
||||||
|
// Adapted from YoutubeExplode
|
||||||
|
// https://github.com/Tyrrrz/YoutubeExplode/blob/5be164be20019783913f76fcc98f18c65aebe9f0/YoutubeExplode/Videos/VideoId.cs#L34-L64
|
||||||
|
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;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static YouTubeVideoEmbedProjection? TryResolve(Embed embed)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(embed.Url))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var videoId = TryParseVideoId(embed.Url);
|
||||||
|
if (string.IsNullOrWhiteSpace(videoId))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new YouTubeVideoEmbedProjection(videoId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,74 +1,72 @@
|
|||||||
using System.Linq;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using DiscordChatExporter.Core.Utils;
|
using DiscordChatExporter.Core.Utils;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
using Tyrrrz.Extensions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/emoji#emoji-object
|
||||||
|
public partial record Emoji(
|
||||||
|
// Only present on custom emoji
|
||||||
|
Snowflake? Id,
|
||||||
|
// Name of custom emoji (e.g. LUL) or actual representation of standard emoji (e.g. 🙂)
|
||||||
|
string Name,
|
||||||
|
bool IsAnimated,
|
||||||
|
string ImageUrl)
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/emoji#emoji-object
|
// Name of custom emoji (e.g. LUL) or name of standard emoji (e.g. slight_smile)
|
||||||
public partial class Emoji
|
public string Code => Id is not null
|
||||||
{
|
? Name
|
||||||
// Only present on custom emoji
|
: EmojiIndex.TryGetCode(Name) ?? Name;
|
||||||
public string? Id { get; }
|
}
|
||||||
|
|
||||||
// Name of custom emoji (e.g. LUL) or actual representation of standard emoji (e.g. 🙂)
|
public partial record Emoji
|
||||||
public string Name { get; }
|
{
|
||||||
|
private static string GetTwemojiName(string name) => string.Join("-",
|
||||||
// Name of custom emoji (e.g. LUL) or name of standard emoji (e.g. slight_smile)
|
name
|
||||||
public string Code => !string.IsNullOrWhiteSpace(Id)
|
|
||||||
? Name
|
|
||||||
: EmojiIndex.TryGetCode(Name) ?? Name;
|
|
||||||
|
|
||||||
public bool IsAnimated { get; }
|
|
||||||
|
|
||||||
public string ImageUrl { get; }
|
|
||||||
|
|
||||||
public Emoji(string? id, string name, bool isAnimated, string imageUrl)
|
|
||||||
{
|
|
||||||
Id = id;
|
|
||||||
Name = name;
|
|
||||||
IsAnimated = isAnimated;
|
|
||||||
ImageUrl = imageUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Emoji
|
|
||||||
{
|
|
||||||
private static string GetTwemojiName(string name) => name
|
|
||||||
.GetRunes()
|
.GetRunes()
|
||||||
// Variant selector rune is skipped in Twemoji names
|
// Variant selector rune is skipped in Twemoji names
|
||||||
.Where(r => r.Value != 0xfe0f)
|
.Where(r => r.Value != 0xfe0f)
|
||||||
.Select(r => r.Value.ToString("x"))
|
.Select(r => r.Value.ToString("x"))
|
||||||
.JoinToString("-");
|
);
|
||||||
|
|
||||||
public static string GetImageUrl(string? id, string name, bool isAnimated)
|
private static string GetImageUrl(Snowflake id, bool isAnimated) => isAnimated
|
||||||
{
|
? $"https://cdn.discordapp.com/emojis/{id}.gif"
|
||||||
// Custom emoji
|
: $"https://cdn.discordapp.com/emojis/{id}.png";
|
||||||
if (!string.IsNullOrWhiteSpace(id))
|
|
||||||
{
|
|
||||||
return isAnimated
|
|
||||||
? $"https://cdn.discordapp.com/emojis/{id}.gif"
|
|
||||||
: $"https://cdn.discordapp.com/emojis/{id}.png";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Standard emoji
|
private static string GetImageUrl(string name) =>
|
||||||
var twemojiName = GetTwemojiName(name);
|
$"https://twemoji.maxcdn.com/2/svg/{GetTwemojiName(name)}.svg";
|
||||||
return $"https://twemoji.maxcdn.com/2/svg/{twemojiName}.svg";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Emoji Parse(JsonElement json)
|
public static string GetImageUrl(Snowflake? id, string? name, bool isAnimated)
|
||||||
{
|
{
|
||||||
var id = json.GetPropertyOrNull("id")?.GetString();
|
// Custom emoji
|
||||||
var name = json.GetProperty("name").GetString();
|
if (id is not null)
|
||||||
var isAnimated = json.GetPropertyOrNull("animated")?.GetBoolean() ?? false;
|
return GetImageUrl(id.Value, isAnimated);
|
||||||
|
|
||||||
var imageUrl = GetImageUrl(id, name, isAnimated);
|
// Standard emoji
|
||||||
|
if (!string.IsNullOrWhiteSpace(name))
|
||||||
|
return GetImageUrl(name);
|
||||||
|
|
||||||
return new Emoji(id, name, isAnimated, imageUrl);
|
// Either ID or name should be set
|
||||||
}
|
throw new ApplicationException("Emoji has neither ID nor name set.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Emoji Parse(JsonElement json)
|
||||||
|
{
|
||||||
|
var id = json.GetPropertyOrNull("id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse);
|
||||||
|
var name = json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
var isAnimated = json.GetPropertyOrNull("animated")?.GetBooleanOrNull() ?? false;
|
||||||
|
|
||||||
|
var imageUrl = GetImageUrl(id, name, isAnimated);
|
||||||
|
|
||||||
|
return new Emoji(
|
||||||
|
id,
|
||||||
|
// Name may be missing if it's an emoji inside a reaction
|
||||||
|
name ?? "<unknown emoji>",
|
||||||
|
isAnimated,
|
||||||
|
imageUrl
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,53 +1,35 @@
|
|||||||
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;
|
||||||
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/guild#guild-object
|
||||||
|
public record Guild(Snowflake Id, string Name, string IconUrl) : IHasId
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/guild#guild-object
|
public static Guild DirectMessages { get; } = new(
|
||||||
public partial class Guild : IHasId
|
Snowflake.Zero,
|
||||||
|
"Direct Messages",
|
||||||
|
GetDefaultIconUrl()
|
||||||
|
);
|
||||||
|
|
||||||
|
private static string GetDefaultIconUrl() =>
|
||||||
|
"https://cdn.discordapp.com/embed/avatars/0.png";
|
||||||
|
|
||||||
|
private static string GetIconUrl(Snowflake id, string iconHash) =>
|
||||||
|
$"https://cdn.discordapp.com/icons/{id}/{iconHash}.png";
|
||||||
|
|
||||||
|
public static Guild Parse(JsonElement json)
|
||||||
{
|
{
|
||||||
public Snowflake Id { get; }
|
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||||
|
var name = json.GetProperty("name").GetNonNullString();
|
||||||
|
var iconHash = json.GetPropertyOrNull("icon")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
|
||||||
public string Name { get; }
|
var iconUrl = !string.IsNullOrWhiteSpace(iconHash)
|
||||||
|
? GetIconUrl(id, iconHash)
|
||||||
|
: GetDefaultIconUrl();
|
||||||
|
|
||||||
public string IconUrl { get; }
|
return new Guild(id, name, iconUrl);
|
||||||
|
|
||||||
public Guild(Snowflake id, string name, string iconUrl)
|
|
||||||
{
|
|
||||||
Id = id;
|
|
||||||
Name = name;
|
|
||||||
IconUrl = iconUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Guild
|
|
||||||
{
|
|
||||||
public static Guild DirectMessages { get; } = new(
|
|
||||||
Snowflake.Zero,
|
|
||||||
"Direct Messages",
|
|
||||||
GetDefaultIconUrl()
|
|
||||||
);
|
|
||||||
|
|
||||||
private static string GetDefaultIconUrl() =>
|
|
||||||
"https://cdn.discordapp.com/embed/avatars/0.png";
|
|
||||||
|
|
||||||
private static string GetIconUrl(Snowflake id, string iconHash) =>
|
|
||||||
$"https://cdn.discordapp.com/icons/{id}/{iconHash}.png";
|
|
||||||
|
|
||||||
public static Guild Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
|
||||||
var name = json.GetProperty("name").GetString();
|
|
||||||
var iconHash = json.GetProperty("icon").GetString();
|
|
||||||
|
|
||||||
var iconUrl = !string.IsNullOrWhiteSpace(iconHash)
|
|
||||||
? GetIconUrl(id, iconHash)
|
|
||||||
: GetDefaultIconUrl();
|
|
||||||
|
|
||||||
return new Guild(id, name, iconUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,51 +6,41 @@ using DiscordChatExporter.Core.Discord.Data.Common;
|
|||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/guild#guild-member-object
|
||||||
|
public partial record Member(
|
||||||
|
User User,
|
||||||
|
string Nick,
|
||||||
|
IReadOnlyList<Snowflake> RoleIds) : IHasId
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/guild#guild-member-object
|
public Snowflake Id => User.Id;
|
||||||
public partial class Member : IHasId
|
}
|
||||||
|
|
||||||
|
public partial record Member
|
||||||
|
{
|
||||||
|
public static Member CreateForUser(User user) => new(
|
||||||
|
user,
|
||||||
|
user.Name,
|
||||||
|
Array.Empty<Snowflake>()
|
||||||
|
);
|
||||||
|
|
||||||
|
public static Member Parse(JsonElement json)
|
||||||
{
|
{
|
||||||
public Snowflake Id => User.Id;
|
var user = json.GetProperty("user").Pipe(User.Parse);
|
||||||
|
var nick = json.GetPropertyOrNull("nick")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
|
||||||
public User User { get; }
|
var roleIds = json
|
||||||
|
.GetPropertyOrNull("roles")?
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(j => j.GetNonWhiteSpaceString())
|
||||||
|
.Select(Snowflake.Parse)
|
||||||
|
.ToArray() ?? Array.Empty<Snowflake>();
|
||||||
|
|
||||||
public string Nick { get; }
|
return new Member(
|
||||||
|
|
||||||
public IReadOnlyList<Snowflake> RoleIds { get; }
|
|
||||||
|
|
||||||
public Member(User user, string nick, IReadOnlyList<Snowflake> roleIds)
|
|
||||||
{
|
|
||||||
User = user;
|
|
||||||
Nick = nick;
|
|
||||||
RoleIds = roleIds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Nick;
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Member
|
|
||||||
{
|
|
||||||
public static Member CreateForUser(User user) => new(
|
|
||||||
user,
|
user,
|
||||||
user.Name,
|
nick ?? user.Name,
|
||||||
Array.Empty<Snowflake>()
|
roleIds
|
||||||
);
|
);
|
||||||
|
|
||||||
public static Member Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var user = json.GetProperty("user").Pipe(User.Parse);
|
|
||||||
var nick = json.GetPropertyOrNull("nick")?.GetString();
|
|
||||||
|
|
||||||
var roleIds =
|
|
||||||
json.GetPropertyOrNull("roles")?.EnumerateArray().Select(j => j.GetString()).Select(Snowflake.Parse).ToArray() ??
|
|
||||||
Array.Empty<Snowflake>();
|
|
||||||
|
|
||||||
return new Member(
|
|
||||||
user,
|
|
||||||
nick ?? user.Name,
|
|
||||||
roleIds
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,150 +3,92 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
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.Discord.Data.Embeds;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#message-object
|
||||||
|
public record Message(
|
||||||
|
Snowflake Id,
|
||||||
|
MessageKind Kind,
|
||||||
|
User Author,
|
||||||
|
DateTimeOffset Timestamp,
|
||||||
|
DateTimeOffset? EditedTimestamp,
|
||||||
|
DateTimeOffset? CallEndedTimestamp,
|
||||||
|
bool IsPinned,
|
||||||
|
string Content,
|
||||||
|
IReadOnlyList<Attachment> Attachments,
|
||||||
|
IReadOnlyList<Embed> Embeds,
|
||||||
|
IReadOnlyList<Sticker> Stickers,
|
||||||
|
IReadOnlyList<Reaction> Reactions,
|
||||||
|
IReadOnlyList<User> MentionedUsers,
|
||||||
|
MessageReference? Reference,
|
||||||
|
Message? ReferencedMessage) : IHasId
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/channel#message-object-message-types
|
public static Message Parse(JsonElement json)
|
||||||
public enum MessageType
|
|
||||||
{
|
{
|
||||||
Default = 0,
|
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||||
RecipientAdd = 1,
|
var author = json.GetProperty("author").Pipe(User.Parse);
|
||||||
RecipientRemove = 2,
|
var timestamp = json.GetProperty("timestamp").GetDateTimeOffset();
|
||||||
Call = 3,
|
var editedTimestamp = json.GetPropertyOrNull("edited_timestamp")?.GetDateTimeOffset();
|
||||||
ChannelNameChange = 4,
|
var callEndedTimestamp = json.GetPropertyOrNull("call")?.GetPropertyOrNull("ended_timestamp")
|
||||||
ChannelIconChange = 5,
|
?.GetDateTimeOffset();
|
||||||
ChannelPinnedMessage = 6,
|
var kind = (MessageKind)json.GetProperty("type").GetInt32();
|
||||||
GuildMemberJoin = 7,
|
var isPinned = json.GetPropertyOrNull("pinned")?.GetBooleanOrNull() ?? false;
|
||||||
Reply = 19
|
var messageReference = json.GetPropertyOrNull("message_reference")?.Pipe(MessageReference.Parse);
|
||||||
}
|
var referencedMessage = json.GetPropertyOrNull("referenced_message")?.Pipe(Parse);
|
||||||
|
|
||||||
// https://discord.com/developers/docs/resources/channel#message-object
|
var content = kind switch
|
||||||
public partial class Message : IHasId
|
|
||||||
{
|
|
||||||
public Snowflake Id { get; }
|
|
||||||
|
|
||||||
public MessageType Type { get; }
|
|
||||||
|
|
||||||
public User Author { get; }
|
|
||||||
|
|
||||||
public DateTimeOffset Timestamp { get; }
|
|
||||||
|
|
||||||
public DateTimeOffset? EditedTimestamp { get; }
|
|
||||||
|
|
||||||
public DateTimeOffset? CallEndedTimestamp { get; }
|
|
||||||
|
|
||||||
public bool IsPinned { get; }
|
|
||||||
|
|
||||||
public string Content { get; }
|
|
||||||
|
|
||||||
public IReadOnlyList<Attachment> Attachments { get; }
|
|
||||||
|
|
||||||
public IReadOnlyList<Embed> Embeds { get; }
|
|
||||||
|
|
||||||
public IReadOnlyList<Reaction> Reactions { get; }
|
|
||||||
|
|
||||||
public IReadOnlyList<User> MentionedUsers { get; }
|
|
||||||
|
|
||||||
public MessageReference? Reference { get; }
|
|
||||||
|
|
||||||
public Message? ReferencedMessage { get; }
|
|
||||||
|
|
||||||
public Message(
|
|
||||||
Snowflake id,
|
|
||||||
MessageType type,
|
|
||||||
User author,
|
|
||||||
DateTimeOffset timestamp,
|
|
||||||
DateTimeOffset? editedTimestamp,
|
|
||||||
DateTimeOffset? callEndedTimestamp,
|
|
||||||
bool isPinned,
|
|
||||||
string content,
|
|
||||||
IReadOnlyList<Attachment> attachments,
|
|
||||||
IReadOnlyList<Embed> embeds,
|
|
||||||
IReadOnlyList<Reaction> reactions,
|
|
||||||
IReadOnlyList<User> mentionedUsers,
|
|
||||||
MessageReference? messageReference,
|
|
||||||
Message? referencedMessage)
|
|
||||||
{
|
{
|
||||||
Id = id;
|
MessageKind.RecipientAdd => "Added a recipient.",
|
||||||
Type = type;
|
MessageKind.RecipientRemove => "Removed a recipient.",
|
||||||
Author = author;
|
MessageKind.Call =>
|
||||||
Timestamp = timestamp;
|
$"Started a call that lasted {callEndedTimestamp?.Pipe(t => t - timestamp).Pipe(t => (int)t.TotalMinutes) ?? 0} minutes.",
|
||||||
EditedTimestamp = editedTimestamp;
|
MessageKind.ChannelNameChange => "Changed the channel name.",
|
||||||
CallEndedTimestamp = callEndedTimestamp;
|
MessageKind.ChannelIconChange => "Changed the channel icon.",
|
||||||
IsPinned = isPinned;
|
MessageKind.ChannelPinnedMessage => "Pinned a message.",
|
||||||
Content = content;
|
MessageKind.GuildMemberJoin => "Joined the server.",
|
||||||
Attachments = attachments;
|
_ => json.GetPropertyOrNull("content")?.GetStringOrNull() ?? ""
|
||||||
Embeds = embeds;
|
};
|
||||||
Reactions = reactions;
|
|
||||||
MentionedUsers = mentionedUsers;
|
|
||||||
Reference = messageReference;
|
|
||||||
ReferencedMessage = referencedMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Content;
|
var attachments =
|
||||||
}
|
json.GetPropertyOrNull("attachments")?.EnumerateArrayOrNull()?.Select(Attachment.Parse).ToArray() ??
|
||||||
|
Array.Empty<Attachment>();
|
||||||
|
|
||||||
public partial class Message
|
var embeds =
|
||||||
{
|
json.GetPropertyOrNull("embeds")?.EnumerateArrayOrNull()?.Select(Embed.Parse).ToArray() ??
|
||||||
public static Message Parse(JsonElement json)
|
Array.Empty<Embed>();
|
||||||
{
|
|
||||||
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
|
||||||
var author = json.GetProperty("author").Pipe(User.Parse);
|
|
||||||
var timestamp = json.GetProperty("timestamp").GetDateTimeOffset();
|
|
||||||
var editedTimestamp = json.GetPropertyOrNull("edited_timestamp")?.GetDateTimeOffset();
|
|
||||||
var callEndedTimestamp = json.GetPropertyOrNull("call")?.GetPropertyOrNull("ended_timestamp")?.GetDateTimeOffset();
|
|
||||||
var type = (MessageType) json.GetProperty("type").GetInt32();
|
|
||||||
var isPinned = json.GetPropertyOrNull("pinned")?.GetBoolean() ?? false;
|
|
||||||
var messageReference = json.GetPropertyOrNull("message_reference")?.Pipe(MessageReference.Parse);
|
|
||||||
var referencedMessage = json.GetPropertyOrNull("referenced_message")?.Pipe(Parse);
|
|
||||||
|
|
||||||
var content = type switch
|
var stickers =
|
||||||
{
|
json.GetPropertyOrNull("sticker_items")?.EnumerateArrayOrNull()?.Select(Sticker.Parse).ToArray() ??
|
||||||
MessageType.RecipientAdd => "Added a recipient.",
|
Array.Empty<Sticker>();
|
||||||
MessageType.RecipientRemove => "Removed a recipient.",
|
|
||||||
MessageType.Call =>
|
|
||||||
$"Started a call that lasted {callEndedTimestamp?.Pipe(t => t - timestamp).Pipe(t => (int) t.TotalMinutes) ?? 0} minutes.",
|
|
||||||
MessageType.ChannelNameChange => "Changed the channel name.",
|
|
||||||
MessageType.ChannelIconChange => "Changed the channel icon.",
|
|
||||||
MessageType.ChannelPinnedMessage => "Pinned a message.",
|
|
||||||
MessageType.GuildMemberJoin => "Joined the server.",
|
|
||||||
_ => json.GetPropertyOrNull("content")?.GetString() ?? ""
|
|
||||||
};
|
|
||||||
|
|
||||||
var attachments =
|
var reactions =
|
||||||
json.GetPropertyOrNull("attachments")?.EnumerateArray().Select(Attachment.Parse).ToArray() ??
|
json.GetPropertyOrNull("reactions")?.EnumerateArrayOrNull()?.Select(Reaction.Parse).ToArray() ??
|
||||||
Array.Empty<Attachment>();
|
Array.Empty<Reaction>();
|
||||||
|
|
||||||
var embeds =
|
var mentionedUsers =
|
||||||
json.GetPropertyOrNull("embeds")?.EnumerateArray().Select(Embed.Parse).ToArray() ??
|
json.GetPropertyOrNull("mentions")?.EnumerateArrayOrNull()?.Select(User.Parse).ToArray() ??
|
||||||
Array.Empty<Embed>();
|
Array.Empty<User>();
|
||||||
|
|
||||||
var reactions =
|
return new Message(
|
||||||
json.GetPropertyOrNull("reactions")?.EnumerateArray().Select(Reaction.Parse).ToArray() ??
|
id,
|
||||||
Array.Empty<Reaction>();
|
kind,
|
||||||
|
author,
|
||||||
var mentionedUsers =
|
timestamp,
|
||||||
json.GetPropertyOrNull("mentions")?.EnumerateArray().Select(User.Parse).ToArray() ??
|
editedTimestamp,
|
||||||
Array.Empty<User>();
|
callEndedTimestamp,
|
||||||
|
isPinned,
|
||||||
return new Message(
|
content,
|
||||||
id,
|
attachments,
|
||||||
type,
|
embeds,
|
||||||
author,
|
stickers,
|
||||||
timestamp,
|
reactions,
|
||||||
editedTimestamp,
|
mentionedUsers,
|
||||||
callEndedTimestamp,
|
messageReference,
|
||||||
isPinned,
|
referencedMessage
|
||||||
content,
|
);
|
||||||
attachments,
|
|
||||||
embeds,
|
|
||||||
reactions,
|
|
||||||
mentionedUsers,
|
|
||||||
messageReference,
|
|
||||||
referencedMessage
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#message-object-message-types
|
||||||
|
public enum MessageKind
|
||||||
|
{
|
||||||
|
Default = 0,
|
||||||
|
RecipientAdd = 1,
|
||||||
|
RecipientRemove = 2,
|
||||||
|
Call = 3,
|
||||||
|
ChannelNameChange = 4,
|
||||||
|
ChannelIconChange = 5,
|
||||||
|
ChannelPinnedMessage = 6,
|
||||||
|
GuildMemberJoin = 7,
|
||||||
|
Reply = 19
|
||||||
|
}
|
||||||
@@ -2,36 +2,17 @@ using System.Text.Json;
|
|||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#message-object-message-reference-structure
|
||||||
|
public record MessageReference(Snowflake? MessageId, Snowflake? ChannelId, Snowflake? GuildId)
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/channel#message-object-message-reference-structure
|
public static MessageReference Parse(JsonElement json)
|
||||||
public partial class MessageReference
|
|
||||||
{
|
{
|
||||||
public Snowflake? MessageId { get; }
|
var messageId = json.GetPropertyOrNull("message_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse);
|
||||||
|
var channelId = json.GetPropertyOrNull("channel_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse);
|
||||||
|
var guildId = json.GetPropertyOrNull("guild_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse);
|
||||||
|
|
||||||
public Snowflake? ChannelId { get; }
|
return new MessageReference(messageId, channelId, guildId);
|
||||||
|
|
||||||
public Snowflake? GuildId { get; }
|
|
||||||
|
|
||||||
public MessageReference(Snowflake? messageId, Snowflake? channelId, Snowflake? guildId)
|
|
||||||
{
|
|
||||||
MessageId = messageId;
|
|
||||||
ChannelId = channelId;
|
|
||||||
GuildId = guildId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => MessageId?.ToString() ?? "<unknown reference>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class MessageReference
|
|
||||||
{
|
|
||||||
public static MessageReference Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var messageId = json.GetPropertyOrNull("message_id")?.GetString().Pipe(Snowflake.Parse);
|
|
||||||
var channelId = json.GetPropertyOrNull("channel_id")?.GetString().Pipe(Snowflake.Parse);
|
|
||||||
var guildId = json.GetPropertyOrNull("guild_id")?.GetString().Pipe(Snowflake.Parse);
|
|
||||||
|
|
||||||
return new MessageReference(messageId, channelId, guildId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,32 +1,16 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#reaction-object
|
||||||
|
public record Reaction(Emoji Emoji, int Count)
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/channel#reaction-object
|
public static Reaction Parse(JsonElement json)
|
||||||
public partial class Reaction
|
|
||||||
{
|
{
|
||||||
public Emoji Emoji { get; }
|
var emoji = json.GetProperty("emoji").Pipe(Emoji.Parse);
|
||||||
|
var count = json.GetProperty("count").GetInt32();
|
||||||
|
|
||||||
public int Count { get; }
|
return new Reaction(emoji, count);
|
||||||
|
|
||||||
public Reaction(Emoji emoji, int count)
|
|
||||||
{
|
|
||||||
Emoji = emoji;
|
|
||||||
Count = count;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => $"{Emoji} ({Count})";
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Reaction
|
|
||||||
{
|
|
||||||
public static Reaction Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var emoji = json.GetProperty("emoji").Pipe(Emoji.Parse);
|
|
||||||
var count = json.GetProperty("count").GetInt32();
|
|
||||||
|
|
||||||
return new Reaction(emoji, count);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,50 +4,24 @@ using DiscordChatExporter.Core.Discord.Data.Common;
|
|||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/topics/permissions#role-object
|
||||||
|
public record Role(Snowflake Id, string Name, int Position, Color? Color) : IHasId
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/topics/permissions#role-object
|
public static Role Parse(JsonElement json)
|
||||||
public partial class Role : IHasId
|
|
||||||
{
|
{
|
||||||
public Snowflake Id { get; }
|
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||||
|
var name = json.GetProperty("name").GetNonNullString();
|
||||||
|
var position = json.GetProperty("position").GetInt32();
|
||||||
|
|
||||||
public string Name { get; }
|
var color = json
|
||||||
|
.GetPropertyOrNull("color")?
|
||||||
|
.GetInt32()
|
||||||
|
.Pipe(System.Drawing.Color.FromArgb)
|
||||||
|
.ResetAlpha()
|
||||||
|
.NullIf(c => c.ToRgb() <= 0);
|
||||||
|
|
||||||
public int Position { get; }
|
return new Role(id, name, position, color);
|
||||||
|
|
||||||
public Color? Color { get; }
|
|
||||||
|
|
||||||
public Role(
|
|
||||||
Snowflake id,
|
|
||||||
string name,
|
|
||||||
int position,
|
|
||||||
Color? color)
|
|
||||||
{
|
|
||||||
Id = id;
|
|
||||||
Name = name;
|
|
||||||
Position = position;
|
|
||||||
Color = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Role
|
|
||||||
{
|
|
||||||
public static Role Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
|
||||||
var name = json.GetProperty("name").GetString();
|
|
||||||
var position = json.GetProperty("position").GetInt32();
|
|
||||||
|
|
||||||
var color = json
|
|
||||||
.GetPropertyOrNull("color")?
|
|
||||||
.GetInt32()
|
|
||||||
.Pipe(System.Drawing.Color.FromArgb)
|
|
||||||
.ResetAlpha()
|
|
||||||
.NullIf(c => c.ToRgb() <= 0);
|
|
||||||
|
|
||||||
return new Role(id, name, position, color);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
public record Sticker(Snowflake Id, string Name, StickerFormat Format, string SourceUrl)
|
||||||
|
{
|
||||||
|
private static string GetSourceUrl(Snowflake id, StickerFormat format)
|
||||||
|
{
|
||||||
|
var extension = format == StickerFormat.Lottie ? "json" : "png";
|
||||||
|
return $"https://discord.com/stickers/{id}.{extension}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Sticker Parse(JsonElement json)
|
||||||
|
{
|
||||||
|
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||||
|
var name = json.GetProperty("name").GetNonWhiteSpaceString();
|
||||||
|
var format = (StickerFormat)json.GetProperty("format_type").GetInt32();
|
||||||
|
|
||||||
|
var sourceUrl = GetSourceUrl(id, format);
|
||||||
|
|
||||||
|
return new Sticker(id, name, format, sourceUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
public enum StickerFormat
|
||||||
|
{
|
||||||
|
Png = 1,
|
||||||
|
PngAnimated = 2,
|
||||||
|
Lottie = 3
|
||||||
|
}
|
||||||
@@ -4,70 +4,47 @@ using DiscordChatExporter.Core.Discord.Data.Common;
|
|||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
namespace DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/user#user-object
|
||||||
|
public partial record User(
|
||||||
|
Snowflake Id,
|
||||||
|
bool IsBot,
|
||||||
|
int Discriminator,
|
||||||
|
string Name,
|
||||||
|
string AvatarUrl) : IHasId
|
||||||
{
|
{
|
||||||
// https://discord.com/developers/docs/resources/user#user-object
|
public string DiscriminatorFormatted => $"{Discriminator:0000}";
|
||||||
public partial class User : IHasId
|
|
||||||
|
public string FullName => $"{Name}#{DiscriminatorFormatted}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial record User
|
||||||
|
{
|
||||||
|
private static string GetDefaultAvatarUrl(int discriminator) =>
|
||||||
|
$"https://cdn.discordapp.com/embed/avatars/{discriminator % 5}.png";
|
||||||
|
|
||||||
|
private static string GetAvatarUrl(Snowflake id, string avatarHash)
|
||||||
{
|
{
|
||||||
public Snowflake Id { get; }
|
var extension = avatarHash.StartsWith("a_", StringComparison.Ordinal)
|
||||||
|
? "gif"
|
||||||
|
: "png";
|
||||||
|
|
||||||
public bool IsBot { get; }
|
return $"https://cdn.discordapp.com/avatars/{id}/{avatarHash}.{extension}?size=512";
|
||||||
|
|
||||||
public int Discriminator { get; }
|
|
||||||
|
|
||||||
public string DiscriminatorFormatted => $"{Discriminator:0000}";
|
|
||||||
|
|
||||||
public string Name { get; }
|
|
||||||
|
|
||||||
public string FullName => $"{Name}#{DiscriminatorFormatted}";
|
|
||||||
|
|
||||||
public string AvatarUrl { get; }
|
|
||||||
|
|
||||||
public User(
|
|
||||||
Snowflake id,
|
|
||||||
bool isBot,
|
|
||||||
int discriminator,
|
|
||||||
string name,
|
|
||||||
string avatarUrl)
|
|
||||||
{
|
|
||||||
Id = id;
|
|
||||||
IsBot = isBot;
|
|
||||||
Discriminator = discriminator;
|
|
||||||
Name = name;
|
|
||||||
AvatarUrl = avatarUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => FullName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class User
|
public static User Parse(JsonElement json)
|
||||||
{
|
{
|
||||||
private static string GetDefaultAvatarUrl(int discriminator) =>
|
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||||
$"https://cdn.discordapp.com/embed/avatars/{discriminator % 5}.png";
|
var isBot = json.GetPropertyOrNull("bot")?.GetBooleanOrNull() ?? false;
|
||||||
|
var discriminator = json.GetProperty("discriminator").GetNonWhiteSpaceString().Pipe(int.Parse);
|
||||||
|
var name = json.GetProperty("username").GetNonNullString();
|
||||||
|
var avatarHash = json.GetPropertyOrNull("avatar")?.GetNonWhiteSpaceStringOrNull();
|
||||||
|
|
||||||
private static string GetAvatarUrl(Snowflake id, string avatarHash)
|
var avatarUrl = !string.IsNullOrWhiteSpace(avatarHash)
|
||||||
{
|
? GetAvatarUrl(id, avatarHash)
|
||||||
// Animated
|
: GetDefaultAvatarUrl(discriminator);
|
||||||
if (avatarHash.StartsWith("a_", StringComparison.Ordinal))
|
|
||||||
return $"https://cdn.discordapp.com/avatars/{id}/{avatarHash}.gif?size=40";
|
|
||||||
|
|
||||||
// Non-animated
|
return new User(id, isBot, discriminator, name, avatarUrl);
|
||||||
return $"https://cdn.discordapp.com/avatars/{id}/{avatarHash}.png?size=40";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static User Parse(JsonElement json)
|
|
||||||
{
|
|
||||||
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
|
||||||
var isBot = json.GetPropertyOrNull("bot")?.GetBoolean() ?? false;
|
|
||||||
var discriminator = json.GetProperty("discriminator").GetString().Pipe(int.Parse);
|
|
||||||
var name = json.GetProperty("username").GetString();
|
|
||||||
var avatarHash = json.GetProperty("avatar").GetString();
|
|
||||||
|
|
||||||
var avatarUrl = !string.IsNullOrWhiteSpace(avatarHash)
|
|
||||||
? GetAvatarUrl(id, avatarHash)
|
|
||||||
: GetDefaultAvatarUrl(discriminator);
|
|
||||||
|
|
||||||
return new User(id, isBot, discriminator, name, avatarUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data
|
|
||||||
{
|
|
||||||
public partial class YouTubeVideoEmbedProjection
|
|
||||||
{
|
|
||||||
public string VideoId { get; }
|
|
||||||
|
|
||||||
public string Url { get; }
|
|
||||||
|
|
||||||
public YouTubeVideoEmbedProjection(string videoId, string url)
|
|
||||||
{
|
|
||||||
VideoId = videoId;
|
|
||||||
Url = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class YouTubeVideoEmbedProjection
|
|
||||||
{
|
|
||||||
// Adapted from YoutubeExplode
|
|
||||||
// https://github.com/Tyrrrz/YoutubeExplode/blob/5be164be20019783913f76fcc98f18c65aebe9f0/YoutubeExplode/Videos/VideoId.cs#L34-L64
|
|
||||||
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;
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static YouTubeVideoEmbedProjection? TryResolve(Embed embed)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(embed.Url))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var videoId = TryParseVideoId(embed.Url);
|
|
||||||
if (string.IsNullOrWhiteSpace(videoId))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var url = $"https://www.youtube.com/embed/{videoId}";
|
|
||||||
|
|
||||||
return new YouTubeVideoEmbedProjection(videoId, url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,279 +3,340 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DiscordChatExporter.Core.Discord.Data;
|
using DiscordChatExporter.Core.Discord.Data;
|
||||||
using DiscordChatExporter.Core.Exceptions;
|
using DiscordChatExporter.Core.Exceptions;
|
||||||
using DiscordChatExporter.Core.Utils;
|
using DiscordChatExporter.Core.Utils;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
using Gress;
|
||||||
using JsonExtensions.Http;
|
using JsonExtensions.Http;
|
||||||
using JsonExtensions.Reading;
|
using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord
|
namespace DiscordChatExporter.Core.Discord;
|
||||||
|
|
||||||
|
public class DiscordClient
|
||||||
{
|
{
|
||||||
public class DiscordClient
|
private readonly string _token;
|
||||||
|
private readonly Uri _baseUri = new("https://discord.com/api/v9/", UriKind.Absolute);
|
||||||
|
|
||||||
|
private TokenKind _tokenKind = TokenKind.Unknown;
|
||||||
|
|
||||||
|
public DiscordClient(string token) => _token = token;
|
||||||
|
|
||||||
|
private async ValueTask<HttpResponseMessage> GetResponseAsync(
|
||||||
|
string url,
|
||||||
|
bool isBot,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient;
|
using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(_baseUri, url));
|
||||||
private readonly AuthToken _token;
|
|
||||||
|
|
||||||
private readonly Uri _baseUri = new("https://discord.com/api/v8/", UriKind.Absolute);
|
// Don't validate because token can have invalid characters
|
||||||
|
// https://github.com/Tyrrrz/DiscordChatExporter/issues/828
|
||||||
|
request.Headers.TryAddWithoutValidation(
|
||||||
|
"Authorization",
|
||||||
|
isBot ? $"Bot {_token}" : _token
|
||||||
|
);
|
||||||
|
|
||||||
public DiscordClient(HttpClient httpClient, AuthToken token)
|
return await Http.Client.SendAsync(
|
||||||
|
request,
|
||||||
|
HttpCompletionOption.ResponseHeadersRead,
|
||||||
|
cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ValueTask<HttpResponseMessage> GetResponseAsync(
|
||||||
|
string url,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await Http.ResponsePolicy.ExecuteAsync(async innerCancellationToken =>
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
if (_tokenKind == TokenKind.User)
|
||||||
_token = token;
|
return await GetResponseAsync(url, false, innerCancellationToken);
|
||||||
}
|
|
||||||
|
|
||||||
public DiscordClient(AuthToken token)
|
if (_tokenKind == TokenKind.Bot)
|
||||||
: this(Http.Client, token)
|
return await GetResponseAsync(url, true, innerCancellationToken);
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private async ValueTask<HttpResponseMessage> GetResponseAsync(string url) =>
|
// Try to authenticate as user
|
||||||
await Http.ResponsePolicy.ExecuteAsync(async () =>
|
var userResponse = await GetResponseAsync(url, false, innerCancellationToken);
|
||||||
|
if (userResponse.StatusCode != HttpStatusCode.Unauthorized)
|
||||||
{
|
{
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(_baseUri, url));
|
_tokenKind = TokenKind.User;
|
||||||
request.Headers.Authorization = _token.GetAuthenticationHeader();
|
return userResponse;
|
||||||
|
|
||||||
return await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
|
||||||
});
|
|
||||||
|
|
||||||
private async ValueTask<JsonElement> GetJsonResponseAsync(string url)
|
|
||||||
{
|
|
||||||
using var response = await GetResponseAsync(url);
|
|
||||||
|
|
||||||
if (!response.IsSuccessStatusCode)
|
|
||||||
{
|
|
||||||
throw response.StatusCode switch
|
|
||||||
{
|
|
||||||
HttpStatusCode.Unauthorized => DiscordChatExporterException.Unauthorized(),
|
|
||||||
HttpStatusCode.Forbidden => DiscordChatExporterException.Forbidden(),
|
|
||||||
HttpStatusCode.NotFound => DiscordChatExporterException.NotFound(url),
|
|
||||||
_ => DiscordChatExporterException.FailedHttpRequest(response)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return await response.Content.ReadAsJsonAsync();
|
userResponse.Dispose();
|
||||||
}
|
|
||||||
|
|
||||||
private async ValueTask<JsonElement?> TryGetJsonResponseAsync(string url)
|
// Otherwise, try to authenticate as bot
|
||||||
{
|
var botResponse = await GetResponseAsync(url, true, innerCancellationToken);
|
||||||
using var response = await GetResponseAsync(url);
|
if (botResponse.StatusCode != HttpStatusCode.Unauthorized)
|
||||||
|
|
||||||
return response.IsSuccessStatusCode
|
|
||||||
? await response.Content.ReadAsJsonAsync()
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async IAsyncEnumerable<Guild> GetUserGuildsAsync()
|
|
||||||
{
|
|
||||||
yield return Guild.DirectMessages;
|
|
||||||
|
|
||||||
var currentAfter = Snowflake.Zero;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
var url = new UrlBuilder()
|
_tokenKind = TokenKind.Bot;
|
||||||
.SetPath("users/@me/guilds")
|
return botResponse;
|
||||||
.SetQueryParameter("limit", "100")
|
|
||||||
.SetQueryParameter("after", currentAfter.ToString())
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
var response = await GetJsonResponseAsync(url);
|
|
||||||
|
|
||||||
var isEmpty = true;
|
|
||||||
foreach (var guild in response.EnumerateArray().Select(Guild.Parse))
|
|
||||||
{
|
|
||||||
yield return guild;
|
|
||||||
|
|
||||||
currentAfter = guild.Id;
|
|
||||||
isEmpty = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isEmpty)
|
|
||||||
yield break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public async ValueTask<Guild> GetGuildAsync(Snowflake guildId)
|
// The token is probably invalid altogether.
|
||||||
|
// Return the last response anyway, upstream should handle the error.
|
||||||
|
return botResponse;
|
||||||
|
}, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ValueTask<JsonElement> GetJsonResponseAsync(
|
||||||
|
string url,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
using var response = await GetResponseAsync(url, cancellationToken);
|
||||||
|
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
if (guildId == Guild.DirectMessages.Id)
|
throw response.StatusCode switch
|
||||||
return Guild.DirectMessages;
|
|
||||||
|
|
||||||
var response = await GetJsonResponseAsync($"guilds/{guildId}");
|
|
||||||
return Guild.Parse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async IAsyncEnumerable<Channel> GetGuildChannelsAsync(Snowflake guildId)
|
|
||||||
{
|
|
||||||
if (guildId == Guild.DirectMessages.Id)
|
|
||||||
{
|
{
|
||||||
var response = await GetJsonResponseAsync("users/@me/channels");
|
HttpStatusCode.Unauthorized => DiscordChatExporterException.Unauthorized(),
|
||||||
foreach (var channelJson in response.EnumerateArray())
|
HttpStatusCode.Forbidden => DiscordChatExporterException.Forbidden(),
|
||||||
yield return Channel.Parse(channelJson);
|
HttpStatusCode.NotFound => DiscordChatExporterException.NotFound(url),
|
||||||
}
|
_ => DiscordChatExporterException.FailedHttpRequest(response)
|
||||||
else
|
};
|
||||||
{
|
|
||||||
var response = await GetJsonResponseAsync($"guilds/{guildId}/channels");
|
|
||||||
|
|
||||||
var responseOrdered = response
|
|
||||||
.EnumerateArray()
|
|
||||||
.OrderBy(j => j.GetProperty("position").GetInt32())
|
|
||||||
.ThenBy(j => Snowflake.Parse(j.GetProperty("id").GetString()))
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
var categories = responseOrdered
|
|
||||||
.Where(j => j.GetProperty("type").GetInt32() == (int) ChannelType.GuildCategory)
|
|
||||||
.Select((j, index) => ChannelCategory.Parse(j, index + 1))
|
|
||||||
.ToDictionary(j => j.Id.ToString(), StringComparer.Ordinal);
|
|
||||||
|
|
||||||
var position = 0;
|
|
||||||
|
|
||||||
foreach (var channelJson in responseOrdered)
|
|
||||||
{
|
|
||||||
var parentId = channelJson.GetPropertyOrNull("parent_id")?.GetString();
|
|
||||||
|
|
||||||
var category = !string.IsNullOrWhiteSpace(parentId)
|
|
||||||
? categories.GetValueOrDefault(parentId)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
var channel = Channel.Parse(channelJson, category, position);
|
|
||||||
|
|
||||||
position++;
|
|
||||||
|
|
||||||
yield return channel;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async IAsyncEnumerable<Role> GetGuildRolesAsync(Snowflake guildId)
|
return await response.Content.ReadAsJsonAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ValueTask<JsonElement?> TryGetJsonResponseAsync(
|
||||||
|
string url,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
using var response = await GetResponseAsync(url, cancellationToken);
|
||||||
|
|
||||||
|
return response.IsSuccessStatusCode
|
||||||
|
? await response.Content.ReadAsJsonAsync(cancellationToken)
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async IAsyncEnumerable<Guild> GetUserGuildsAsync(
|
||||||
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
yield return Guild.DirectMessages;
|
||||||
|
|
||||||
|
var currentAfter = Snowflake.Zero;
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
if (guildId == Guild.DirectMessages.Id)
|
var url = new UrlBuilder()
|
||||||
|
.SetPath("users/@me/guilds")
|
||||||
|
.SetQueryParameter("limit", "100")
|
||||||
|
.SetQueryParameter("after", currentAfter.ToString())
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var response = await GetJsonResponseAsync(url, cancellationToken);
|
||||||
|
|
||||||
|
var isEmpty = true;
|
||||||
|
foreach (var guild in response.EnumerateArray().Select(Guild.Parse))
|
||||||
|
{
|
||||||
|
yield return guild;
|
||||||
|
|
||||||
|
currentAfter = guild.Id;
|
||||||
|
isEmpty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEmpty)
|
||||||
yield break;
|
yield break;
|
||||||
|
|
||||||
var response = await GetJsonResponseAsync($"guilds/{guildId}/roles");
|
|
||||||
|
|
||||||
foreach (var roleJson in response.EnumerateArray())
|
|
||||||
yield return Role.Parse(roleJson);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async ValueTask<Member?> TryGetGuildMemberAsync(Snowflake guildId, User user)
|
public async ValueTask<Guild> GetGuildAsync(
|
||||||
|
Snowflake guildId,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (guildId == Guild.DirectMessages.Id)
|
||||||
|
return Guild.DirectMessages;
|
||||||
|
|
||||||
|
var response = await GetJsonResponseAsync($"guilds/{guildId}", cancellationToken);
|
||||||
|
return Guild.Parse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async IAsyncEnumerable<Channel> GetGuildChannelsAsync(
|
||||||
|
Snowflake guildId,
|
||||||
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (guildId == Guild.DirectMessages.Id)
|
||||||
{
|
{
|
||||||
if (guildId == Guild.DirectMessages.Id)
|
var response = await GetJsonResponseAsync("users/@me/channels", cancellationToken);
|
||||||
return Member.CreateForUser(user);
|
foreach (var channelJson in response.EnumerateArray())
|
||||||
|
yield return Channel.Parse(channelJson);
|
||||||
var response = await TryGetJsonResponseAsync($"guilds/{guildId}/members/{user.Id}");
|
|
||||||
return response?.Pipe(Member.Parse);
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
public async ValueTask<ChannelCategory> GetChannelCategoryAsync(Snowflake channelId)
|
|
||||||
{
|
{
|
||||||
try
|
var response = await GetJsonResponseAsync($"guilds/{guildId}/channels", cancellationToken);
|
||||||
|
|
||||||
|
var responseOrdered = response
|
||||||
|
.EnumerateArray()
|
||||||
|
.OrderBy(j => j.GetProperty("position").GetInt32())
|
||||||
|
.ThenBy(j => j.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
var categories = responseOrdered
|
||||||
|
.Where(j => j.GetProperty("type").GetInt32() == (int) ChannelKind.GuildCategory)
|
||||||
|
.Select((j, index) => ChannelCategory.Parse(j, index + 1))
|
||||||
|
.ToDictionary(j => j.Id.ToString(), StringComparer.Ordinal);
|
||||||
|
|
||||||
|
var position = 0;
|
||||||
|
|
||||||
|
foreach (var channelJson in responseOrdered)
|
||||||
{
|
{
|
||||||
var response = await GetJsonResponseAsync($"channels/{channelId}");
|
var parentId = channelJson.GetPropertyOrNull("parent_id")?.GetNonWhiteSpaceStringOrNull();
|
||||||
return ChannelCategory.Parse(response);
|
|
||||||
}
|
var category = !string.IsNullOrWhiteSpace(parentId)
|
||||||
// In some cases, the Discord API returns an empty body when requesting channel category.
|
? categories.GetValueOrDefault(parentId)
|
||||||
// Instead, we use an empty channel category as a fallback.
|
: null;
|
||||||
catch (DiscordChatExporterException)
|
|
||||||
{
|
var channel = Channel.Parse(channelJson, category, position);
|
||||||
return ChannelCategory.Empty;
|
|
||||||
|
position++;
|
||||||
|
|
||||||
|
yield return channel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async ValueTask<Channel> GetChannelAsync(Snowflake channelId)
|
public async IAsyncEnumerable<Role> GetGuildRolesAsync(
|
||||||
|
Snowflake guildId,
|
||||||
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (guildId == Guild.DirectMessages.Id)
|
||||||
|
yield break;
|
||||||
|
|
||||||
|
var response = await GetJsonResponseAsync($"guilds/{guildId}/roles", cancellationToken);
|
||||||
|
|
||||||
|
foreach (var roleJson in response.EnumerateArray())
|
||||||
|
yield return Role.Parse(roleJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<Member> GetGuildMemberAsync(
|
||||||
|
Snowflake guildId,
|
||||||
|
User user,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (guildId == Guild.DirectMessages.Id)
|
||||||
|
return Member.CreateForUser(user);
|
||||||
|
|
||||||
|
var response = await TryGetJsonResponseAsync($"guilds/{guildId}/members/{user.Id}", cancellationToken);
|
||||||
|
return response?.Pipe(Member.Parse) ?? Member.CreateForUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<ChannelCategory> GetChannelCategoryAsync(
|
||||||
|
Snowflake channelId,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var response = await GetJsonResponseAsync($"channels/{channelId}");
|
var response = await GetJsonResponseAsync($"channels/{channelId}", cancellationToken);
|
||||||
|
return ChannelCategory.Parse(response);
|
||||||
var parentId = response.GetPropertyOrNull("parent_id")?.GetString().Pipe(Snowflake.Parse);
|
|
||||||
|
|
||||||
var category = parentId is not null
|
|
||||||
? await GetChannelCategoryAsync(parentId.Value)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
return Channel.Parse(response, category);
|
|
||||||
}
|
}
|
||||||
|
// 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 ChannelCategory.Unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async ValueTask<Message?> TryGetLastMessageAsync(Snowflake channelId, Snowflake? before = null)
|
public async ValueTask<Channel> GetChannelAsync(
|
||||||
|
Snowflake channelId,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var response = await GetJsonResponseAsync($"channels/{channelId}", cancellationToken);
|
||||||
|
|
||||||
|
var parentId = response.GetPropertyOrNull("parent_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse);
|
||||||
|
|
||||||
|
var category = parentId is not null
|
||||||
|
? await GetChannelCategoryAsync(parentId.Value, cancellationToken)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
return Channel.Parse(response, category);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ValueTask<Message?> TryGetLastMessageAsync(
|
||||||
|
Snowflake channelId,
|
||||||
|
Snowflake? before = null,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var url = new UrlBuilder()
|
||||||
|
.SetPath($"channels/{channelId}/messages")
|
||||||
|
.SetQueryParameter("limit", "1")
|
||||||
|
.SetQueryParameter("before", before?.ToString())
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var response = await GetJsonResponseAsync(url, cancellationToken);
|
||||||
|
return response.EnumerateArray().Select(Message.Parse).LastOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async IAsyncEnumerable<Message> GetMessagesAsync(
|
||||||
|
Snowflake channelId,
|
||||||
|
Snowflake? after = null,
|
||||||
|
Snowflake? before = null,
|
||||||
|
IProgress<Percentage>? progress = null,
|
||||||
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
// Get the last message in the specified range.
|
||||||
|
// This snapshots the boundaries, which means that messages posted after the export started
|
||||||
|
// will not appear in the output.
|
||||||
|
// Additionally, it provides the date of the last message, which is used to calculate progress.
|
||||||
|
var lastMessage = await TryGetLastMessageAsync(channelId, before, cancellationToken);
|
||||||
|
if (lastMessage is null || lastMessage.Timestamp < after?.ToDate())
|
||||||
|
yield break;
|
||||||
|
|
||||||
|
// Keep track of first message in range in order to calculate progress
|
||||||
|
var firstMessage = default(Message);
|
||||||
|
var currentAfter = after ?? Snowflake.Zero;
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
var url = new UrlBuilder()
|
var url = new UrlBuilder()
|
||||||
.SetPath($"channels/{channelId}/messages")
|
.SetPath($"channels/{channelId}/messages")
|
||||||
.SetQueryParameter("limit", "1")
|
.SetQueryParameter("limit", "100")
|
||||||
.SetQueryParameter("before", before?.ToString())
|
.SetQueryParameter("after", currentAfter.ToString())
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var response = await GetJsonResponseAsync(url);
|
var response = await GetJsonResponseAsync(url, cancellationToken);
|
||||||
return response.EnumerateArray().Select(Message.Parse).LastOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async IAsyncEnumerable<Message> GetMessagesAsync(
|
var messages = response
|
||||||
Snowflake channelId,
|
.EnumerateArray()
|
||||||
Snowflake? after = null,
|
.Select(Message.Parse)
|
||||||
Snowflake? before = null,
|
.Reverse() // reverse because messages appear newest first
|
||||||
IProgress<double>? progress = null)
|
.ToArray();
|
||||||
{
|
|
||||||
// Get the last message in the specified range.
|
// Break if there are no messages (can happen if messages are deleted during execution)
|
||||||
// This snapshots the boundaries, which means that messages posted after the export started
|
if (!messages.Any())
|
||||||
// will not appear in the output.
|
|
||||||
// Additionally, it provides the date of the last message, which is used to calculate progress.
|
|
||||||
var lastMessage = await TryGetLastMessageAsync(channelId, before);
|
|
||||||
if (lastMessage is null || lastMessage.Timestamp < after?.ToDate())
|
|
||||||
yield break;
|
yield break;
|
||||||
|
|
||||||
// Keep track of first message in range in order to calculate progress
|
foreach (var message in messages)
|
||||||
var firstMessage = default(Message);
|
|
||||||
var currentAfter = after ?? Snowflake.Zero;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
var url = new UrlBuilder()
|
firstMessage ??= message;
|
||||||
.SetPath($"channels/{channelId}/messages")
|
|
||||||
.SetQueryParameter("limit", "100")
|
|
||||||
.SetQueryParameter("after", currentAfter.ToString())
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
var response = await GetJsonResponseAsync(url);
|
// Ensure messages are in range (take into account that last message could have been deleted)
|
||||||
|
if (message.Timestamp > lastMessage.Timestamp)
|
||||||
var messages = response
|
|
||||||
.EnumerateArray()
|
|
||||||
.Select(Message.Parse)
|
|
||||||
.Reverse() // reverse because messages appear newest first
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
// Break if there are no messages (can happen if messages are deleted during execution)
|
|
||||||
if (!messages.Any())
|
|
||||||
yield break;
|
yield break;
|
||||||
|
|
||||||
foreach (var message in messages)
|
// Report progress based on the duration of exported messages divided by total
|
||||||
|
if (progress is not null)
|
||||||
{
|
{
|
||||||
firstMessage ??= message;
|
var exportedDuration = (message.Timestamp - firstMessage.Timestamp).Duration();
|
||||||
|
var totalDuration = (lastMessage.Timestamp - firstMessage.Timestamp).Duration();
|
||||||
|
|
||||||
// Ensure messages are in range (take into account that last message could have been deleted)
|
progress.Report(Percentage.FromFraction(
|
||||||
if (message.Timestamp > lastMessage.Timestamp)
|
|
||||||
yield break;
|
|
||||||
|
|
||||||
// Report progress based on the duration of exported messages divided by total
|
|
||||||
if (progress is not null)
|
|
||||||
{
|
|
||||||
var exportedDuration = (message.Timestamp - firstMessage.Timestamp).Duration();
|
|
||||||
var totalDuration = (lastMessage.Timestamp - firstMessage.Timestamp).Duration();
|
|
||||||
|
|
||||||
if (totalDuration > TimeSpan.Zero)
|
|
||||||
{
|
|
||||||
progress.Report(exportedDuration / totalDuration);
|
|
||||||
}
|
|
||||||
// Avoid division by zero if all messages have the exact same timestamp
|
// Avoid division by zero if all messages have the exact same timestamp
|
||||||
// (which may be the case if there's only one message in the channel)
|
// (which may be the case if there's only one message in the channel)
|
||||||
else
|
totalDuration > TimeSpan.Zero
|
||||||
{
|
? exportedDuration / totalDuration
|
||||||
progress.Report(1);
|
: 1
|
||||||
}
|
));
|
||||||
}
|
|
||||||
|
|
||||||
yield return message;
|
|
||||||
currentAfter = message.Id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
yield return message;
|
||||||
|
currentAfter = message.Id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,71 +1,55 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord
|
namespace DiscordChatExporter.Core.Discord;
|
||||||
|
|
||||||
|
public readonly partial record struct Snowflake(ulong Value)
|
||||||
{
|
{
|
||||||
public readonly partial struct Snowflake
|
public DateTimeOffset ToDate() => DateTimeOffset.FromUnixTimeMilliseconds(
|
||||||
|
(long)((Value >> 22) + 1420070400000UL)
|
||||||
|
).ToLocalTime();
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
|
public override string ToString() => Value.ToString(CultureInfo.InvariantCulture);
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial record struct Snowflake
|
||||||
|
{
|
||||||
|
public static Snowflake Zero { get; } = new(0);
|
||||||
|
|
||||||
|
public static Snowflake FromDate(DateTimeOffset date) => new(
|
||||||
|
((ulong)date.ToUnixTimeMilliseconds() - 1420070400000UL) << 22
|
||||||
|
);
|
||||||
|
|
||||||
|
public static Snowflake? TryParse(string? str, IFormatProvider? formatProvider = null)
|
||||||
{
|
{
|
||||||
public ulong Value { get; }
|
if (string.IsNullOrWhiteSpace(str))
|
||||||
|
return null;
|
||||||
|
|
||||||
public Snowflake(ulong value) => Value = value;
|
// As number
|
||||||
|
if (Regex.IsMatch(str, @"^\d+$") && ulong.TryParse(str, NumberStyles.Number, formatProvider, out var value))
|
||||||
public DateTimeOffset ToDate() => DateTimeOffset.FromUnixTimeMilliseconds(
|
|
||||||
(long) ((Value >> 22) + 1420070400000UL)
|
|
||||||
).ToLocalTime();
|
|
||||||
|
|
||||||
public override string ToString() => Value.ToString(CultureInfo.InvariantCulture);
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial struct Snowflake
|
|
||||||
{
|
|
||||||
public static Snowflake Zero { get; } = new(0);
|
|
||||||
|
|
||||||
public static Snowflake FromDate(DateTimeOffset date)
|
|
||||||
{
|
{
|
||||||
var value = ((ulong) date.ToUnixTimeMilliseconds() - 1420070400000UL) << 22;
|
|
||||||
return new Snowflake(value);
|
return new Snowflake(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Snowflake? TryParse(string? str, IFormatProvider? formatProvider = null)
|
// As date
|
||||||
|
if (DateTimeOffset.TryParse(str, formatProvider, DateTimeStyles.None, out var date))
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(str))
|
return FromDate(date);
|
||||||
return null;
|
|
||||||
|
|
||||||
// As number
|
|
||||||
if (Regex.IsMatch(str, @"^\d+$") &&
|
|
||||||
ulong.TryParse(str, NumberStyles.Number, formatProvider, out var value))
|
|
||||||
{
|
|
||||||
return new Snowflake(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// As date
|
|
||||||
if (DateTimeOffset.TryParse(str, formatProvider, DateTimeStyles.None, out var date))
|
|
||||||
{
|
|
||||||
return FromDate(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Snowflake Parse(string str, IFormatProvider? formatProvider) =>
|
return null;
|
||||||
TryParse(str, formatProvider) ?? throw new FormatException($"Invalid snowflake '{str}'.");
|
|
||||||
|
|
||||||
public static Snowflake Parse(string str) => Parse(str, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial struct Snowflake : IComparable<Snowflake>, IEquatable<Snowflake>
|
public static Snowflake Parse(string str, IFormatProvider? formatProvider) =>
|
||||||
{
|
TryParse(str, formatProvider) ?? throw new FormatException($"Invalid snowflake '{str}'.");
|
||||||
public int CompareTo(Snowflake other) => Value.CompareTo(other.Value);
|
|
||||||
|
|
||||||
public bool Equals(Snowflake other) => CompareTo(other) == 0;
|
public static Snowflake Parse(string str) => Parse(str, null);
|
||||||
|
}
|
||||||
|
|
||||||
public override bool Equals(object? obj) => obj is Snowflake other && Equals(other);
|
public partial record struct Snowflake : IComparable<Snowflake>
|
||||||
|
{
|
||||||
public override int GetHashCode() => Value.GetHashCode();
|
public int CompareTo(Snowflake other) => Value.CompareTo(other.Value);
|
||||||
|
|
||||||
public static bool operator ==(Snowflake left, Snowflake right) => left.Equals(right);
|
|
||||||
|
|
||||||
public static bool operator !=(Snowflake left, Snowflake right) => !(left == right);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace DiscordChatExporter.Core.Discord;
|
||||||
|
|
||||||
|
public enum TokenKind
|
||||||
|
{
|
||||||
|
Unknown,
|
||||||
|
User,
|
||||||
|
Bot
|
||||||
|
}
|
||||||
@@ -5,11 +5,11 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JsonExtensions" Version="1.1.0" />
|
<PackageReference Include="Gress" Version="2.0.1" />
|
||||||
<PackageReference Include="MiniRazor.CodeGen" Version="2.1.2" />
|
<PackageReference Include="JsonExtensions" Version="1.2.0" />
|
||||||
<PackageReference Include="Polly" Version="7.2.2" />
|
<PackageReference Include="MiniRazor.CodeGen" Version="2.2.1" />
|
||||||
<PackageReference Include="Superpower" Version="2.3.0" />
|
<PackageReference Include="Polly" Version="7.2.3" />
|
||||||
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
|
<PackageReference Include="Superpower" Version="3.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,24 +1,24 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Exceptions
|
namespace DiscordChatExporter.Core.Exceptions;
|
||||||
|
|
||||||
|
public partial class DiscordChatExporterException : Exception
|
||||||
{
|
{
|
||||||
public partial class DiscordChatExporterException : Exception
|
public bool IsFatal { get; }
|
||||||
{
|
|
||||||
public bool IsCritical { get; }
|
|
||||||
|
|
||||||
public DiscordChatExporterException(string message, bool isCritical = false)
|
public DiscordChatExporterException(string message, bool isFatal = false)
|
||||||
: base(message)
|
: base(message)
|
||||||
{
|
{
|
||||||
IsCritical = isCritical;
|
IsFatal = isFatal;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public partial class DiscordChatExporterException
|
public partial class DiscordChatExporterException
|
||||||
|
{
|
||||||
|
internal static DiscordChatExporterException FailedHttpRequest(HttpResponseMessage response)
|
||||||
{
|
{
|
||||||
internal static DiscordChatExporterException FailedHttpRequest(HttpResponseMessage response)
|
var message = $@"
|
||||||
{
|
|
||||||
var message = $@"
|
|
||||||
Failed to perform an HTTP request.
|
Failed to perform an HTTP request.
|
||||||
|
|
||||||
[Request]
|
[Request]
|
||||||
@@ -27,19 +27,18 @@ Failed to perform an HTTP request.
|
|||||||
[Response]
|
[Response]
|
||||||
{response}";
|
{response}";
|
||||||
|
|
||||||
return new DiscordChatExporterException(message.Trim(), true);
|
return new DiscordChatExporterException(message.Trim(), true);
|
||||||
}
|
|
||||||
|
|
||||||
internal static DiscordChatExporterException Unauthorized() =>
|
|
||||||
new("Authentication token is invalid.");
|
|
||||||
|
|
||||||
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("No messages found for the specified period.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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("No messages found for the specified period.");
|
||||||
}
|
}
|
||||||
@@ -1,69 +1,81 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
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.Discord.Data;
|
||||||
using DiscordChatExporter.Core.Discord.Data.Common;
|
using DiscordChatExporter.Core.Discord.Data.Common;
|
||||||
using DiscordChatExporter.Core.Exceptions;
|
using DiscordChatExporter.Core.Exceptions;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
using Gress;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Exporting
|
namespace DiscordChatExporter.Core.Exporting;
|
||||||
|
|
||||||
|
public class ChannelExporter
|
||||||
{
|
{
|
||||||
public class ChannelExporter
|
private readonly DiscordClient _discord;
|
||||||
|
|
||||||
|
public ChannelExporter(DiscordClient discord) => _discord = discord;
|
||||||
|
|
||||||
|
public async ValueTask ExportChannelAsync(
|
||||||
|
ExportRequest request,
|
||||||
|
IProgress<Percentage>? progress = null,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
private readonly DiscordClient _discord;
|
// Build context
|
||||||
|
var contextMembers = new HashSet<Member>(IdBasedEqualityComparer.Instance);
|
||||||
|
var contextChannels = await _discord.GetGuildChannelsAsync(request.Guild.Id, cancellationToken);
|
||||||
|
var contextRoles = await _discord.GetGuildRolesAsync(request.Guild.Id, cancellationToken);
|
||||||
|
|
||||||
public ChannelExporter(DiscordClient discord) => _discord = discord;
|
var context = new ExportContext(
|
||||||
|
request,
|
||||||
|
contextMembers,
|
||||||
|
contextChannels,
|
||||||
|
contextRoles
|
||||||
|
);
|
||||||
|
|
||||||
public ChannelExporter(AuthToken token) : this(new DiscordClient(token)) {}
|
// Export messages
|
||||||
|
await using var messageExporter = new MessageExporter(context);
|
||||||
|
|
||||||
public async ValueTask ExportChannelAsync(ExportRequest request, IProgress<double>? progress = null)
|
var exportedAnything = false;
|
||||||
|
var encounteredUsers = new HashSet<User>(IdBasedEqualityComparer.Instance);
|
||||||
|
|
||||||
|
await foreach (var message in _discord.GetMessagesAsync(
|
||||||
|
request.Channel.Id,
|
||||||
|
request.After,
|
||||||
|
request.Before,
|
||||||
|
progress,
|
||||||
|
cancellationToken))
|
||||||
{
|
{
|
||||||
// Build context
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
var contextMembers = new HashSet<Member>(IdBasedEqualityComparer.Instance);
|
|
||||||
var contextChannels = await _discord.GetGuildChannelsAsync(request.Guild.Id);
|
|
||||||
var contextRoles = await _discord.GetGuildRolesAsync(request.Guild.Id);
|
|
||||||
|
|
||||||
var context = new ExportContext(
|
// Skips any messages that fail to pass the supplied filter
|
||||||
request,
|
if (!request.MessageFilter.IsMatch(message))
|
||||||
contextMembers,
|
continue;
|
||||||
contextChannels,
|
|
||||||
contextRoles
|
|
||||||
);
|
|
||||||
|
|
||||||
// Export messages
|
// Resolve members for referenced users
|
||||||
await using var messageExporter = new MessageExporter(context);
|
foreach (var referencedUser in message.MentionedUsers.Prepend(message.Author))
|
||||||
|
|
||||||
var exportedAnything = false;
|
|
||||||
var encounteredUsers = new HashSet<User>(IdBasedEqualityComparer.Instance);
|
|
||||||
await foreach (var message in _discord.GetMessagesAsync(request.Channel.Id, request.After, request.Before, progress))
|
|
||||||
{
|
{
|
||||||
// Skips any messages that fail to pass the supplied filter
|
if (!encounteredUsers.Add(referencedUser))
|
||||||
if (!request.MessageFilter.Filter(message))
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Resolve members for referenced users
|
var member = await _discord.GetGuildMemberAsync(
|
||||||
foreach (var referencedUser in message.MentionedUsers.Prepend(message.Author))
|
request.Guild.Id,
|
||||||
{
|
referencedUser,
|
||||||
if (!encounteredUsers.Add(referencedUser))
|
cancellationToken
|
||||||
continue;
|
);
|
||||||
|
|
||||||
var member =
|
contextMembers.Add(member);
|
||||||
await _discord.TryGetGuildMemberAsync(request.Guild.Id, referencedUser) ??
|
|
||||||
Member.CreateForUser(referencedUser);
|
|
||||||
|
|
||||||
contextMembers.Add(member);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Export message
|
|
||||||
await messageExporter.ExportMessageAsync(message);
|
|
||||||
exportedAnything = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Throw if no messages were exported
|
// Export message
|
||||||
if (!exportedAnything)
|
await messageExporter.ExportMessageAsync(message, cancellationToken);
|
||||||
throw DiscordChatExporterException.ChannelIsEmpty();
|
exportedAnything = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Throw if no messages were exported
|
||||||
|
if (!exportedAnything)
|
||||||
|
throw DiscordChatExporterException.ChannelIsEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,101 +4,102 @@ using System.Drawing;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
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.Discord.Data;
|
||||||
using DiscordChatExporter.Core.Utils.Extensions;
|
using DiscordChatExporter.Core.Utils.Extensions;
|
||||||
using Tyrrrz.Extensions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Exporting
|
namespace DiscordChatExporter.Core.Exporting;
|
||||||
|
|
||||||
|
internal class ExportContext
|
||||||
{
|
{
|
||||||
internal class ExportContext
|
private readonly MediaDownloader _mediaDownloader;
|
||||||
|
|
||||||
|
public ExportRequest Request { get; }
|
||||||
|
|
||||||
|
public IReadOnlyCollection<Member> Members { get; }
|
||||||
|
|
||||||
|
public IReadOnlyCollection<Channel> Channels { get; }
|
||||||
|
|
||||||
|
public IReadOnlyCollection<Role> Roles { get; }
|
||||||
|
|
||||||
|
public ExportContext(
|
||||||
|
ExportRequest request,
|
||||||
|
IReadOnlyCollection<Member> members,
|
||||||
|
IReadOnlyCollection<Channel> channels,
|
||||||
|
IReadOnlyCollection<Role> roles)
|
||||||
{
|
{
|
||||||
private readonly MediaDownloader _mediaDownloader;
|
Request = request;
|
||||||
|
Members = members;
|
||||||
|
Channels = channels;
|
||||||
|
Roles = roles;
|
||||||
|
|
||||||
public ExportRequest Request { get; }
|
_mediaDownloader = new MediaDownloader(request.OutputMediaDirPath, request.ShouldReuseMedia);
|
||||||
|
}
|
||||||
|
|
||||||
public IReadOnlyCollection<Member> Members { get; }
|
public string FormatDate(DateTimeOffset date) => Request.DateFormat switch
|
||||||
|
{
|
||||||
|
"unix" => date.ToUnixTimeSeconds().ToString(),
|
||||||
|
"unixms" => date.ToUnixTimeMilliseconds().ToString(),
|
||||||
|
var dateFormat => date.ToLocalString(dateFormat)
|
||||||
|
};
|
||||||
|
|
||||||
public IReadOnlyCollection<Channel> Channels { get; }
|
public Member? TryGetMember(Snowflake id) => Members.FirstOrDefault(m => m.Id == id);
|
||||||
|
|
||||||
public IReadOnlyCollection<Role> Roles { get; }
|
public Channel? TryGetChannel(Snowflake id) => Channels.FirstOrDefault(c => c.Id == id);
|
||||||
|
|
||||||
public ExportContext(
|
public Role? TryGetRole(Snowflake id) => Roles.FirstOrDefault(r => r.Id == id);
|
||||||
ExportRequest request,
|
|
||||||
IReadOnlyCollection<Member> members,
|
public Color? TryGetUserColor(Snowflake id)
|
||||||
IReadOnlyCollection<Channel> channels,
|
{
|
||||||
IReadOnlyCollection<Role> roles)
|
var member = TryGetMember(id);
|
||||||
|
var roles = member?.RoleIds.Join(Roles, i => i, r => r.Id, (_, role) => role);
|
||||||
|
|
||||||
|
return roles?
|
||||||
|
.Where(r => r.Color is not null)
|
||||||
|
.OrderByDescending(r => r.Position)
|
||||||
|
.Select(r => r.Color)
|
||||||
|
.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<string> ResolveMediaUrlAsync(string url, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (!Request.ShouldDownloadMedia)
|
||||||
|
return url;
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Request = request;
|
var filePath = await _mediaDownloader.DownloadAsync(url, cancellationToken);
|
||||||
Members = members;
|
|
||||||
Channels = channels;
|
|
||||||
Roles = roles;
|
|
||||||
|
|
||||||
_mediaDownloader = new MediaDownloader(request.OutputMediaDirPath, request.ShouldReuseMedia);
|
// We want relative path so that the output files can be copied around without breaking.
|
||||||
}
|
// Base directory path may be null if the file is stored at the root or relative to working directory.
|
||||||
|
var relativeFilePath = !string.IsNullOrWhiteSpace(Request.OutputBaseDirPath)
|
||||||
|
? Path.GetRelativePath(Request.OutputBaseDirPath, filePath)
|
||||||
|
: filePath;
|
||||||
|
|
||||||
public string FormatDate(DateTimeOffset date) => Request.DateFormat switch
|
// HACK: for HTML, we need to format the URL properly
|
||||||
{
|
if (Request.Format is ExportFormat.HtmlDark or ExportFormat.HtmlLight)
|
||||||
"unix" => date.ToUnixTimeSeconds().ToString(),
|
|
||||||
"unixms" => date.ToUnixTimeMilliseconds().ToString(),
|
|
||||||
var dateFormat => date.ToLocalString(dateFormat)
|
|
||||||
};
|
|
||||||
|
|
||||||
public Member? TryGetMember(Snowflake id) => Members.FirstOrDefault(m => m.Id == id);
|
|
||||||
|
|
||||||
public Channel? TryGetChannel(Snowflake id) => Channels.FirstOrDefault(c => c.Id == id);
|
|
||||||
|
|
||||||
public Role? TryGetRole(Snowflake id) => Roles.FirstOrDefault(r => r.Id == id);
|
|
||||||
|
|
||||||
public Color? TryGetUserColor(Snowflake id)
|
|
||||||
{
|
|
||||||
var member = TryGetMember(id);
|
|
||||||
var roles = member?.RoleIds.Join(Roles, i => i, r => r.Id, (_, role) => role);
|
|
||||||
|
|
||||||
return roles?
|
|
||||||
.Where(r => r.Color is not null)
|
|
||||||
.OrderByDescending(r => r.Position)
|
|
||||||
.Select(r => r.Color)
|
|
||||||
.FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async ValueTask<string> ResolveMediaUrlAsync(string url)
|
|
||||||
{
|
|
||||||
if (!Request.ShouldDownloadMedia)
|
|
||||||
return url;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var filePath = await _mediaDownloader.DownloadAsync(url);
|
// Need to escape each path segment while keeping the directory separators intact
|
||||||
|
return string.Join(
|
||||||
// We want relative path so that the output files can be copied around without breaking.
|
Path.AltDirectorySeparatorChar,
|
||||||
// Base directory path may be null if the file is stored at the root or relative to working directory.
|
relativeFilePath
|
||||||
var relativeFilePath = !string.IsNullOrWhiteSpace(Request.OutputBaseDirPath)
|
|
||||||
? Path.GetRelativePath(Request.OutputBaseDirPath, filePath)
|
|
||||||
: filePath;
|
|
||||||
|
|
||||||
// HACK: for HTML, we need to format the URL properly
|
|
||||||
if (Request.Format is ExportFormat.HtmlDark or ExportFormat.HtmlLight)
|
|
||||||
{
|
|
||||||
// Need to escape each path segment while keeping the directory separators intact
|
|
||||||
return relativeFilePath
|
|
||||||
.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
|
.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
|
||||||
.Select(Uri.EscapeDataString)
|
.Select(Uri.EscapeDataString)
|
||||||
.JoinToString(Path.AltDirectorySeparatorChar.ToString());
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return relativeFilePath;
|
return relativeFilePath;
|
||||||
}
|
}
|
||||||
// Try to catch only exceptions related to failed HTTP requests
|
// Try to catch only exceptions related to failed HTTP requests
|
||||||
// https://github.com/Tyrrrz/DiscordChatExporter/issues/332
|
// https://github.com/Tyrrrz/DiscordChatExporter/issues/332
|
||||||
// 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
|
// 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
|
||||||
return url;
|
return url;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,36 +1,35 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Exporting
|
namespace DiscordChatExporter.Core.Exporting;
|
||||||
|
|
||||||
|
public enum ExportFormat
|
||||||
{
|
{
|
||||||
public enum ExportFormat
|
PlainText,
|
||||||
{
|
HtmlDark,
|
||||||
PlainText,
|
HtmlLight,
|
||||||
HtmlDark,
|
Csv,
|
||||||
HtmlLight,
|
Json
|
||||||
Csv,
|
}
|
||||||
Json
|
|
||||||
}
|
|
||||||
|
|
||||||
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.PlainText => "TXT",
|
||||||
ExportFormat.HtmlDark => "HTML (Dark)",
|
ExportFormat.HtmlDark => "HTML (Dark)",
|
||||||
ExportFormat.HtmlLight => "HTML (Light)",
|
ExportFormat.HtmlLight => "HTML (Light)",
|
||||||
ExportFormat.Csv => "CSV",
|
ExportFormat.Csv => "CSV",
|
||||||
ExportFormat.Json => "JSON",
|
ExportFormat.Json => "JSON",
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(format))
|
_ => throw new ArgumentOutOfRangeException(nameof(format))
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.IO;
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using DiscordChatExporter.Core.Discord;
|
using DiscordChatExporter.Core.Discord;
|
||||||
@@ -8,161 +7,115 @@ using DiscordChatExporter.Core.Exporting.Filtering;
|
|||||||
using DiscordChatExporter.Core.Exporting.Partitioning;
|
using DiscordChatExporter.Core.Exporting.Partitioning;
|
||||||
using DiscordChatExporter.Core.Utils;
|
using DiscordChatExporter.Core.Utils;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Exporting
|
namespace DiscordChatExporter.Core.Exporting;
|
||||||
|
|
||||||
|
public partial record ExportRequest(
|
||||||
|
Guild Guild,
|
||||||
|
Channel Channel,
|
||||||
|
string OutputPath,
|
||||||
|
ExportFormat Format,
|
||||||
|
Snowflake? After,
|
||||||
|
Snowflake? Before,
|
||||||
|
PartitionLimit PartitionLimit,
|
||||||
|
MessageFilter MessageFilter,
|
||||||
|
bool ShouldDownloadMedia,
|
||||||
|
bool ShouldReuseMedia,
|
||||||
|
string DateFormat)
|
||||||
{
|
{
|
||||||
public partial class ExportRequest
|
private string? _outputBaseFilePath;
|
||||||
|
public string OutputBaseFilePath => _outputBaseFilePath ??= GetOutputBaseFilePath(
|
||||||
|
Guild,
|
||||||
|
Channel,
|
||||||
|
OutputPath,
|
||||||
|
Format,
|
||||||
|
After,
|
||||||
|
Before
|
||||||
|
);
|
||||||
|
|
||||||
|
public string OutputBaseDirPath => Path.GetDirectoryName(OutputBaseFilePath) ?? OutputPath;
|
||||||
|
|
||||||
|
public string OutputMediaDirPath => $"{OutputBaseFilePath}_Files{Path.DirectorySeparatorChar}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial record ExportRequest
|
||||||
|
{
|
||||||
|
private static string GetOutputBaseFilePath(
|
||||||
|
Guild guild,
|
||||||
|
Channel channel,
|
||||||
|
string outputPath,
|
||||||
|
ExportFormat format,
|
||||||
|
Snowflake? after = null,
|
||||||
|
Snowflake? before = null)
|
||||||
{
|
{
|
||||||
public Guild Guild { get; }
|
// Formats path
|
||||||
|
outputPath = Regex.Replace(outputPath, "%.", m =>
|
||||||
|
PathEx.EscapeFileName(m.Value switch
|
||||||
|
{
|
||||||
|
"%g" => guild.Id.ToString(),
|
||||||
|
"%G" => guild.Name,
|
||||||
|
"%t" => channel.Category.Id.ToString(),
|
||||||
|
"%T" => channel.Category.Name,
|
||||||
|
"%c" => channel.Id.ToString(),
|
||||||
|
"%C" => channel.Name,
|
||||||
|
"%p" => channel.Position?.ToString() ?? "0",
|
||||||
|
"%P" => channel.Category.Position?.ToString() ?? "0",
|
||||||
|
"%a" => after?.ToDate().ToString("yyyy-MM-dd") ?? "",
|
||||||
|
"%b" => before?.ToDate().ToString("yyyy-MM-dd") ?? "",
|
||||||
|
"%%" => "%",
|
||||||
|
_ => m.Value
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
public Channel Channel { get; }
|
// Output is a directory
|
||||||
|
if (Directory.Exists(outputPath) || string.IsNullOrWhiteSpace(Path.GetExtension(outputPath)))
|
||||||
public string OutputPath { get; }
|
|
||||||
|
|
||||||
public string OutputBaseFilePath { get; }
|
|
||||||
|
|
||||||
public string OutputBaseDirPath { get; }
|
|
||||||
|
|
||||||
public string OutputMediaDirPath { get; }
|
|
||||||
|
|
||||||
public ExportFormat Format { get; }
|
|
||||||
|
|
||||||
public Snowflake? After { get; }
|
|
||||||
|
|
||||||
public Snowflake? Before { get; }
|
|
||||||
|
|
||||||
public PartitionLimit PartitionLimit { get; }
|
|
||||||
|
|
||||||
public MessageFilter MessageFilter { get; }
|
|
||||||
|
|
||||||
public bool ShouldDownloadMedia { get; }
|
|
||||||
|
|
||||||
public bool ShouldReuseMedia { get; }
|
|
||||||
|
|
||||||
public string DateFormat { get; }
|
|
||||||
|
|
||||||
public ExportRequest(
|
|
||||||
Guild guild,
|
|
||||||
Channel channel,
|
|
||||||
string outputPath,
|
|
||||||
ExportFormat format,
|
|
||||||
Snowflake? after,
|
|
||||||
Snowflake? before,
|
|
||||||
PartitionLimit partitionLimit,
|
|
||||||
MessageFilter messageFilter,
|
|
||||||
bool shouldDownloadMedia,
|
|
||||||
bool shouldReuseMedia,
|
|
||||||
string dateFormat)
|
|
||||||
{
|
{
|
||||||
Guild = guild;
|
var fileName = GetDefaultOutputFileName(guild, channel, format, after, before);
|
||||||
Channel = channel;
|
return Path.Combine(outputPath, fileName);
|
||||||
OutputPath = outputPath;
|
|
||||||
Format = format;
|
|
||||||
After = after;
|
|
||||||
Before = before;
|
|
||||||
PartitionLimit = partitionLimit;
|
|
||||||
MessageFilter = messageFilter;
|
|
||||||
ShouldDownloadMedia = shouldDownloadMedia;
|
|
||||||
ShouldReuseMedia = shouldReuseMedia;
|
|
||||||
DateFormat = dateFormat;
|
|
||||||
|
|
||||||
OutputBaseFilePath = GetOutputBaseFilePath(
|
|
||||||
guild,
|
|
||||||
channel,
|
|
||||||
outputPath,
|
|
||||||
format,
|
|
||||||
after,
|
|
||||||
before
|
|
||||||
);
|
|
||||||
|
|
||||||
OutputBaseDirPath = Path.GetDirectoryName(OutputBaseFilePath) ?? OutputPath;
|
|
||||||
OutputMediaDirPath = $"{OutputBaseFilePath}_Files{Path.DirectorySeparatorChar}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Output is a file
|
||||||
|
return outputPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class ExportRequest
|
public static string GetDefaultOutputFileName(
|
||||||
|
Guild guild,
|
||||||
|
Channel channel,
|
||||||
|
ExportFormat format,
|
||||||
|
Snowflake? after = null,
|
||||||
|
Snowflake? before = null)
|
||||||
{
|
{
|
||||||
private static string GetOutputBaseFilePath(
|
var buffer = new StringBuilder();
|
||||||
Guild guild,
|
|
||||||
Channel channel,
|
// Guild and channel names
|
||||||
string outputPath,
|
buffer.Append($"{guild.Name} - {channel.Category.Name} - {channel.Name} [{channel.Id}]");
|
||||||
ExportFormat format,
|
|
||||||
Snowflake? after = null,
|
// Date range
|
||||||
Snowflake? before = null)
|
if (after is not null || before is not null)
|
||||||
{
|
{
|
||||||
|
buffer.Append(' ').Append('(');
|
||||||
|
|
||||||
// Formats path
|
// Both 'after' and 'before' are set
|
||||||
outputPath = Regex.Replace(outputPath, "%.", m =>
|
if (after is not null && before is not null)
|
||||||
PathEx.EscapePath(m.Value switch
|
|
||||||
{
|
|
||||||
"%g" => guild.Id.ToString(),
|
|
||||||
"%G" => guild.Name,
|
|
||||||
"%t" => channel.Category.Id.ToString(),
|
|
||||||
"%T" => channel.Category.Name,
|
|
||||||
"%c" => channel.Id.ToString(),
|
|
||||||
"%C" => channel.Name,
|
|
||||||
"%p" => channel.Position?.ToString() ?? "0",
|
|
||||||
"%P" => channel.Category.Position?.ToString() ?? "0",
|
|
||||||
"%a" => (after ?? Snowflake.Zero).ToDate().ToString("yyyy-MM-dd"),
|
|
||||||
"%b" => (before?.ToDate() ?? DateTime.Now).ToString("yyyy-MM-dd"),
|
|
||||||
"%%" => "%",
|
|
||||||
_ => m.Value
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
// Output is a directory
|
|
||||||
if (Directory.Exists(outputPath) || string.IsNullOrWhiteSpace(Path.GetExtension(outputPath)))
|
|
||||||
{
|
{
|
||||||
var fileName = GetDefaultOutputFileName(guild, channel, format, after, before);
|
buffer.Append($"{after.Value.ToDate():yyyy-MM-dd} to {before.Value.ToDate():yyyy-MM-dd}");
|
||||||
return Path.Combine(outputPath, fileName);
|
}
|
||||||
|
// Only 'after' is set
|
||||||
|
else if (after is not null)
|
||||||
|
{
|
||||||
|
buffer.Append($"after {after.Value.ToDate():yyyy-MM-dd}");
|
||||||
|
}
|
||||||
|
// Only 'before' is set
|
||||||
|
else if (before is not null)
|
||||||
|
{
|
||||||
|
buffer.Append($"before {before.Value.ToDate():yyyy-MM-dd}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output is a file
|
buffer.Append(')');
|
||||||
return outputPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetDefaultOutputFileName(
|
// File extension
|
||||||
Guild guild,
|
buffer.Append('.').Append(format.GetFileExtension());
|
||||||
Channel channel,
|
|
||||||
ExportFormat format,
|
|
||||||
Snowflake? after = null,
|
|
||||||
Snowflake? before = null)
|
|
||||||
{
|
|
||||||
var buffer = new StringBuilder();
|
|
||||||
|
|
||||||
// Guild and channel names
|
return PathEx.EscapeFileName(buffer.ToString());
|
||||||
buffer.Append($"{guild.Name} - {channel.Category.Name} - {channel.Name} [{channel.Id}]");
|
|
||||||
|
|
||||||
// Date range
|
|
||||||
if (after is not null || before is not null)
|
|
||||||
{
|
|
||||||
buffer.Append(" (");
|
|
||||||
|
|
||||||
// Both 'after' and 'before' are set
|
|
||||||
if (after is not null && before is not null)
|
|
||||||
{
|
|
||||||
buffer.Append($"{after?.ToDate():yyyy-MM-dd} to {before?.ToDate():yyyy-MM-dd}");
|
|
||||||
}
|
|
||||||
// Only 'after' is set
|
|
||||||
else if (after is not null)
|
|
||||||
{
|
|
||||||
buffer.Append($"after {after?.ToDate():yyyy-MM-dd}");
|
|
||||||
}
|
|
||||||
// Only 'before' is set
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buffer.Append($"before {before?.ToDate():yyyy-MM-dd}");
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer.Append(")");
|
|
||||||
}
|
|
||||||
|
|
||||||
// File extension
|
|
||||||
buffer.Append($".{format.GetFileExtension()}");
|
|
||||||
|
|
||||||
// Replace invalid chars
|
|
||||||
PathEx.EscapePath(buffer);
|
|
||||||
|
|
||||||
return buffer.ToString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
namespace DiscordChatExporter.Core.Exporting.Filtering
|
namespace DiscordChatExporter.Core.Exporting.Filtering;
|
||||||
|
|
||||||
|
internal enum BinaryExpressionKind
|
||||||
{
|
{
|
||||||
internal enum BinaryExpressionKind
|
Or,
|
||||||
{
|
And
|
||||||
Or,
|
|
||||||
And
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,26 +1,25 @@
|
|||||||
using System;
|
using System;
|
||||||
using DiscordChatExporter.Core.Discord.Data;
|
using DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Exporting.Filtering
|
namespace DiscordChatExporter.Core.Exporting.Filtering;
|
||||||
|
|
||||||
|
internal class BinaryExpressionMessageFilter : MessageFilter
|
||||||
{
|
{
|
||||||
internal class BinaryExpressionMessageFilter : MessageFilter
|
private readonly MessageFilter _first;
|
||||||
|
private readonly MessageFilter _second;
|
||||||
|
private readonly BinaryExpressionKind _kind;
|
||||||
|
|
||||||
|
public BinaryExpressionMessageFilter(MessageFilter first, MessageFilter second, BinaryExpressionKind kind)
|
||||||
{
|
{
|
||||||
private readonly MessageFilter _first;
|
_first = first;
|
||||||
private readonly MessageFilter _second;
|
_second = second;
|
||||||
private readonly BinaryExpressionKind _kind;
|
_kind = kind;
|
||||||
|
|
||||||
public BinaryExpressionMessageFilter(MessageFilter first, MessageFilter second, BinaryExpressionKind kind)
|
|
||||||
{
|
|
||||||
_first = first;
|
|
||||||
_second = second;
|
|
||||||
_kind = kind;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool Filter(Message message) => _kind switch
|
|
||||||
{
|
|
||||||
BinaryExpressionKind.Or => _first.Filter(message) || _second.Filter(message),
|
|
||||||
BinaryExpressionKind.And => _first.Filter(message) && _second.Filter(message),
|
|
||||||
_ => throw new InvalidOperationException($"Unknown binary expression kind '{_kind}'.")
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool IsMatch(Message message) => _kind switch
|
||||||
|
{
|
||||||
|
BinaryExpressionKind.Or => _first.IsMatch(message) || _second.IsMatch(message),
|
||||||
|
BinaryExpressionKind.And => _first.IsMatch(message) && _second.IsMatch(message),
|
||||||
|
_ => throw new InvalidOperationException($"Unknown binary expression kind '{_kind}'.")
|
||||||
|
};
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,33 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using DiscordChatExporter.Core.Discord.Data;
|
using DiscordChatExporter.Core.Discord.Data;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Exporting.Filtering
|
namespace DiscordChatExporter.Core.Exporting.Filtering;
|
||||||
|
|
||||||
|
internal class ContainsMessageFilter : MessageFilter
|
||||||
{
|
{
|
||||||
internal class ContainsMessageFilter : MessageFilter
|
private readonly string _text;
|
||||||
{
|
|
||||||
private readonly string _text;
|
|
||||||
|
|
||||||
public ContainsMessageFilter(string text) => _text = text;
|
public ContainsMessageFilter(string text) => _text = text;
|
||||||
|
|
||||||
public override bool Filter(Message message) => Regex.IsMatch(
|
private bool IsMatch(string? content) =>
|
||||||
message.Content,
|
!string.IsNullOrWhiteSpace(content) &&
|
||||||
"\\b" + _text + "\\b",
|
Regex.IsMatch(
|
||||||
|
content,
|
||||||
|
"\\b" + Regex.Escape(_text) + "\\b",
|
||||||
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
|
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
public override bool IsMatch(Message message) =>
|
||||||
|
IsMatch(message.Content) ||
|
||||||
|
message.Embeds.Any(e =>
|
||||||
|
IsMatch(e.Title) ||
|
||||||
|
IsMatch(e.Author?.Name) ||
|
||||||
|
IsMatch(e.Description) ||
|
||||||
|
IsMatch(e.Footer?.Text) ||
|
||||||
|
e.Fields.Any(f =>
|
||||||
|
IsMatch(f.Name) ||
|
||||||
|
IsMatch(f.Value)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user