Print archived threads only if explicitly requested in the `channels` command

pull/1125/head
Tyrrrz 1 year ago
parent ac1bd16439
commit 6fba60e570

@ -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");
} }
} }
} }

@ -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
); );
} }

@ -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,6 +319,9 @@ public class DiscordClient
} }
} }
// Archived threads
if (includeArchived)
{
foreach (var channel in channels) foreach (var channel in channels)
{ {
// Public archived threads // Public archived threads
@ -340,6 +348,7 @@ public class DiscordClient
} }
} }
} }
}
public async IAsyncEnumerable<Role> GetGuildRolesAsync( public async IAsyncEnumerable<Role> GetGuildRolesAsync(
Snowflake guildId, Snowflake guildId,

Loading…
Cancel
Save