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 Apr 2, 2024
1 parent d752eed commit 41aaffe
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 2 deletions.
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 users 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).mapToObj(Long::toUnsignedString).collect(Collectors.toList()),
0, TimeUnit.SECONDS, null);
}

/**
* Bans the given users 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.asList(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.
* @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,45 @@
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.Collections;
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;

/**
* The user ids that could not be banned.
*/
private final Set<Long> failedUserIds;

/**
* Create a new BulkBanResponseImpl.
*
* @param data The json data about this response.
*/
public BulkBanResponseImpl(JsonNode data) {
Set<Long> bannedUsers = new LinkedHashSet<>();
Set<Long> failedUsers = new LinkedHashSet<>();
data.get("banned_users").forEach(bannedUser -> bannedUsers.add(bannedUser.asLong()));
data.get("failed_users").forEach(failedUser -> failedUsers.add(failedUser.asLong()));
bannedUserIds = Collections.unmodifiableSet(bannedUsers);
failedUserIds = Collections.unmodifiableSet(failedUsers);
}

@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
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ public enum RestRequestResultErrorCode {
"Webhooks posted to forum channels cannot have both a thread_name and thread_id"),
WEBHOOKS_CAN_ONLY_CREATE_THREADS_IN_FORUM_CHANNELS(220003, "Webhooks can only create threads in forum channels"),
WEBHOOK_SERVICES_CANNOT_BE_USED_IN_FORUM_CHANNELS(220004, "Webhook services cannot be used in forum channels"),
MESSAGE_BLOCKED_BY_HARMFUL_LINKS_FILTER(240000, "Message blocked by harmful links filter");
MESSAGE_BLOCKED_BY_HARMFUL_LINKS_FILTER(240000, "Message blocked by harmful links filter"),
FAILED_TO_BAN_USERS(500000, "Failed to ban users");

/**
* A map for retrieving the enum instances by code.
Expand Down

0 comments on commit 41aaffe

Please sign in to comment.