Skip to content

Commit

Permalink
Bulk banning
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC committed Mar 18, 2024
1 parent f558ad3 commit 9b42887
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@ export enum JSONErrorCodes {
GUILD_JOIN_INVITE_LIMITED_ACCESS = 400002,
GUILD_GO_LIVE_LIMITED_ACCESS = 400003,
GUILD_LIMITED_ACCESS_MAX = 409999,
FAILED_TO_BAN_USERS = 500000,


/** @deprecated Use {@link Constants~JSONErrorCodes.SLOWMODE_RATE_LIMITED | SLOWMODE_RATE_LIMITED } instead. This will be removed in `1.10.0`. */
Expand Down
31 changes: 30 additions & 1 deletion lib/routes/Guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ import type {
EditOnboardingOptions,
EditIncidentActionsOptions,
IncidentActions,
RawIncidentActions
RawIncidentActions,
BulkBanOptions,
BulkBanResponse,
RawBulkBanResponse
} from "../types/guilds";
import * as Routes from "../util/Routes";
import type { CreateAutoModerationRuleOptions, EditAutoModerationRuleOptions, RawAutoModerationRule } from "../types/auto-moderation";
Expand Down Expand Up @@ -160,6 +163,32 @@ export default class Guilds {
}).then(data => data.pruned);
}

/**
* Ban up to 200 members from a guild. This requires both the `BAN_MEMBERS` and `MANAGE_GUILD` permissions.
* If no members were banned, a {@link Constants~JSONErrorCodes.FAILED_TO_BAN_USERS | FAILED_TO_BAN_USERS } will be returned.
* The bot user is ignored.
* @param guildID The ID of the guild.
* @param options The options for banning.
*/
async bulkBan(guildID: string, options: BulkBanOptions): Promise<BulkBanResponse> {
const reason = options?.reason;
if (options?.reason) {
delete options.reason;
}
return this.#manager.authRequest<RawBulkBanResponse>({
method: "POST",
path: Routes.GUILD_BULK_BAN(guildID),
json: {
delete_message_seconds: options.deleteMessageSeconds,
users_ids: options.userIDs
},
reason
}).then(data => ({
bannedUsers: data.banned_users,
failedUsers: data.failed_users
}));
}

/**
* Create a guild. This can only be used by bots in under 10 guilds.
* @param options The options for creating the guild.
Expand Down
14 changes: 13 additions & 1 deletion lib/structures/Guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ import type {
RawSticker,
InventorySettings,
EditIncidentActionsOptions,
IncidentActions
IncidentActions,
BulkBanOptions,
BulkBanResponse
} from "../types/guilds";
import type {
CreateScheduledEventOptions,
Expand Down Expand Up @@ -718,6 +720,16 @@ export default class Guild extends Base {
return this.client.rest.guilds.beginPrune(this.id, options);
}

/**
* Ban up to 200 members from this guild. This requires both the `BAN_MEMBERS` and `MANAGE_GUILD` permissions.
* If no members were banned, a {@link Constants~JSONErrorCodes.FAILED_TO_BAN_USERS | FAILED_TO_BAN_USERS } will be returned.
* The bot user is ignored.
* @param options The options for banning.
*/
async bulkBan(options: BulkBanOptions): Promise<BulkBanResponse> {
return this.client.rest.guilds.bulkBan(this.id, options);
}

/**
* Create an auto moderation rule for this guild.
* @param options The options for the rule.
Expand Down
20 changes: 20 additions & 0 deletions lib/types/guilds.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,3 +832,23 @@ export interface IncidentActions {
/** When disabled invites will expire (up to 24 hours in the future). */
invitesDisabledUntil: string | null;
}

export interface BulkBanOptions {
/** Number of seconds to deleted messages for, between 0 and 604800 (7 days). */
deleteMessageSeconds?: number;
reason?: string;
/** list of user ids to ban (max 200) */
userIDs: Array<string>;
}

export interface RawBulkBanResponse {
banned_users: Array<string>;
failed_users: Array<string>;
}

export interface BulkBanResponse {
/** The users that were successfully banned. */
bannedUsers: Array<string>;
/** The users that could not be banned. */
failedUsers: Array<string>;
}
1 change: 1 addition & 0 deletions lib/util/Routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const GUILD_STICKER = (guildID: string, stickerID: string) => `/gu
export const GUILD_STICKERS = (guildID: string) => `/guilds/${guildID}/stickers` as const;
export const GUILD_ONBOARDING = (guildID: string) => `/guilds/${guildID}/onboarding` as const;
export const GUILD_INCIDENT_ACTIONS = (guildID: string) => `/guilds/${guildID}/incident-actions` as const;
export const GUILD_BULK_BAN = (guildID: string) => `/guilds/${guildID}/bulk-ban` as const;

// Channels
export const CHANNEL = (channelID: string) => `/channels/${channelID}` as const;
Expand Down

0 comments on commit 9b42887

Please sign in to comment.