From f21f1d78b0894eba172f3ef3ac0e095f9cbe219f Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 31 Jul 2020 20:16:00 -0300 Subject: [PATCH] Fix SocketGuild --- .../Entities/Guilds/SocketGuild.cs | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index 169190a33..d2d759bb3 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -809,17 +809,37 @@ namespace Discord.WebSocket var members = Users; var self = CurrentUser; _members.Clear(); - _members.TryAdd(self.Id, self); + if (self != null) + _members.TryAdd(self.Id, self); DownloadedMemberCount = _members.Count; foreach (var member in members) { - if (member.Id != self.Id) + if (member.Id != self?.Id) member.GlobalUser.RemoveRef(Discord); } } + /// + /// 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() { @@ -1201,8 +1221,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)