[Feature] Bulk ban support (#2881)

This commit is contained in:
Mihail Gribkov
2024-03-16 01:22:23 +03:00
committed by GitHub
parent 3331614a7a
commit 03402cd4d2
8 changed files with 92 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
using System.Collections.Generic;
namespace Discord;
/// <summary>
/// Represents a result of a bulk ban.
/// </summary>
public readonly struct BulkBanResult
{
/// <summary>
/// Gets the collection of user IDs that were successfully banned.
/// </summary>
public IReadOnlyCollection<ulong> BannedUsers { get; }
/// <summary>
/// Gets the collection of user IDs that failed to be banned.
/// </summary>
public IReadOnlyCollection<ulong> FailedUsers { get; }
internal BulkBanResult(IReadOnlyCollection<ulong> bannedUsers, IReadOnlyCollection<ulong> failedUsers)
{
BannedUsers = bannedUsers;
FailedUsers = failedUsers;
}
}

View File

@@ -1418,5 +1418,16 @@ namespace Discord
/// A task that represents the asynchronous creation operation. The task result contains the modified <see cref="IncidentsData"/>.
/// </returns>
Task<GuildIncidentsData> ModifyIncidentActionsAsync(Action<GuildIncidentsDataProperties> props, RequestOptions options = null);
/// <summary>
/// Executes a bulk ban on the specified users.
/// </summary>
/// <param name="userIds">A collection of user ids to ban.</param>
/// <param name="deleteMessageSeconds">The number of seconds to delete messages for. Max 604800.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains a <see cref="BulkBanResult"/>.
/// </returns>
Task<BulkBanResult> BulkBanAsync(IEnumerable<ulong> userIds, int? deleteMessageSeconds = null, RequestOptions options = null);
}
}

View File

@@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace Discord.API;
internal class BulkBanResult
{
[JsonProperty("banned_users")]
public ulong[] BannedUsers { get; set; }
[JsonProperty("failed_users")]
public ulong[] FailedUsers { get; set; }
}

View File

@@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace Discord.API.Rest;
internal class BulkBanParams
{
[JsonProperty("user_ids")]
public ulong[] UserIds { get; set; }
[JsonProperty("delete_message_seconds")]
public Optional<int> DeleteMessageSeconds { get; set; }
}

View File

@@ -1771,6 +1771,23 @@ namespace Discord.API
var ids = new BucketIds(guildId: guildId);
return SendAsync("DELETE", () => $"guilds/{guildId}/bans/{userId}", ids, options: options);
}
public Task<BulkBanResult> BulkBanAsync(ulong guildId, ulong[] userIds, int? deleteMessagesSeconds = null, RequestOptions options = null)
{
Preconditions.NotEqual(userIds.Length, 0, nameof(userIds));
Preconditions.AtMost(userIds.Length, 200, nameof(userIds));
Preconditions.AtMost(deleteMessagesSeconds ?? 0, 604800, nameof(deleteMessagesSeconds));
options = RequestOptions.CreateOrClone(options);
var data = new BulkBanParams
{
DeleteMessageSeconds = deleteMessagesSeconds ?? Optional<int>.Unspecified,
UserIds = userIds
};
return SendJsonAsync<BulkBanResult>("POST", () => $"guilds/{guildId}/bulk-ban", data, new BucketIds(guildId), options: options);
}
#endregion
#region Guild Widget

View File

@@ -244,6 +244,13 @@ namespace Discord.Rest
public static Task RemoveBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, RequestOptions options)
=> client.ApiClient.RemoveGuildBanAsync(guild.Id, userId, options);
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);
}
#endregion
#region Channels

View File

@@ -401,6 +401,10 @@ namespace Discord.Rest
/// <inheritdoc />
public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
=> GuildHelper.RemoveBanAsync(this, Discord, userId, options);
/// <inheritdoc />
public Task<BulkBanResult> BulkBanAsync(IEnumerable<ulong> userIds, int? deleteMessageSeconds = null, RequestOptions options = null)
=> GuildHelper.BulkBanAsync(this, Discord, userIds.ToArray(), deleteMessageSeconds, options);
#endregion
#region Channels

View File

@@ -722,6 +722,10 @@ namespace Discord.WebSocket
/// <inheritdoc />
public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
=> GuildHelper.RemoveBanAsync(this, Discord, userId, options);
/// <inheritdoc />
public Task<BulkBanResult> BulkBanAsync(IEnumerable<ulong> userIds, int? deleteMessageSeconds = null, RequestOptions options = null)
=> GuildHelper.BulkBanAsync(this, Discord, userIds.ToArray(), deleteMessageSeconds, options);
#endregion
#region Channels