mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-04-20 21:18:23 +00:00
Refactor
This commit is contained in:
@@ -11,23 +11,37 @@ public partial record Channel(
|
||||
Snowflake Id,
|
||||
ChannelKind Kind,
|
||||
Snowflake GuildId,
|
||||
Snowflake? ParentId,
|
||||
string? ParentName,
|
||||
int? ParentPosition,
|
||||
ChannelCategory Category,
|
||||
Channel? Parent,
|
||||
string Name,
|
||||
int? Position,
|
||||
string? IconUrl,
|
||||
string? Topic,
|
||||
Snowflake? LastMessageId) : IChannel
|
||||
bool IsActive,
|
||||
Snowflake? LastMessageId)
|
||||
{
|
||||
// Used for visual backwards-compatibility with old exports, where
|
||||
// channels without a parent (i.e. mostly DM channels) had a fallback
|
||||
// category created for them.
|
||||
public string Category => Parent?.Name ?? Kind switch
|
||||
{
|
||||
ChannelKind.GuildCategory => "Category",
|
||||
ChannelKind.GuildTextChat => "Text",
|
||||
ChannelKind.DirectTextChat => "Private",
|
||||
ChannelKind.DirectGroupTextChat => "Group",
|
||||
ChannelKind.GuildPrivateThread => "Private Thread",
|
||||
ChannelKind.GuildPublicThread => "Public Thread",
|
||||
ChannelKind.GuildNews => "News",
|
||||
ChannelKind.GuildNewsThread => "News Thread",
|
||||
_ => "Default"
|
||||
};
|
||||
|
||||
// Only needed for WPF data binding. Don't use anywhere else.
|
||||
public bool IsVoice => Kind.IsVoice();
|
||||
}
|
||||
|
||||
public partial record Channel
|
||||
{
|
||||
public static Channel Parse(JsonElement json, ChannelCategory? categoryHint = null, int? positionHint = null, string? parentName = null, int? parentPosition = null)
|
||||
public static Channel Parse(JsonElement json, Channel? parent = null, int? positionHint = null)
|
||||
{
|
||||
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||
var kind = (ChannelKind)json.GetProperty("type").GetInt32();
|
||||
@@ -36,10 +50,6 @@ public partial record Channel
|
||||
json.GetPropertyOrNull("guild_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse) ??
|
||||
Guild.DirectMessages.Id;
|
||||
|
||||
var parentId = json.GetPropertyOrNull("parent_id")?.GetNonWhiteSpaceStringOrNull()?.Pipe(Snowflake.Parse);
|
||||
|
||||
var category = categoryHint ?? ChannelCategory.CreateDefault(kind);
|
||||
|
||||
var name =
|
||||
// Guild channel
|
||||
json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull() ??
|
||||
@@ -66,6 +76,11 @@ public partial record Channel
|
||||
|
||||
var topic = json.GetPropertyOrNull("topic")?.GetStringOrNull();
|
||||
|
||||
var isActive = !json
|
||||
.GetPropertyOrNull("thread_metadata")?
|
||||
.GetPropertyOrNull("archived")?
|
||||
.GetBooleanOrNull() ?? true;
|
||||
|
||||
var lastMessageId = json
|
||||
.GetPropertyOrNull("last_message_id")?
|
||||
.GetNonWhiteSpaceStringOrNull()?
|
||||
@@ -75,14 +90,12 @@ public partial record Channel
|
||||
id,
|
||||
kind,
|
||||
guildId,
|
||||
parentId,
|
||||
parentName,
|
||||
parentPosition,
|
||||
category,
|
||||
parent,
|
||||
name,
|
||||
position,
|
||||
iconUrl,
|
||||
topic,
|
||||
isActive,
|
||||
lastMessageId
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using DiscordChatExporter.Core.Discord.Data.Common;
|
||||
using DiscordChatExporter.Core.Utils.Extensions;
|
||||
using JsonExtensions.Reading;
|
||||
|
||||
namespace DiscordChatExporter.Core.Discord.Data;
|
||||
|
||||
public record ChannelCategory(Snowflake Id, string Name, int? Position) : IHasId
|
||||
{
|
||||
public static ChannelCategory CreateDefault(ChannelKind channelKind) => new(
|
||||
Snowflake.Zero,
|
||||
channelKind switch
|
||||
{
|
||||
ChannelKind.GuildTextChat => "Text",
|
||||
ChannelKind.DirectTextChat => "Private",
|
||||
ChannelKind.DirectGroupTextChat => "Group",
|
||||
ChannelKind.GuildNews => "News",
|
||||
_ => "Default"
|
||||
},
|
||||
null
|
||||
);
|
||||
|
||||
public static ChannelCategory Parse(JsonElement json, int? positionHint = null)
|
||||
{
|
||||
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||
|
||||
var name =
|
||||
json.GetPropertyOrNull("name")?.GetNonWhiteSpaceStringOrNull() ??
|
||||
id.ToString();
|
||||
|
||||
var position =
|
||||
positionHint ??
|
||||
json.GetPropertyOrNull("position")?.GetInt32OrNull();
|
||||
|
||||
return new ChannelCategory(id, name, position);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using DiscordChatExporter.Core.Discord.Data.Common;
|
||||
using DiscordChatExporter.Core.Utils.Extensions;
|
||||
using JsonExtensions.Reading;
|
||||
|
||||
namespace DiscordChatExporter.Core.Discord.Data;
|
||||
|
||||
// https://discord.com/developers/docs/resources/channel#channel-object-example-thread-channel
|
||||
public record ChannelThread(
|
||||
Snowflake Id,
|
||||
ChannelKind Kind,
|
||||
Snowflake GuildId,
|
||||
Snowflake? ParentId,
|
||||
string? ParentName,
|
||||
string Name,
|
||||
bool IsActive,
|
||||
Snowflake? LastMessageId) : IChannel
|
||||
{
|
||||
public int? ParentPosition => null;
|
||||
public int? Position => null;
|
||||
public string? IconUrl => null;
|
||||
public string? Topic => null;
|
||||
|
||||
public static ChannelThread Parse(JsonElement json, string parentName)
|
||||
{
|
||||
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||
var kind = (ChannelKind)json.GetProperty("type").GetInt32();
|
||||
var guildId = json.GetProperty("guild_id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||
var parentId = json.GetProperty("parent_id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||
var name = json.GetProperty("name").GetNonWhiteSpaceString();
|
||||
|
||||
var isActive = !json
|
||||
.GetPropertyOrNull("thread_metadata")?
|
||||
.GetPropertyOrNull("archived")?
|
||||
.GetBooleanOrNull() ?? true;
|
||||
|
||||
var lastMessageId = json
|
||||
.GetPropertyOrNull("last_message_id")?
|
||||
.GetNonWhiteSpaceStringOrNull()?
|
||||
.Pipe(Snowflake.Parse);
|
||||
|
||||
return new ChannelThread(
|
||||
id,
|
||||
kind,
|
||||
guildId,
|
||||
parentId,
|
||||
parentName,
|
||||
name,
|
||||
isActive,
|
||||
lastMessageId
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
namespace DiscordChatExporter.Core.Discord.Data.Common;
|
||||
|
||||
public interface IChannel : IHasId
|
||||
{
|
||||
ChannelKind Kind { get; }
|
||||
Snowflake GuildId { get; }
|
||||
Snowflake? ParentId { get; }
|
||||
string? ParentName { get; }
|
||||
int? ParentPosition { get; }
|
||||
string Name { get; }
|
||||
int? Position { get; }
|
||||
string? IconUrl { get; }
|
||||
string? Topic { get; }
|
||||
Snowflake? LastMessageId { get; }
|
||||
}
|
||||
@@ -20,7 +20,8 @@ public partial record Member(
|
||||
|
||||
public partial record Member
|
||||
{
|
||||
public static Member CreateDefault(User user) => new(user, null, null, Array.Empty<Snowflake>());
|
||||
public static Member CreateFallback(User user) =>
|
||||
new(user, null, null, Array.Empty<Snowflake>());
|
||||
|
||||
public static Member Parse(JsonElement json, Snowflake? guildId = null)
|
||||
{
|
||||
|
||||
@@ -215,14 +215,14 @@ public class DiscordClient
|
||||
|
||||
var channelsJson = response
|
||||
.EnumerateArray()
|
||||
.OrderBy(j => j.GetProperty("position").GetInt32())
|
||||
.OrderBy(c => c.GetProperty("position").GetInt32())
|
||||
.ThenBy(j => j.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse))
|
||||
.ToArray();
|
||||
|
||||
var categories = channelsJson
|
||||
var parentsById = channelsJson
|
||||
.Where(j => j.GetProperty("type").GetInt32() == (int)ChannelKind.GuildCategory)
|
||||
.Select((j, index) => ChannelCategory.Parse(j, index + 1))
|
||||
.ToDictionary(j => j.Id.ToString(), StringComparer.Ordinal);
|
||||
.Select((j, i) => Channel.Parse(j, null, i + 1))
|
||||
.ToDictionary(j => j.Id);
|
||||
|
||||
// Discord channel positions are relative, so we need to normalize them
|
||||
// so that the user may refer to them more easily in file name templates.
|
||||
@@ -230,21 +230,19 @@ public class DiscordClient
|
||||
|
||||
foreach (var channelJson in channelsJson)
|
||||
{
|
||||
var parentId = channelJson
|
||||
var parent = channelJson
|
||||
.GetPropertyOrNull("parent_id")?
|
||||
.GetNonWhiteSpaceStringOrNull();
|
||||
.GetNonWhiteSpaceStringOrNull()?
|
||||
.Pipe(Snowflake.Parse)
|
||||
.Pipe(parentsById.GetValueOrDefault);
|
||||
|
||||
var category = !string.IsNullOrWhiteSpace(parentId)
|
||||
? categories.GetValueOrDefault(parentId)
|
||||
: null;
|
||||
|
||||
yield return Channel.Parse(channelJson, category, position, category?.Name, category?.Position);
|
||||
yield return Channel.Parse(channelJson, parent, position);
|
||||
position++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<ChannelThread> GetGuildThreadsAsync(
|
||||
public async IAsyncEnumerable<Channel> GetGuildThreadsAsync(
|
||||
Snowflake guildId,
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -264,13 +262,14 @@ public class DiscordClient
|
||||
.SetQueryParameter("offset", currentOffset.ToString())
|
||||
.Build();
|
||||
|
||||
// Can be null on channels that the user cannot access
|
||||
var response = await TryGetJsonResponseAsync(url, cancellationToken);
|
||||
if (response is null)
|
||||
break;
|
||||
|
||||
foreach (var threadJson in response.Value.GetProperty("threads").EnumerateArray())
|
||||
{
|
||||
yield return ChannelThread.Parse(threadJson, channel.Name);
|
||||
yield return Channel.Parse(threadJson, channel);
|
||||
currentOffset++;
|
||||
}
|
||||
|
||||
@@ -284,12 +283,18 @@ public class DiscordClient
|
||||
{
|
||||
// Active threads
|
||||
{
|
||||
var parentsById = channels.ToDictionary(c => c.Id);
|
||||
|
||||
var response = await GetJsonResponseAsync($"guilds/{guildId}/threads/active", cancellationToken);
|
||||
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
|
||||
{
|
||||
var parentId = threadJson.GetProperty("parent_id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
|
||||
var parentChannel = channels.First(t => t.Id == parentId);
|
||||
yield return ChannelThread.Parse(threadJson, parentChannel.Name);
|
||||
var parent = threadJson
|
||||
.GetPropertyOrNull("parent_id")?
|
||||
.GetNonWhiteSpaceStringOrNull()?
|
||||
.Pipe(Snowflake.Parse)
|
||||
.Pipe(parentsById.GetValueOrDefault);
|
||||
|
||||
yield return Channel.Parse(threadJson, parent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,7 +308,7 @@ public class DiscordClient
|
||||
);
|
||||
|
||||
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
|
||||
yield return ChannelThread.Parse(threadJson, channel.Name);
|
||||
yield return Channel.Parse(threadJson, channel);
|
||||
}
|
||||
|
||||
// Private archived threads
|
||||
@@ -314,7 +319,7 @@ public class DiscordClient
|
||||
);
|
||||
|
||||
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
|
||||
yield return ChannelThread.Parse(threadJson, channel.Name);
|
||||
yield return Channel.Parse(threadJson, channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -352,41 +357,33 @@ public class DiscordClient
|
||||
return response?.Pipe(Invite.Parse);
|
||||
}
|
||||
|
||||
public async ValueTask<ChannelCategory> GetChannelCategoryAsync(
|
||||
public async ValueTask<Channel?> TryGetChannelAsync(
|
||||
Snowflake channelId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await GetJsonResponseAsync($"channels/{channelId}", cancellationToken);
|
||||
return ChannelCategory.Parse(response);
|
||||
}
|
||||
// In some cases, Discord API returns an empty body when requesting a channel.
|
||||
// Use an empty channel category as fallback for these cases.
|
||||
catch (DiscordChatExporterException)
|
||||
{
|
||||
return new ChannelCategory(channelId, "Unknown Category", 0);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Channel> GetChannelAsync(
|
||||
Snowflake channelId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var response = await GetJsonResponseAsync($"channels/{channelId}", cancellationToken);
|
||||
var response = await TryGetJsonResponseAsync($"channels/{channelId}", cancellationToken);
|
||||
if (response is null)
|
||||
return null;
|
||||
|
||||
var parentId = response
|
||||
.Value
|
||||
.GetPropertyOrNull("parent_id")?
|
||||
.GetNonWhiteSpaceStringOrNull()?
|
||||
.Pipe(Snowflake.Parse);
|
||||
|
||||
var category = parentId is not null
|
||||
? await GetChannelCategoryAsync(parentId.Value, cancellationToken)
|
||||
var parent = parentId is not null
|
||||
? await TryGetChannelAsync(parentId.Value, cancellationToken)
|
||||
: null;
|
||||
|
||||
return Channel.Parse(response, category, parentName: category?.Name, parentPosition: category?.Position);
|
||||
return Channel.Parse(response.Value, parent);
|
||||
}
|
||||
|
||||
public async ValueTask<Channel> GetChannelAsync(
|
||||
Snowflake channelId,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
await TryGetChannelAsync(channelId, cancellationToken) ??
|
||||
throw new InvalidOperationException($"Channel {channelId} not found.");
|
||||
|
||||
private async ValueTask<Message?> TryGetLastMessageAsync(
|
||||
Snowflake channelId,
|
||||
Snowflake? before = null,
|
||||
|
||||
@@ -63,7 +63,7 @@ internal class ExportContext
|
||||
|
||||
// User may have been deleted since they were mentioned
|
||||
if (user is not null)
|
||||
member = Member.CreateDefault(user);
|
||||
member = Member.CreateFallback(user);
|
||||
}
|
||||
|
||||
// Store the result even if it's null, to avoid re-fetching non-existing members
|
||||
|
||||
@@ -4,7 +4,6 @@ using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using DiscordChatExporter.Core.Discord;
|
||||
using DiscordChatExporter.Core.Discord.Data;
|
||||
using DiscordChatExporter.Core.Discord.Data.Common;
|
||||
using DiscordChatExporter.Core.Exporting.Filtering;
|
||||
using DiscordChatExporter.Core.Exporting.Partitioning;
|
||||
using DiscordChatExporter.Core.Utils;
|
||||
@@ -15,7 +14,7 @@ public partial class ExportRequest
|
||||
{
|
||||
public Guild Guild { get; }
|
||||
|
||||
public IChannel Channel { get; }
|
||||
public Channel Channel { get; }
|
||||
|
||||
public string OutputFilePath { get; }
|
||||
|
||||
@@ -43,7 +42,7 @@ public partial class ExportRequest
|
||||
|
||||
public ExportRequest(
|
||||
Guild guild,
|
||||
IChannel channel,
|
||||
Channel channel,
|
||||
string outputPath,
|
||||
string? assetsDirPath,
|
||||
ExportFormat format,
|
||||
@@ -95,7 +94,7 @@ public partial class ExportRequest
|
||||
{
|
||||
public static string GetDefaultOutputFileName(
|
||||
Guild guild,
|
||||
IChannel channel,
|
||||
Channel channel,
|
||||
ExportFormat format,
|
||||
Snowflake? after = null,
|
||||
Snowflake? before = null)
|
||||
@@ -103,7 +102,7 @@ public partial class ExportRequest
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
// Guild and channel names
|
||||
buffer.Append($"{guild.Name} - {channel.ParentName} - {channel.Name} [{channel.Id}]");
|
||||
buffer.Append($"{guild.Name} - {channel.Category} - {channel.Name} [{channel.Id}]");
|
||||
|
||||
// Date range
|
||||
if (after is not null || before is not null)
|
||||
@@ -138,7 +137,7 @@ public partial class ExportRequest
|
||||
private static string FormatPath(
|
||||
string path,
|
||||
Guild guild,
|
||||
IChannel channel,
|
||||
Channel channel,
|
||||
Snowflake? after,
|
||||
Snowflake? before)
|
||||
{
|
||||
@@ -149,12 +148,12 @@ public partial class ExportRequest
|
||||
{
|
||||
"%g" => guild.Id.ToString(),
|
||||
"%G" => guild.Name,
|
||||
"%t" => channel.ParentId.ToString() ?? "",
|
||||
"%T" => channel.ParentName ?? "",
|
||||
"%t" => channel.Parent?.Id.ToString() ?? "",
|
||||
"%T" => channel.Parent?.Name ?? "",
|
||||
"%c" => channel.Id.ToString(),
|
||||
"%C" => channel.Name,
|
||||
"%p" => channel.Position?.ToString() ?? "0",
|
||||
"%P" => channel.ParentPosition?.ToString() ?? "0",
|
||||
"%P" => channel.Parent?.Position?.ToString() ?? "0",
|
||||
"%a" => after?.ToDate().ToString("yyyy-MM-dd") ?? "",
|
||||
"%b" => before?.ToDate().ToString("yyyy-MM-dd") ?? "",
|
||||
"%d" => DateTimeOffset.Now.ToString("yyyy-MM-dd"),
|
||||
@@ -166,7 +165,7 @@ public partial class ExportRequest
|
||||
|
||||
private static string GetOutputBaseFilePath(
|
||||
Guild guild,
|
||||
IChannel channel,
|
||||
Channel channel,
|
||||
string outputPath,
|
||||
ExportFormat format,
|
||||
Snowflake? after = null,
|
||||
|
||||
@@ -241,8 +241,8 @@ internal class JsonMessageWriter : MessageWriter
|
||||
_writer.WriteStartObject("channel");
|
||||
_writer.WriteString("id", Context.Request.Channel.Id.ToString());
|
||||
_writer.WriteString("type", Context.Request.Channel.Kind.ToString());
|
||||
_writer.WriteString("categoryId", Context.Request.Channel.ParentId.ToString());
|
||||
_writer.WriteString("category", Context.Request.Channel.ParentName);
|
||||
_writer.WriteString("categoryId", Context.Request.Channel.Parent?.Id.ToString());
|
||||
_writer.WriteString("category", Context.Request.Channel.Category);
|
||||
_writer.WriteString("name", Context.Request.Channel.Name);
|
||||
_writer.WriteString("topic", Context.Request.Channel.Topic);
|
||||
|
||||
|
||||
@@ -193,7 +193,7 @@ internal class PlainTextMessageWriter : MessageWriter
|
||||
{
|
||||
await _writer.WriteLineAsync(new string('=', 62));
|
||||
await _writer.WriteLineAsync($"Guild: {Context.Request.Guild.Name}");
|
||||
await _writer.WriteLineAsync($"Channel: {Context.Request.Channel.ParentName} / {Context.Request.Channel.Name}");
|
||||
await _writer.WriteLineAsync($"Channel: {Context.Request.Channel.Category} / {Context.Request.Channel.Name}");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Context.Request.Channel.Topic))
|
||||
{
|
||||
|
||||
@@ -1004,7 +1004,7 @@
|
||||
</div>
|
||||
<div class="preamble__entries-container">
|
||||
<div class="preamble__entry">@Context.Request.Guild.Name</div>
|
||||
<div class="preamble__entry">@Context.Request.Channel.ParentName / @Context.Request.Channel.Name</div>
|
||||
<div class="preamble__entry">@Context.Request.Channel.Category / @Context.Request.Channel.Name</div>
|
||||
|
||||
@if (!string.IsNullOrWhiteSpace(Context.Request.Channel.Topic))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user