Skip to content

Commit

Permalink
AlwaysDownloadUsers will prevent from getting guild members count
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Dec 18, 2022
1 parent 9f808b3 commit 8f4fb79
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
25 changes: 18 additions & 7 deletions src/Kook.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,27 @@ public class SocketGuild : SocketEntity<ulong>, IGuild, IDisposable, IUpdateable
/// between that and this property.
/// </para>
/// </note>
/// <note type="warning">
/// Only when <see cref="KookSocketConfig.AlwaysDownloadUsers"/> is set to <see langword="true"/>
/// will this property be populated upon startup. Otherwise, this property will be <see langword="null"/>,
/// and will be populated when <see cref="DownloadUsersAsync"/> is called.
/// </note>
/// </remarks>
public int MemberCount { get; internal set; }
public int? MemberCount { get; internal set; }
/// <summary> Gets the number of members downloaded to the local guild cache. </summary>
public int DownloadedMemberCount { get; private set; }

internal bool IsAvailable { get; private set; }
/// <summary> Indicates whether the client is connected to this guild. </summary>
public bool IsConnected { get; internal set; }
/// <summary> Indicates whether the client has all the members downloaded to the local guild cache. </summary>
public bool HasAllMembers => MemberCount <= DownloadedMemberCount;
/// <remarks>
/// <note type="warning">
/// If <see cref="MemberCount"/> is <see langword="null"/>, this property will always return <see langword="null"/>,
/// which means that the client is unable to determine whether all the members are downloaded or not.
/// </note>
/// </remarks>
public bool? HasAllMembers => MemberCount is null ? null : MemberCount <= DownloadedMemberCount;
/// <inheritdoc/>
public int MaxBitrate => GuildHelper.GetMaxBitrate(this);
/// <inheritdoc/>
Expand Down Expand Up @@ -181,7 +192,7 @@ public IReadOnlyCollection<SocketGuildChannel> Channels
/// A <see cref="SocketTextChannel"/> representing the default text channel for this guild.
/// </returns>
public SocketTextChannel DefaultChannel => TextChannels
.Where(c => CurrentUser.GetPermissions(c).ViewChannel)
.Where(c => CurrentUser?.GetPermissions(c).ViewChannel is true)
.SingleOrDefault(c => c.Id == DefaultChannelId);
/// <summary>
/// Gets the welcome text channel for this guild.
Expand All @@ -193,7 +204,7 @@ public IReadOnlyCollection<SocketGuildChannel> Channels
/// A <see cref="SocketTextChannel"/> representing the default text channel for this guild.
/// </returns>
public SocketTextChannel WelcomeChannel => TextChannels
.Where(c => CurrentUser.GetPermissions(c).ViewChannel)
.Where(c => CurrentUser?.GetPermissions(c).ViewChannel is true)
.SingleOrDefault(c => c.Id == WelcomeChannelId);
/// <inheritdoc cref="IGuild.Emotes"/>
public IReadOnlyCollection<GuildEmote> Emotes => _emotes.Select(x => x.Value).Where(x => x != null).ToReadOnlyCollection(_emotes);
Expand Down Expand Up @@ -376,7 +387,7 @@ internal void Update(ClientState state, Model model)
NotifyType = model.NotifyType;
Region = model.Region;
IsOpenEnabled = model.EnableOpen;
OpenId = model.OpenId != 0 ? model.OpenId : null;;
OpenId = model.OpenId != 0 ? model.OpenId : null;
DefaultChannelId = model.DefaultChannelId != 0 ? model.DefaultChannelId : null;
WelcomeChannelId = model.WelcomeChannelId != 0 ? model.WelcomeChannelId : null;

Expand Down Expand Up @@ -770,7 +781,7 @@ public void PurgeUserCache(Func<SocketGuildUser, bool> predicate)
/// </returns>
public IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> GetUsersAsync(RequestOptions options = null)
{
if (HasAllMembers)
if (HasAllMembers is true)
return ImmutableArray.Create(Users).ToAsyncEnumerable<IReadOnlyCollection<IGuildUser>>();
return GuildHelper.GetUsersAsync(this, Kook, KookConfig.MaxUsersPerBatch, 1, options);
}
Expand Down Expand Up @@ -936,7 +947,7 @@ public void Dispose() { }
/// <inheritdoc />
async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload && !HasAllMembers)
if (mode == CacheMode.AllowDownload && HasAllMembers is not true)
return (await GetUsersAsync(options).FlattenAsync().ConfigureAwait(false)).ToImmutableArray();
else
return Users;
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/Entities/Roles/SocketRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public Task DeleteAsync(RequestOptions options = null)
public async IAsyncEnumerable<IReadOnlyCollection<SocketGuildUser>> GetUsersAsync(RequestOptions options = null)
{
// From SocketGuild.Users
if (Guild.HasAllMembers)
if (Guild.HasAllMembers is true)
{
IEnumerable<IReadOnlyCollection<SocketGuildUser>> userCollections = Guild.Users
.Where(u => u.Roles.Contains(this))
Expand Down
10 changes: 3 additions & 7 deletions src/Kook.Net.WebSocket/KookSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private KookSocketClient(KookSocketConfig config, KookSocketApiClient client)
if (_guildDownloadTask?.IsCompleted == true
&& ConnectionState == ConnectionState.Connected)
{
if (AlwaysDownloadUsers && !g.HasAllMembers)
if (AlwaysDownloadUsers && g.HasAllMembers is not true)
_ = g.DownloadUsersAsync();
if (AlwaysDownloadVoiceStates)
_ = g.DownloadVoiceStatesAsync();
Expand Down Expand Up @@ -1559,14 +1559,10 @@ await _gatewayLogger.WarningAsync($"Unknown Event Type ({gatewayEvent.Type})")
}
else if (_connection.CancelToken.IsCancellationRequested)
return;

// Download guild member count
foreach (SocketGuild socketGuild in State.Guilds)
socketGuild.MemberCount = await ApiClient.GetGuildMemberCountAsync(socketGuild.Id).ConfigureAwait(false);


// Download user list if enabled
if (BaseConfig.AlwaysDownloadUsers)
_ = DownloadUsersAsync(Guilds.Where(x => x.IsAvailable && !x.HasAllMembers));
_ = DownloadUsersAsync(Guilds.Where(x => x.IsAvailable && x.HasAllMembers is not true));
if (BaseConfig.AlwaysDownloadVoiceStates)
_ = DownloadVoiceStatesAsync(Guilds.Where(x => x.IsAvailable));
if (BaseConfig.AlwaysDownloadBoostSubscriptions)
Expand Down

0 comments on commit 8f4fb79

Please sign in to comment.