Skip to content

Commit

Permalink
Add bulk ban support
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdennnoch committed Mar 19, 2024
1 parent d752eed commit 989d8d5
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.javacord.api.entity.server;

import java.util.Collection;

/**
* The response of a bulk ban request.
*/
public interface BulkBanResponse {

/**
* Gets the user ids that were banned successfully.
*
* @return The banned user ids.
*/
Collection<Long> getBannedUserIds();

/**
* Gets the user ids that could not be banned.
*
* @return The failed user ids.
*/
Collection<Long> getFailedUserIds();
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
* The class represents a Discord server, sometimes also called guild.
Expand Down Expand Up @@ -2282,6 +2283,55 @@ default CompletableFuture<Void> banUser(long userId, Duration duration, String r
return banUser(Long.toUnsignedString(userId), duration, reason);
}

/**
* Bans the given user from the server.
*
* @param userIds The ids of the users to ban.
* @return A future returning details about the bulk ban.
*/
default CompletableFuture<BulkBanResponse> bulkBanUsers(long... userIds) {
return bulkBanUsers(
LongStream.of(userIds).boxed().map(Long::toUnsignedString).collect(Collectors.toList()),
0, TimeUnit.SECONDS, null);
}

/**
* Bans the given user from the server.
*
* @param userIds The ids of the users to ban.
* @return A future returning details about the bulk ban.
*/
default CompletableFuture<BulkBanResponse> bulkBanUsers(String... userIds) {
return bulkBanUsers(Arrays.stream(userIds).collect(Collectors.toList()), 0, TimeUnit.SECONDS, null);
}

/**
* Bans the given user from the server.
*
* @param userIds The ids of the users to ban.
* @param deleteMessageDuration The number of messages to delete within the duration.
* (Between 0 and 604800 seconds (7 days))
* @param unit The unit of time for the duration.
* @return A future returning details about the bulk ban.
*/
default CompletableFuture<BulkBanResponse> bulkBanUsers(Collection<String> userIds, long deleteMessageDuration,
TimeUnit unit) {
return bulkBanUsers(userIds, 0, TimeUnit.SECONDS, null);
}

/**
* Bans the given users from the server.
*
* @param userIds The ids of the users to ban.
* @param deleteMessageDuration The number of messages to delete within the duration.
* (Between 0 and 604800 seconds (7 days))
* @param unit The unit of time for the duration.
* @param reason The Audit Log reason for the bulk ban.
* @return A future returning details about the bulk ban.
*/
CompletableFuture<BulkBanResponse> bulkBanUsers(Collection<String> userIds, long deleteMessageDuration,
TimeUnit unit, String reason);

/**
* Unbans the given user from the server.
*
Expand Down Expand Up @@ -3845,4 +3895,4 @@ default Optional<RegularServerChannel> getWidgetChannel() {
* @return The system channel flags for this server.
*/
EnumSet<SystemChannelFlag> getSystemChannelFlags();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.javacord.core.entity.server;

import com.fasterxml.jackson.databind.JsonNode;
import org.javacord.api.entity.server.BulkBanResponse;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

public class BulkBanResponseImpl implements BulkBanResponse {

/**
* The user ids that were banned successfully.
*/
private final Set<Long> bannedUserIds = new LinkedHashSet<>();

/**
* The user ids that could not be banned.
*/
private final Set<Long> failedUserIds = new LinkedHashSet<>();

/**
* Create a new BulkBanResponseImpl.
*
* @param data The json data about this response.
*/
public BulkBanResponseImpl(JsonNode data) {
data.get("banned_users").forEach(bannedUser -> bannedUserIds.add(bannedUser.asLong()));
data.get("failed_users").forEach(failedUser -> failedUserIds.add(failedUser.asLong()));
}

@Override
public Collection<Long> getBannedUserIds() {
return bannedUserIds;
}

@Override
public Collection<Long> getFailedUserIds() {
return failedUserIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.logging.log4j.Logger;
import org.javacord.api.DiscordApi;
import org.javacord.api.Javacord;
Expand Down Expand Up @@ -35,6 +36,7 @@
import org.javacord.api.entity.server.ActiveThreads;
import org.javacord.api.entity.server.Ban;
import org.javacord.api.entity.server.BoostLevel;
import org.javacord.api.entity.server.BulkBanResponse;
import org.javacord.api.entity.server.DefaultMessageNotificationLevel;
import org.javacord.api.entity.server.ExplicitContentFilterLevel;
import org.javacord.api.entity.server.MultiFactorAuthenticationLevel;
Expand Down Expand Up @@ -1746,6 +1748,27 @@ public CompletableFuture<Void> banUser(String userId, long deleteMessageDuration
return request.execute(result -> null);
}

@Override
public CompletableFuture<BulkBanResponse> bulkBanUsers(
Collection<String> userIds,
long deleteMessageDuration,
TimeUnit unit,
String reason
) {
ObjectNode body = JsonNodeFactory.instance.objectNode();
ArrayNode users = body.putArray("user_ids");
userIds.forEach(users::add);
if (deleteMessageDuration > 0) {
body.put("delete_message_seconds", unit.toSeconds(deleteMessageDuration));
}

return new RestRequest<BulkBanResponse>(getApi(), RestMethod.POST, RestEndpoint.BULK_BAN)
.setUrlParameters(getIdAsString())
.setAuditLogReason(reason)
.setBody(body)
.execute(result -> new BulkBanResponseImpl(result.getJsonBody()));
}

@Override
public CompletableFuture<Void> unbanUser(long userId, String reason) {
return new RestRequest<Void>(getApi(), RestMethod.DELETE, RestEndpoint.BAN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum RestEndpoint {
WEBHOOK_MESSAGE("/webhooks/%s/%s/messages/%s",0),
INVITE("/invites/%s"),
BAN("/guilds/%s/bans", 0),
BULK_BAN("/guilds/%s/bulk-ban", 0),
CURRENT_USER("/users/@me"),
AUDIT_LOG("/guilds/%s/audit-logs", 0),
CUSTOM_EMOJI("/guilds/%s/emojis", 0),
Expand Down

0 comments on commit 989d8d5

Please sign in to comment.