mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-11 10:26:57 +02:00
Don't ignore next pages of archived threads
This commit is contained in:
parent
6fb197cf0b
commit
7ddd55951c
1 changed files with 50 additions and 30 deletions
|
@ -438,43 +438,63 @@ public class DiscordClient(string token)
|
||||||
foreach (var channel in channels)
|
foreach (var channel in channels)
|
||||||
{
|
{
|
||||||
// Public archived threads
|
// Public archived threads
|
||||||
{
|
await foreach (
|
||||||
// Can be null on certain channels
|
var th in GetAllArchivedThreadsAsync(channel, "public", cancellationToken)
|
||||||
var response = await TryGetJsonResponseAsync(
|
)
|
||||||
$"channels/{channel.Id}/threads/archived/public",
|
yield return th;
|
||||||
cancellationToken
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response is null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
foreach (
|
|
||||||
var threadJson in response.Value.GetProperty("threads").EnumerateArray()
|
|
||||||
)
|
|
||||||
yield return Channel.Parse(threadJson, channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Private archived threads
|
// Private archived threads
|
||||||
{
|
await foreach (
|
||||||
// Can be null on certain channels
|
var th in GetAllArchivedThreadsAsync(channel, "private", cancellationToken)
|
||||||
var response = await TryGetJsonResponseAsync(
|
)
|
||||||
$"channels/{channel.Id}/threads/archived/private",
|
yield return th;
|
||||||
cancellationToken
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response is null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
foreach (
|
|
||||||
var threadJson in response.Value.GetProperty("threads").EnumerateArray()
|
|
||||||
)
|
|
||||||
yield return Channel.Parse(threadJson, channel);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async IAsyncEnumerable<Channel> GetAllArchivedThreadsAsync(
|
||||||
|
Channel channel,
|
||||||
|
string archiveType,
|
||||||
|
[EnumeratorCancellation] CancellationToken cancellationToken
|
||||||
|
)
|
||||||
|
{
|
||||||
|
// Base endpoint: "public" or "private"
|
||||||
|
var endpointBase = $"channels/{channel.Id}/threads/archived/{archiveType}";
|
||||||
|
// Cursor parameter: ISO8601 timestamp string
|
||||||
|
string? beforeTimestamp = null;
|
||||||
|
bool hasMorePages = true;
|
||||||
|
|
||||||
|
while (hasMorePages && !cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
// Build URL with optional before= parameter
|
||||||
|
var url = beforeTimestamp is null
|
||||||
|
? endpointBase
|
||||||
|
: $"{endpointBase}?before={Uri.EscapeDataString(beforeTimestamp)}";
|
||||||
|
|
||||||
|
var response = await TryGetJsonResponseAsync(url, cancellationToken);
|
||||||
|
if (response is null)
|
||||||
|
yield break;
|
||||||
|
|
||||||
|
// Parse out the threads array
|
||||||
|
var threadsJson = response.Value.GetProperty("threads").EnumerateArray().ToList();
|
||||||
|
foreach (var threadJson in threadsJson)
|
||||||
|
{
|
||||||
|
yield return Channel.Parse(threadJson, channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check pagination flag
|
||||||
|
hasMorePages = response.Value.GetProperty("has_more").GetBoolean();
|
||||||
|
|
||||||
|
if (hasMorePages && threadsJson.Count > 0)
|
||||||
|
{
|
||||||
|
// Prepare next cursor: the archived timestamp of the last thread
|
||||||
|
var lastThreadMeta = threadsJson.Last().GetProperty("thread_metadata");
|
||||||
|
beforeTimestamp = lastThreadMeta.GetProperty("archive_timestamp").GetString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async IAsyncEnumerable<Role> GetGuildRolesAsync(
|
public async IAsyncEnumerable<Role> GetGuildRolesAsync(
|
||||||
Snowflake guildId,
|
Snowflake guildId,
|
||||||
[EnumeratorCancellation] CancellationToken cancellationToken = default
|
[EnumeratorCancellation] CancellationToken cancellationToken = default
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue