From dc015f9779b2a11a9d2292acdf96ec313cb350f5 Mon Sep 17 00:00:00 2001 From: Fokusnikh <96922685+Polybroo@users.noreply.github.com> Date: Mon, 22 Aug 2022 23:12:56 +0200 Subject: [PATCH] ForumChannels returns only threads it is parent of --- src/Discord.Net.Rest/DiscordRestApiClient.cs | 27 +++++++++++++++++++ .../Entities/Channels/RestForumChannel.cs | 2 +- .../Entities/Channels/ThreadHelper.cs | 5 ++++ .../Entities/Channels/SocketForumChannel.cs | 4 +-- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index c5b075103..a6c971842 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -595,6 +595,33 @@ namespace Discord.API return await SendAsync("GET", () => $"guilds/{guildId}/threads/active", bucket, options: options); } + public async Task GetActiveThreadsInChannelAsync(ulong guildId, ulong parentChannel, RequestOptions options = null) + { + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + + options = RequestOptions.CreateOrClone(options); + + var bucket = new BucketIds(guildId: guildId); + + ChannelThreads allChannelThreads = await SendAsync("GET", () => $"guilds/{guildId}/threads/active", bucket, options: options); + ChannelThreads filteredThreads = new ChannelThreads(); + + filteredThreads.Threads = allChannelThreads.Threads.Where(x => x.CategoryId == parentChannel).ToArray(); + + List members = new List(); + + foreach (ThreadMember m in allChannelThreads.Members) + { + if (filteredThreads.Threads.Where(x => x.Id == (ulong)m.Id).Count() == 0) + continue; + members.Add(m); + } + + filteredThreads.Members = members.ToArray(); + + return filteredThreads; + } + public async Task GetPublicArchivedThreadsAsync(ulong channelId, DateTimeOffset? before = null, int? limit = null, RequestOptions options = null) { Preconditions.NotEqual(channelId, 0, nameof(channelId)); diff --git a/src/Discord.Net.Rest/Entities/Channels/RestForumChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestForumChannel.cs index aff8400aa..51889a788 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestForumChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestForumChannel.cs @@ -92,7 +92,7 @@ namespace Discord.Rest /// public Task> GetActiveThreadsAsync(RequestOptions options = null) - => ThreadHelper.GetActiveThreadsAsync(Guild, Discord, options); + => ThreadHelper.GetActiveThreadsInChannelAsync(Guild, this, Discord, options); /// public Task> GetJoinedPrivateArchivedThreadsAsync(int? limit = null, DateTimeOffset? before = null, RequestOptions options = null) diff --git a/src/Discord.Net.Rest/Entities/Channels/ThreadHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ThreadHelper.cs index f5fce5a50..7e0494e58 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ThreadHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ThreadHelper.cs @@ -67,6 +67,11 @@ namespace Discord.Rest var result = await client.ApiClient.GetActiveThreadsAsync(guild.Id, options).ConfigureAwait(false); return result.Threads.Select(x => RestThreadChannel.Create(client, guild, x)).ToImmutableArray(); } + public static async Task> GetActiveThreadsInChannelAsync(IGuild guild, IGuildChannel parentChannel, BaseDiscordClient client, RequestOptions options) + { + var result = await client.ApiClient.GetActiveThreadsInChannelAsync(guild.Id, parentChannel.Id, options).ConfigureAwait(false); + return result.Threads.Select(x => RestThreadChannel.Create(client, guild, x)).ToImmutableArray(); + } public static async Task> GetPublicArchivedThreadsAsync(IGuildChannel channel, BaseDiscordClient client, int? limit = null, DateTimeOffset? before = null, RequestOptions options = null) diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs index ea58ecdb5..3b216aef6 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs @@ -89,7 +89,7 @@ namespace Discord.WebSocket /// public Task> GetActiveThreadsAsync(RequestOptions options = null) - => ThreadHelper.GetActiveThreadsAsync(Guild, Discord, options); + => ThreadHelper.GetActiveThreadsInChannelAsync(Guild, this, Discord, options); /// public Task> GetJoinedPrivateArchivedThreadsAsync(int? limit = null, DateTimeOffset? before = null, RequestOptions options = null) @@ -105,7 +105,7 @@ namespace Discord.WebSocket #region IForumChannel async Task> IForumChannel.GetActiveThreadsAsync(RequestOptions options) - => await GetActiveThreadsAsync(options).ConfigureAwait(false); + => await GetActiveThreadsInChannelAsync(options).ConfigureAwait(false); async Task> IForumChannel.GetPublicArchivedThreadsAsync(int? limit, DateTimeOffset? before, RequestOptions options) => await GetPublicArchivedThreadsAsync(limit, before, options).ConfigureAwait(false); async Task> IForumChannel.GetPrivateArchivedThreadsAsync(int? limit, DateTimeOffset? before, RequestOptions options)