Use alternative endpoint to retrieve threads when requested by a bot (#1037)

Co-authored-by: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com>
pull/1040/head
William Unsworth 2 years ago committed by GitHub
parent 6bbde4ccdc
commit 41b4e47d0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -36,6 +36,12 @@ public class GetChannelsCommand : DiscordCommandBase
.ThenBy(c => c.Name)
.ToArray();
var threads = IncludeThreads
? (await Discord.GetGuildThreadsAsync(GuildId, cancellationToken))
.OrderBy(c => c.Name)
.ToArray()
: Array.Empty<ChannelThread>();
foreach (var channel in channels)
{
// Channel ID
@ -51,13 +57,7 @@ public class GetChannelsCommand : DiscordCommandBase
using (console.WithForegroundColor(ConsoleColor.White))
await console.Output.WriteLineAsync($"{channel.Category.Name} / {channel.Name}");
if (IncludeThreads)
{
var threads = (await Discord.GetChannelThreadsAsync(channel.Id, cancellationToken))
.OrderBy(c => c.Name)
.ToArray();
foreach (var thread in threads)
foreach (var thread in threads.Where(t => t.ParentId == channel.Id))
{
// Indent
await console.Output.WriteAsync('\t');
@ -78,4 +78,3 @@ public class GetChannelsCommand : DiscordCommandBase
}
}
}
}

@ -10,6 +10,7 @@ public record ChannelThread(
Snowflake Id,
ChannelKind Kind,
Snowflake GuildId,
Snowflake ParentId,
string Name,
Snowflake? LastMessageId) : IHasId
{
@ -18,6 +19,7 @@ public record ChannelThread(
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
var kind = (ChannelKind)json.GetProperty("type").GetInt32();
var guildId = json.GetProperty("guild_id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
var parentId = json.GetProperty("parent_id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
var name = json.GetProperty("name").GetNonWhiteSpaceString();
var lastMessageId = json
@ -29,6 +31,7 @@ public record ChannelThread(
id,
kind,
guildId,
parentId,
name,
lastMessageId
);

@ -311,15 +311,31 @@ public class DiscordClient
return Channel.Parse(response, category);
}
public async IAsyncEnumerable<ChannelThread> GetChannelThreadsAsync(
Snowflake channelId,
public async IAsyncEnumerable<ChannelThread> GetGuildThreadsAsync(
Snowflake guildId,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var channels = (await GetGuildChannelsAsync(guildId, cancellationToken))
.ToArray();
var tokenKind = _resolvedTokenKind ??= await GetTokenKindAsync(cancellationToken);
if (tokenKind == TokenKind.Bot)
{
var response = await GetJsonResponseAsync($"guilds/{guildId}/threads/active", cancellationToken);
foreach (var threadJson in response.GetProperty("threads").EnumerateArray())
{
yield return ChannelThread.Parse(threadJson);
}
}
foreach (var channel in channels)
{
if (tokenKind == TokenKind.User)
{
var currentOffset = 0;
while (true)
{
var url = new UrlBuilder()
.SetPath($"channels/{channelId}/threads/search")
.SetPath($"channels/{channel.Id}/threads/search")
.SetQueryParameter("offset", currentOffset.ToString())
.Build();
@ -337,6 +353,28 @@ public class DiscordClient
break;
}
}
else
{
var responsePublic = await TryGetJsonResponseAsync($"channels/{channel.Id}/threads/archived/public", cancellationToken);
var responsePrivate = await TryGetJsonResponseAsync($"channels/{channel.Id}/threads/archived/private", cancellationToken);
if (responsePublic is not null)
{
foreach (var threadJson in responsePublic.Value.GetProperty("threads").EnumerateArray())
{
yield return ChannelThread.Parse(threadJson);
}
}
if (responsePrivate is not null)
{
foreach (var threadJson in responsePrivate.Value.GetProperty("threads").EnumerateArray())
{
yield return ChannelThread.Parse(threadJson);
}
}
}
}
}
private async ValueTask<Message?> TryGetLastMessageAsync(
Snowflake channelId,

Loading…
Cancel
Save