Skip to content

Commit

Permalink
Added CombinedBulkBanReponse
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherZane committed Jul 28, 2024
1 parent 73a8ed0 commit 932d188
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Linq;

namespace Disqord.Rest;

public class CombinedBulkBanResponse : IBulkBanResponse
{
public IReadOnlyList<Snowflake> BannedUserIds { get; }
public IReadOnlyList<Snowflake> FailedUserIds { get; }

public CombinedBulkBanResponse(IEnumerable<IBulkBanResponse> responses)
{
BannedUserIds = responses.SelectMany(response => response.BannedUserIds).ToArray();
FailedUserIds = responses.SelectMany(response => response.FailedUserIds).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static IPagedEnumerable<IBulkBanResponse> EnumerateBanCreation(this IGuil
return client.EnumerateBanCreation(guild.Id, userIds, reason, deleteMessageTime, options);
}

public static Task CreateBansAsync(this IGuild guild,
public static Task<IBulkBanResponse> CreateBansAsync(this IGuild guild,
IEnumerable<Snowflake> userIds, string? reason = null, TimeSpan? deleteMessageTime = null,
IRestRequestOptions? options = null, CancellationToken cancellationToken = default)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Disqord.Rest/Extensions/RestClientExtensions.Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public static IPagedEnumerable<IBulkBanResponse> EnumerateBanCreation(this IRest
}, (client, guildId, userIds.ToArray(), reason, deleteMessageTime, options));
}

public static async Task<IReadOnlyList<IBulkBanResponse>> CreateBansAsync(this IRestClient client,
public static async Task<IBulkBanResponse> CreateBansAsync(this IRestClient client,
Snowflake guildId, IEnumerable<Snowflake> userIds, string? reason = null, TimeSpan? deleteMessageTime = null,
IRestRequestOptions? options = null, CancellationToken cancellationToken = default)
{
Expand All @@ -497,12 +497,12 @@ public static async Task<IReadOnlyList<IBulkBanResponse>> CreateBansAsync(this I

if (users.Length <= Discord.Limits.Guild.MaxBulkBanUsersAmount)
{
var response = await client.InternalCreateBansAsync(guildId, users, reason, deleteMessageTime, options, cancellationToken).ConfigureAwait(false);
return new[] { response };
return await client.InternalCreateBansAsync(guildId, users, reason, deleteMessageTime, options, cancellationToken).ConfigureAwait(false);
}

var enumerable = client.EnumerateBanCreation(guildId, users, reason, deleteMessageTime, options);
return await enumerable.FlattenAsync(cancellationToken);
var flattened = await enumerable.FlattenAsync(cancellationToken);
return new CombinedBulkBanResponse(flattened);
}

internal static async Task<IBulkBanResponse> InternalCreateBansAsync(this IRestClient client,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Qommon.Collections.ReadOnly;

namespace Disqord.Rest;

Expand Down Expand Up @@ -37,6 +39,6 @@ protected override async Task<IReadOnlyList<IBulkBanResponse>> NextPageAsync(IRe
_offset += amount;
var response = await Client.InternalCreateBansAsync(_guildId, segment, _reason, _deleteMessagesTime, options,
cancellationToken);
return new []{response};
return Enumerable.Repeat(response, PageSize).ToReadOnlyList();
}
}
21 changes: 11 additions & 10 deletions src/Disqord.Rest/Requests/Pagination/PagedEnumerator`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,22 @@ public async ValueTask<bool> MoveNextAsync(IRestRequestOptions? options = null)
return false;
}

if (current.Count < PageSize)
{
// If Discord returns less entities than the page size,
// it means there are no more entities beyond the ones we just received.
RemainingCount = 0;
}
else
{
// TODO: Rework paged enumerator
// if (current.Count < PageSize)
// {
// // If Discord returns less entities than the page size,
// // it means there are no more entities beyond the ones we just received.
// RemainingCount = 0;
// }
// else
// {
RemainingCount -= current.Count;
}
// }

return true;
}

/// <inheritdoc/>
public virtual ValueTask DisposeAsync()
=> default;
}
}

0 comments on commit 932d188

Please sign in to comment.