-
-
Notifications
You must be signed in to change notification settings - Fork 745
Allow specifying list of user ids in SocketGuild.DownloadUsersAsync #2676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 5 commits
26cb939
f8ace79
df48372
3f599c8
0d06290
1aad00b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ public partial class DiscordSocketClient : BaseSocketClient, IDiscordClient | |
private readonly ConnectionManager _connection; | ||
private readonly Logger _gatewayLogger; | ||
private readonly SemaphoreSlim _stateLock; | ||
private readonly ConcurrentDictionary<string, TaskCompletionSource<bool>> _guildMembersRequestTasks; | ||
|
||
private string _sessionId; | ||
private int _lastSeq; | ||
|
@@ -51,6 +52,7 @@ public partial class DiscordSocketClient : BaseSocketClient, IDiscordClient | |
private GatewayIntents _gatewayIntents; | ||
private ImmutableArray<StickerPack<SocketSticker>> _defaultStickers; | ||
private SocketSelfUser _previousSessionUser; | ||
private long _guildMembersRequestCounter; | ||
|
||
/// <summary> | ||
/// Provides access to a REST-only client with a shared state from this client. | ||
|
@@ -183,6 +185,8 @@ private DiscordSocketClient(DiscordSocketConfig config, API.DiscordSocketApiClie | |
e.ErrorContext.Handled = true; | ||
}; | ||
|
||
_guildMembersRequestTasks = new ConcurrentDictionary<string, TaskCompletionSource<bool>>(); | ||
|
||
ApiClient.SentGatewayMessage += async opCode => await _gatewayLogger.DebugAsync($"Sent {opCode}").ConfigureAwait(false); | ||
ApiClient.ReceivedGatewayEvent += ProcessMessageAsync; | ||
|
||
|
@@ -627,30 +631,36 @@ private async Task ProcessUserDownloadsAsync(IEnumerable<SocketGuild> guilds) | |
{ | ||
var cachedGuilds = guilds.ToImmutableArray(); | ||
|
||
const short batchSize = 1; | ||
ulong[] batchIds = new ulong[Math.Min(batchSize, cachedGuilds.Length)]; | ||
Task[] batchTasks = new Task[batchIds.Length]; | ||
int batchCount = (cachedGuilds.Length + (batchSize - 1)) / batchSize; | ||
foreach (var guild in cachedGuilds) | ||
{ | ||
await ApiClient.SendRequestMembersAsync(guild.Id).ConfigureAwait(false); | ||
await guild.DownloaderPromise.ConfigureAwait(false); | ||
} | ||
} | ||
|
||
for (int i = 0, k = 0; i < batchCount; i++) | ||
/// <inheritdoc /> | ||
public override async Task DownloadUsersAsync(IGuild guild, IEnumerable<ulong> userIds) | ||
{ | ||
if (ConnectionState == ConnectionState.Connected) | ||
{ | ||
bool isLast = i == batchCount - 1; | ||
int count = isLast ? (cachedGuilds.Length - (batchCount - 1) * batchSize) : batchSize; | ||
EnsureGatewayIntent(GatewayIntents.GuildMembers); | ||
|
||
for (int j = 0; j < count; j++, k++) | ||
var socketGuild = GetGuild(guild.Id); | ||
if (socketGuild != null) | ||
{ | ||
var guild = cachedGuilds[k]; | ||
batchIds[j] = guild.Id; | ||
batchTasks[j] = guild.DownloaderPromise; | ||
await ProcessUserDownloadsAsync(socketGuild, userIds).ConfigureAwait(false); | ||
} | ||
} | ||
compujuckel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
await ApiClient.SendRequestMembersAsync(batchIds).ConfigureAwait(false); | ||
|
||
if (isLast && batchCount > 1) | ||
await Task.WhenAll(batchTasks.Take(count)).ConfigureAwait(false); | ||
else | ||
await Task.WhenAll(batchTasks).ConfigureAwait(false); | ||
} | ||
private async Task ProcessUserDownloadsAsync(SocketGuild guild, IEnumerable<ulong> userIds) | ||
{ | ||
var nonce = Interlocked.Increment(ref _guildMembersRequestCounter).ToString(); | ||
var tcs = new TaskCompletionSource<bool>(); | ||
_guildMembersRequestTasks.TryAdd(nonce, tcs); | ||
await ApiClient.SendRequestMembersAsync(guild.Id, userIds, nonce).ConfigureAwait(false); | ||
await tcs.Task.ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
|
@@ -1410,6 +1420,13 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty | |
guild.CompleteDownloadUsers(); | ||
await TimedInvokeAsync(_guildMembersDownloadedEvent, nameof(GuildMembersDownloaded), guild).ConfigureAwait(false); | ||
} | ||
|
||
if (data.Nonce.IsSpecified | ||
&& data.ChunkIndex + 1 >= data.ChunkCount | ||
&& _guildMembersRequestTasks.TryRemove(data.Nonce.Value, out var tcs)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This being the only way of removing request tasks may result in a memory leak if client misses the event (due to temporary connection loss, discord requesting a reconnect, etc.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about allowing users to pass a CancellationToken in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a default request timeout and a way for users to supply their own cancellation token. |
||
{ | ||
tcs.TrySetResult(true); | ||
} | ||
} | ||
else | ||
{ | ||
|
@@ -2904,7 +2921,7 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty | |
} | ||
break; | ||
#endregion | ||
|
||
#region Auto Moderation | ||
|
||
case "AUTO_MODERATION_RULE_CREATE": | ||
|
Uh oh!
There was an error while loading. Please reload this page.