Skip to content

Commit

Permalink
[Feature] Bulk ban support (#2881)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 committed Mar 15, 2024
1 parent 3331614 commit 03402cd
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/BulkBanResult.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
11 changes: 11 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,5 +1418,16 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// 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);
}
}
12 changes: 12 additions & 0 deletions src/Discord.Net.Rest/API/Common/BulkBanResult.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions src/Discord.Net.Rest/API/Rest/BulkBanParams.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
17 changes: 17 additions & 0 deletions src/Discord.Net.Rest/DiscordRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,23 @@ public Task RemoveGuildBanAsync(ulong guildId, ulong userId, RequestOptions opti
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
Expand Down
7 changes: 7 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ public static async Task<RestBan> GetBanAsync(IGuild guild, BaseDiscordClient cl

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
Expand Down
4 changes: 4 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ public Task RemoveBanAsync(IUser user, RequestOptions options = null)
/// <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
Expand Down
4 changes: 4 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,10 @@ public Task RemoveBanAsync(IUser user, RequestOptions options = null)
/// <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
Expand Down

0 comments on commit 03402cd

Please sign in to comment.