Fix flawed bulk message deletion (#872)
* Fix flawed bulk message deletion https://github.com/RogueException/Discord.Net/issues/871, consider changing DeleteMessagesParams.MessageIds type to I(Readonly)List<ulong> or IEnumerable<ulong> to avoid unnecessary copying (batch.ToArray()) * Update code formatting
This commit is contained in:
committed by
Christopher F
parent
cd82a0f70c
commit
804d9188e7
@@ -188,22 +188,28 @@ namespace Discord.Rest
|
|||||||
public static async Task DeleteMessagesAsync(ITextChannel channel, BaseDiscordClient client,
|
public static async Task DeleteMessagesAsync(ITextChannel channel, BaseDiscordClient client,
|
||||||
IEnumerable<ulong> messageIds, RequestOptions options)
|
IEnumerable<ulong> messageIds, RequestOptions options)
|
||||||
{
|
{
|
||||||
|
const int BATCH_SIZE = 100;
|
||||||
|
|
||||||
var msgs = messageIds.ToArray();
|
var msgs = messageIds.ToArray();
|
||||||
if (msgs.Length < 100)
|
int batches = msgs.Length / BATCH_SIZE;
|
||||||
|
for (int i = 0; i <= batches; i++)
|
||||||
{
|
{
|
||||||
var args = new DeleteMessagesParams(msgs);
|
ArraySegment<ulong> batch;
|
||||||
await client.ApiClient.DeleteMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
|
if (i < batches)
|
||||||
|
{
|
||||||
|
batch = new ArraySegment<ulong>(msgs, i * BATCH_SIZE, BATCH_SIZE);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var batch = new ulong[100];
|
batch = new ArraySegment<ulong>(msgs, i * BATCH_SIZE, msgs.Length - batches * BATCH_SIZE);
|
||||||
for (int i = 0; i < (msgs.Length + 99) / 100; i++)
|
if (batch.Count == 0)
|
||||||
{
|
{
|
||||||
Array.Copy(msgs, i * 100, batch, 0, Math.Min(msgs.Length - (100 * i), 100));
|
break;
|
||||||
var args = new DeleteMessagesParams(batch);
|
|
||||||
await client.ApiClient.DeleteMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var args = new DeleteMessagesParams(batch.ToArray());
|
||||||
|
await client.ApiClient.DeleteMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Permission Overwrites
|
//Permission Overwrites
|
||||||
|
|||||||
Reference in New Issue
Block a user