[Feature] Bulk ban support (#2881)
This commit is contained in:
25
src/Discord.Net.Core/Entities/Guilds/BulkBanResult.cs
Normal file
25
src/Discord.Net.Core/Entities/Guilds/BulkBanResult.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1418,5 +1418,16 @@ namespace Discord
|
|||||||
/// A task that represents the asynchronous creation operation. The task result contains the modified <see cref="IncidentsData"/>.
|
/// A task that represents the asynchronous creation operation. The task result contains the modified <see cref="IncidentsData"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
Task<GuildIncidentsData> ModifyIncidentActionsAsync(Action<GuildIncidentsDataProperties> props, RequestOptions options = null);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/Discord.Net.Rest/API/Common/BulkBanResult.cs
Normal file
12
src/Discord.Net.Rest/API/Common/BulkBanResult.cs
Normal 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; }
|
||||||
|
}
|
||||||
12
src/Discord.Net.Rest/API/Rest/BulkBanParams.cs
Normal file
12
src/Discord.Net.Rest/API/Rest/BulkBanParams.cs
Normal 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; }
|
||||||
|
}
|
||||||
@@ -1771,6 +1771,23 @@ namespace Discord.API
|
|||||||
var ids = new BucketIds(guildId: guildId);
|
var ids = new BucketIds(guildId: guildId);
|
||||||
return SendAsync("DELETE", () => $"guilds/{guildId}/bans/{userId}", ids, options: options);
|
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
|
#endregion
|
||||||
|
|
||||||
#region Guild Widget
|
#region Guild Widget
|
||||||
|
|||||||
@@ -244,6 +244,13 @@ namespace Discord.Rest
|
|||||||
|
|
||||||
public static Task RemoveBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, RequestOptions options)
|
public static Task RemoveBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, RequestOptions options)
|
||||||
=> client.ApiClient.RemoveGuildBanAsync(guild.Id, userId, 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
|
#endregion
|
||||||
|
|
||||||
#region Channels
|
#region Channels
|
||||||
|
|||||||
@@ -401,6 +401,10 @@ namespace Discord.Rest
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
|
public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
|
||||||
=> GuildHelper.RemoveBanAsync(this, Discord, userId, options);
|
=> 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
|
#endregion
|
||||||
|
|
||||||
#region Channels
|
#region Channels
|
||||||
|
|||||||
@@ -722,6 +722,10 @@ namespace Discord.WebSocket
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
|
public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
|
||||||
=> GuildHelper.RemoveBanAsync(this, Discord, userId, options);
|
=> 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
|
#endregion
|
||||||
|
|
||||||
#region Channels
|
#region Channels
|
||||||
|
|||||||
Reference in New Issue
Block a user