mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-10 18:06:37 +02:00
This commit is contained in:
parent
6fb197cf0b
commit
0f4e7e34f5
1 changed files with 50 additions and 30 deletions
|
@ -438,43 +438,63 @@ public class DiscordClient(string token)
|
|||
foreach (var channel in channels)
|
||||
{
|
||||
// Public archived threads
|
||||
{
|
||||
// Can be null on certain channels
|
||||
var response = await TryGetJsonResponseAsync(
|
||||
$"channels/{channel.Id}/threads/archived/public",
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
if (response is null)
|
||||
continue;
|
||||
|
||||
foreach (
|
||||
var threadJson in response.Value.GetProperty("threads").EnumerateArray()
|
||||
)
|
||||
yield return Channel.Parse(threadJson, channel);
|
||||
}
|
||||
await foreach (
|
||||
var th in GetAllArchivedThreadsAsync(channel, "public", cancellationToken)
|
||||
)
|
||||
yield return th;
|
||||
|
||||
// Private archived threads
|
||||
{
|
||||
// Can be null on certain channels
|
||||
var response = await TryGetJsonResponseAsync(
|
||||
$"channels/{channel.Id}/threads/archived/private",
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
if (response is null)
|
||||
continue;
|
||||
|
||||
foreach (
|
||||
var threadJson in response.Value.GetProperty("threads").EnumerateArray()
|
||||
)
|
||||
yield return Channel.Parse(threadJson, channel);
|
||||
}
|
||||
await foreach (
|
||||
var th in GetAllArchivedThreadsAsync(channel, "private", cancellationToken)
|
||||
)
|
||||
yield return th;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
Snowflake guildId,
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken = default
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue