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; } 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) public override async ValueTask ExecuteAsync(IConsole console)
{ {
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
@ -42,7 +48,9 @@ public class GetChannelsCommand : DiscordCommandBase
.FirstOrDefault(); .FirstOrDefault();
var threads = IncludeThreads 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>(); : Array.Empty<Channel>();
foreach (var channel in channels) foreach (var channel in channels)
@ -90,7 +98,7 @@ public class GetChannelsCommand : DiscordCommandBase
// Thread status // Thread status
using (console.WithForegroundColor(ConsoleColor.White)) 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, int? Position,
string? IconUrl, string? IconUrl,
string? Topic, string? Topic,
bool IsActive, bool IsArchived,
Snowflake? LastMessageId) : IHasId Snowflake? LastMessageId) : IHasId
{ {
// Used for visual backwards-compatibility with old exports, where // Used for visual backwards-compatibility with old exports, where
@ -77,10 +77,10 @@ public partial record Channel
var topic = json.GetPropertyOrNull("topic")?.GetStringOrNull(); var topic = json.GetPropertyOrNull("topic")?.GetStringOrNull();
var isActive = !json var isArchived = json
.GetPropertyOrNull("thread_metadata")? .GetPropertyOrNull("thread_metadata")?
.GetPropertyOrNull("archived")? .GetPropertyOrNull("archived")?
.GetBooleanOrNull() ?? true; .GetBooleanOrNull() ?? false;
var lastMessageId = json var lastMessageId = json
.GetPropertyOrNull("last_message_id")? .GetPropertyOrNull("last_message_id")?
@ -96,7 +96,7 @@ public partial record Channel
position, position,
iconUrl, iconUrl,
topic, topic,
isActive, isArchived,
lastMessageId lastMessageId
); );
} }

View file

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