Add option to reverse message order in exports (#1487)

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
Copilot
2026-02-26 21:14:57 +02:00
committed by GitHub
parent 522caba420
commit c4bfb3424e
18 changed files with 253 additions and 10 deletions

View File

@@ -578,6 +578,28 @@ public class DiscordClient(
}
}
private async ValueTask<Message?> TryGetFirstMessageAsync(
Snowflake channelId,
Snowflake? after = null,
Snowflake? before = null,
CancellationToken cancellationToken = default
)
{
var url = new UrlBuilder()
.SetPath($"channels/{channelId}/messages")
.SetQueryParameter("limit", "1")
.SetQueryParameter("after", (after ?? Snowflake.Zero).ToString())
.Build();
var response = await GetJsonResponseAsync(url, cancellationToken);
var message = response.EnumerateArray().Select(Message.Parse).FirstOrDefault();
if (message is null || before is not null && message.Timestamp > before.Value.ToDate())
return null;
return message;
}
private async ValueTask<Message?> TryGetLastMessageAsync(
Snowflake channelId,
Snowflake? before = null,
@@ -684,6 +706,86 @@ public class DiscordClient(
}
}
public async IAsyncEnumerable<Message> GetMessagesInReverseAsync(
Snowflake channelId,
Snowflake? after = null,
Snowflake? before = null,
IProgress<Percentage>? progress = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default
)
{
// Get the first message (oldest) in the range to use as the lower bound for
// progress calculation.
var firstMessage = await TryGetFirstMessageAsync(
channelId,
after,
before,
cancellationToken
);
if (firstMessage is null)
yield break;
// Keep track of the last message in range in order to calculate the progress
var lastMessage = default(Message);
var currentBefore = before;
while (true)
{
var url = new UrlBuilder()
.SetPath($"channels/{channelId}/messages")
.SetQueryParameter("limit", "100")
.SetQueryParameter("before", currentBefore?.ToString())
.Build();
var response = await GetJsonResponseAsync(url, cancellationToken);
var messages = response.EnumerateArray().Select(Message.Parse).ToArray();
// Break if there are no messages (can happen if messages are deleted during execution)
if (!messages.Any())
yield break;
// If all messages are empty, make sure that it's not because the bot account doesn't
// have the Message Content Intent enabled.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/1106#issuecomment-1741548959
if (
messages.All(m => m.IsEmpty)
&& await ResolveTokenKindAsync(cancellationToken) == TokenKind.Bot
)
{
var application = await GetApplicationAsync(cancellationToken);
if (!application.IsMessageContentIntentEnabled)
{
throw new DiscordChatExporterException(
"Provided bot account does not have the Message Content Intent enabled.",
true
);
}
}
foreach (var message in messages)
{
lastMessage ??= message;
// Report progress based on timestamps
if (progress is not null)
{
var exportedDuration = (lastMessage.Timestamp - message.Timestamp).Duration();
var totalDuration = (lastMessage.Timestamp - firstMessage.Timestamp).Duration();
progress.Report(
Percentage.FromFraction(
totalDuration > TimeSpan.Zero ? exportedDuration / totalDuration : 1
)
);
}
yield return message;
}
currentBefore = messages.Last().Id;
}
}
public async IAsyncEnumerable<User> GetMessageReactionsAsync(
Snowflake channelId,
Snowflake messageId,

View File

@@ -64,15 +64,23 @@ public class ChannelExporter(DiscordClient discord)
);
}
await foreach (
var message in discord.GetMessagesAsync(
var messages = !request.IsReverseMessageOrder
? discord.GetMessagesAsync(
request.Channel.Id,
request.After,
request.Before,
progress,
cancellationToken
)
)
: discord.GetMessagesInReverseAsync(
request.Channel.Id,
request.After,
request.Before,
progress,
cancellationToken
);
await foreach (var message in messages)
{
try
{

View File

@@ -33,6 +33,8 @@ public partial class ExportRequest
public MessageFilter MessageFilter { get; }
public bool IsReverseMessageOrder { get; }
public bool ShouldFormatMarkdown { get; }
public bool ShouldDownloadAssets { get; }
@@ -55,6 +57,7 @@ public partial class ExportRequest
Snowflake? before,
PartitionLimit partitionLimit,
MessageFilter messageFilter,
bool isReverseMessageOrder,
bool shouldFormatMarkdown,
bool shouldDownloadAssets,
bool shouldReuseAssets,
@@ -69,6 +72,7 @@ public partial class ExportRequest
Before = before;
PartitionLimit = partitionLimit;
MessageFilter = messageFilter;
IsReverseMessageOrder = isReverseMessageOrder;
ShouldFormatMarkdown = shouldFormatMarkdown;
ShouldDownloadAssets = shouldDownloadAssets;
ShouldReuseAssets = shouldReuseAssets;

View File

@@ -17,6 +17,7 @@ internal class HtmlMessageWriter(Stream stream, ExportContext context, string th
private readonly HtmlMinifier _minifier = new();
private readonly List<Message> _messageGroup = [];
// Note: in reverse order, last message is earlier than first message
private bool CanJoinGroup(Message message)
{
// If the group is empty, any message can join it

View File

@@ -23,13 +23,11 @@ internal static class PlainTextMessageExtensions
: "Removed a recipient.",
MessageKind.Call =>
$"Started a call that lasted {
message
$"Started a call that lasted {message
.CallEndedTimestamp?
.Pipe(t => t - message.Timestamp)
.Pipe(t => t.TotalMinutes)
.ToString("n0", CultureInfo.InvariantCulture) ?? "0"
} minutes.",
.ToString("n0", CultureInfo.InvariantCulture) ?? "0"} minutes.",
MessageKind.ChannelNameChange => !string.IsNullOrWhiteSpace(message.Content)
? $"Changed the channel name: {message.Content}"