From 30b5a833d25e794ecbf31ef3490d3458d0d721db Mon Sep 17 00:00:00 2001 From: Paulo Date: Mon, 25 May 2020 00:37:21 -0300 Subject: [PATCH] feature: Add GetUsersAsync to SocketGuild (#1549) * Add GetUsersAsync to SocketGuild * Fix IGuild return * Do not download unless needed --- .../Entities/Guilds/IGuild.cs | 3 ++ .../Entities/Guilds/SocketGuild.cs | 28 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs index b39a49776..cdf0118c4 100644 --- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs +++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs @@ -683,6 +683,9 @@ namespace Discord /// /// Downloads all users for this guild if the current list is incomplete. /// + /// + /// This method downloads all users found within this guild throught the Gateway and caches them. + /// /// /// A task that represents the asynchronous download operation. /// diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index e556853f2..cdba2b67d 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -821,6 +821,25 @@ namespace Discord.WebSocket } } + /// + /// Gets a collection of all users in this guild. + /// + /// + /// This method retrieves all users found within this guild throught REST. + /// Users returned by this method are not cached. + /// + /// The options to be used when sending the request. + /// + /// A task that represents the asynchronous get operation. The task result contains a collection of guild + /// users found within this guild. + /// + public IAsyncEnumerable> GetUsersAsync(RequestOptions options = null) + { + if (HasAllMembers) + return ImmutableArray.Create(Users).ToAsyncEnumerable>(); + return GuildHelper.GetUsersAsync(this, Discord, null, null, options); + } + /// public async Task DownloadUsersAsync() { @@ -1185,8 +1204,13 @@ namespace Discord.WebSocket => await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options).ConfigureAwait(false); /// - Task> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options) - => Task.FromResult>(Users); + async Task> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload && !HasAllMembers) + return (await GetUsersAsync(options).FlattenAsync().ConfigureAwait(false)).ToImmutableArray(); + else + return Users; + } /// async Task IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action func, RequestOptions options)