Skip to content

Commit

Permalink
feature: Add GetUsersAsync to SocketGuild (#1549)
Browse files Browse the repository at this point in the history
* Add GetUsersAsync to SocketGuild

* Fix IGuild return

* Do not download unless needed
  • Loading branch information
SubZero0 authored May 25, 2020
1 parent 7585789 commit 30b5a83
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// <summary>
/// Downloads all users for this guild if the current list is incomplete.
/// </summary>
/// <remarks>
/// This method downloads all users found within this guild throught the Gateway and caches them.
/// </remarks>
/// <returns>
/// A task that represents the asynchronous download operation.
/// </returns>
Expand Down
28 changes: 26 additions & 2 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,25 @@ internal void PurgeGuildUserCache()
}
}

/// <summary>
/// Gets a collection of all users in this guild.
/// </summary>
/// <remarks>
/// <para>This method retrieves all users found within this guild throught REST.</para>
/// <para>Users returned by this method are not cached.</para>
/// </remarks>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous get operation. The task result contains a collection of guild
/// users found within this guild.
/// </returns>
public IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> GetUsersAsync(RequestOptions options = null)
{
if (HasAllMembers)
return ImmutableArray.Create(Users).ToAsyncEnumerable<IReadOnlyCollection<IGuildUser>>();
return GuildHelper.GetUsersAsync(this, Discord, null, null, options);
}

/// <inheritdoc />
public async Task DownloadUsersAsync()
{
Expand Down Expand Up @@ -1185,8 +1204,13 @@ async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissi
=> await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options).ConfigureAwait(false);

/// <inheritdoc />
Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IReadOnlyCollection<IGuildUser>>(Users);
async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload && !HasAllMembers)
return (await GetUsersAsync(options).FlattenAsync().ConfigureAwait(false)).ToImmutableArray();
else
return Users;
}

/// <inheritdoc />
async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options)
Expand Down

0 comments on commit 30b5a83

Please sign in to comment.