Skip to content

Commit

Permalink
bettah bulk bans (#2915)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 committed Apr 22, 2024
1 parent 27a0785 commit f85bf9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/Discord.Net.Core/DiscordConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,10 @@ public class DiscordConfig
/// Returns the maximum number of entitlements that can be gotten per-batch.
/// </summary>
public const int MaxEntitlementsPerBatch = 100;

/// <summary>
/// Returns the maximum number of bans that can be banned in a single bulk request.
/// </summary>
public const int MaxBansPerBulkBatch = 200;
}
}
19 changes: 15 additions & 4 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,20 @@ public static Task RemoveBanAsync(IGuild guild, BaseDiscordClient client, ulong

public static async Task<BulkBanResult> BulkBanAsync(IGuild guild, BaseDiscordClient client, ulong[] userIds, int? deleteMessageSeconds, RequestOptions options)
{
var model = await client.ApiClient.BulkBanAsync(guild.Id, userIds, deleteMessageSeconds, options);
return new(model.BannedUsers?.ToImmutableArray() ?? ImmutableArray<ulong>.Empty,
model.FailedUsers?.ToImmutableArray() ?? ImmutableArray<ulong>.Empty);
var pos = 0;
var banned = new List<ulong>(userIds.Length);
var failed = new List<ulong>();
while (pos * DiscordConfig.MaxBansPerBulkBatch < userIds.Length)
{
var toBan = userIds
.Skip(pos * DiscordConfig.MaxBansPerBulkBatch)
.Take(DiscordConfig.MaxBansPerBulkBatch);
pos++;
var model = await client.ApiClient.BulkBanAsync(guild.Id, toBan.ToArray(), deleteMessageSeconds, options);
banned.AddRange(model.BannedUsers ?? []);
failed.AddRange(model.FailedUsers ?? []);
}
return new(banned.ToImmutableArray(), failed.ToImmutableArray());
}
#endregion

Expand Down Expand Up @@ -620,7 +631,7 @@ public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClie

var createGuildRoleParams = new API.Rest.ModifyGuildRoleParams
{
Color = color?.RawValue ?? Optional.Create<uint>(),
Color = color?.RawValue ?? Optional.Create<uint>(),
Hoist = isHoisted,
Mentionable = isMentionable,
Name = name,
Expand Down

0 comments on commit f85bf9a

Please sign in to comment.