Print archived threads only if explicitly requested in the channels command

This commit is contained in:
Tyrrrz 2023-08-21 17:22:44 +03:00
parent ac1bd16439
commit 6fba60e570
3 changed files with 41 additions and 24 deletions

View file

@ -26,6 +26,12 @@ public class GetChannelsCommand : DiscordCommandBase
)]
public bool IncludeThreads { get; init; }
[CommandOption(
"include-archived-threads",
Description = "Include archived threads in the output."
)]
public bool IncludeArchivedThreads { get; init; }
public override async ValueTask ExecuteAsync(IConsole console)
{
var cancellationToken = console.RegisterCancellationHandler();
@ -42,7 +48,9 @@ public class GetChannelsCommand : DiscordCommandBase
.FirstOrDefault();
var threads = IncludeThreads
? (await Discord.GetGuildThreadsAsync(GuildId, cancellationToken)).OrderBy(c => c.Name).ToArray()
? (await Discord.GetGuildThreadsAsync(GuildId, IncludeArchivedThreads, cancellationToken))
.OrderBy(c => c.Name)
.ToArray()
: Array.Empty<Channel>();
foreach (var channel in channels)
@ -90,7 +98,7 @@ public class GetChannelsCommand : DiscordCommandBase
// Thread status
using (console.WithForegroundColor(ConsoleColor.White))
await console.Output.WriteLineAsync(channelThread.IsActive ? "Active" : "Archived");
await console.Output.WriteLineAsync(channelThread.IsArchived ? "Archived" : "Active");
}
}
}

View file

@ -16,7 +16,7 @@ public partial record Channel(
int? Position,
string? IconUrl,
string? Topic,
bool IsActive,
bool IsArchived,
Snowflake? LastMessageId) : IHasId
{
// Used for visual backwards-compatibility with old exports, where
@ -77,10 +77,10 @@ public partial record Channel
var topic = json.GetPropertyOrNull("topic")?.GetStringOrNull();
var isActive = !json
var isArchived = json
.GetPropertyOrNull("thread_metadata")?
.GetPropertyOrNull("archived")?
.GetBooleanOrNull() ?? true;
.GetBooleanOrNull() ?? false;
var lastMessageId = json
.GetPropertyOrNull("last_message_id")?
@ -96,7 +96,7 @@ public partial record Channel
position,
iconUrl,
topic,
isActive,
isArchived,
lastMessageId
);
}

View file

@ -260,6 +260,7 @@ public class DiscordClient
public async IAsyncEnumerable<Channel> GetGuildThreadsAsync(
Snowflake guildId,
bool includeArchived = false,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var tokenKind = _resolvedTokenKind ??= await GetTokenKindAsync(cancellationToken);
@ -285,7 +286,11 @@ public class DiscordClient
foreach (var threadJson in response.Value.GetProperty("threads").EnumerateArray())
{
yield return Channel.Parse(threadJson, channel);
var thread = Channel.Parse(threadJson, channel);
if (!includeArchived && thread.IsArchived)
continue;
yield return thread;
currentOffset++;
}
@ -314,28 +319,32 @@ public class DiscordClient
}
}
foreach (var channel in channels)
// Archived threads
if (includeArchived)
{
// Public archived threads
foreach (var channel in channels)
{
var response = await GetJsonResponseAsync(
$"channels/{channel.Id}/threads/archived/public",
cancellationToken
);
// Public archived threads
{
var response = await GetJsonResponseAsync(
$"channels/{channel.Id}/threads/archived/public",
cancellationToken
);
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
yield return Channel.Parse(threadJson, channel);
}
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
yield return Channel.Parse(threadJson, channel);
}
// Private archived threads
{
var response = await GetJsonResponseAsync(
$"channels/{channel.Id}/threads/archived/private",
cancellationToken
);
// Private archived threads
{
var response = await GetJsonResponseAsync(
$"channels/{channel.Id}/threads/archived/private",
cancellationToken
);
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
yield return Channel.Parse(threadJson, channel);
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
yield return Channel.Parse(threadJson, channel);
}
}
}
}