From 577afc8d7f465ccec6396c0909c2328c54a4ded3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Conor=E2=84=A2?= <35053522+iiFDCT@users.noreply.github.com> Date: Sun, 22 Sep 2024 09:11:22 +0100 Subject: [PATCH] feat: Support bulk banning (#1541) --- index.d.ts | 9 +++++++++ lib/Client.js | 17 +++++++++++++++++ lib/rest/Endpoints.js | 1 + lib/structures/Guild.js | 12 ++++++++++++ 4 files changed, 39 insertions(+) diff --git a/index.d.ts b/index.d.ts index c4e21b696..18c9cdeec 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1025,6 +1025,13 @@ declare namespace Eris { deleteMessageSeconds?: number; reason?: string; } + interface BulkBanMembersOptions extends Omit { + userIDs: string[]; + } + interface BulkBanMembersResponse { + banned_users: string[]; + failed_users: string[]; + } interface CreateGuildOptions { afkChannelID?: string; afkTimeout?: number; @@ -2055,6 +2062,7 @@ declare namespace Eris { banGuildMember(guildID: string, userID: string, options?: BanMemberOptions): Promise; /** @deprecated */ banGuildMember(guildID: string, userID: string, deleteMessageDays?: number, reason?: string): Promise; + bulkBanGuildMembers(guildID: string, options: BulkBanMembersOptions): Promise; bulkEditCommands(commands: ApplicationCommandBulkEditOptions[]): Promise[]>; bulkEditGuildCommands(guildID: string, commands: ApplicationCommandBulkEditOptions[]): Promise[]>; closeVoiceConnection(guildID: string): void; @@ -2530,6 +2538,7 @@ declare namespace Eris { banMember(userID: string, options?: BanMemberOptions): Promise; /** @deprecated */ banMember(userID: string, deleteMessageDays?: number, reason?: string): Promise; + bulkBanMembers(options: BulkBanMembersOptions): Promise; bulkEditCommands(commands: ApplicationCommandBulkEditOptions[]): Promise[]>; createAutoModerationRule(rule: CreateAutoModerationRuleOptions): Promise; createChannel(name: string): Promise; diff --git a/lib/Client.js b/lib/Client.js index 64f461994..834ee1bb1 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -351,6 +351,23 @@ class Client extends EventEmitter { }); } + /** + * Bulk ban users from a guild - Note: Requires both BAN_MEMBERS and MANAGE_GUILD permissions + * @arg {String} guildID The ID of the guild + * @arg {Object} options Options for banning the users + * @arg {Array} options.userIDs Array of user IDs to ban + * @arg {Number} [options.deleteMessageSeconds=0] Number of seconds to delete messages for each user, between 0 and 604800 inclusive + * @arg {String} [options.reason] The reason to be displayed in audit logs + * @returns {Promise} An object with 2 arrays - `banned_users` with the IDs of the successfully banned users, and `failed_users` with the IDs of any users who could not be banned or were already banned + */ + bulkBanGuildMembers(guildID, options) { + return this.requestHandler.request("POST", Endpoints.GUILD_BULK_BAN(guildID), true, { + user_ids: options.userIDs, + delete_message_seconds: options.deleteMessageSeconds || 0, + reason: options.reason + }) + } + /** * Bulk create/edit global application commands * @arg {Array} commands An array of command objects diff --git a/lib/rest/Endpoints.js b/lib/rest/Endpoints.js index c3732d674..4fc8ff383 100644 --- a/lib/rest/Endpoints.js +++ b/lib/rest/Endpoints.js @@ -39,6 +39,7 @@ module.exports.GUILD = (guildID) module.exports.GUILD_AUDIT_LOGS = (guildID) => `/guilds/${guildID}/audit-logs`; module.exports.GUILD_BAN = (guildID, memberID) => `/guilds/${guildID}/bans/${memberID}`; module.exports.GUILD_BANS = (guildID) => `/guilds/${guildID}/bans`; +module.exports.GUILD_BULK_BAN = (guildID) => `/guilds/${guildID}/bulk-ban`; module.exports.GUILD_CHANNELS = (guildID) => `/guilds/${guildID}/channels`; module.exports.GUILD_COMMAND = (applicationID, guildID, commandID) => `/applications/${applicationID}/guilds/${guildID}/commands/${commandID}`; module.exports.GUILD_COMMAND_PERMISSIONS = (applicationID, guildID) => `/applications/${applicationID}/guilds/${guildID}/commands/permissions`; diff --git a/lib/structures/Guild.js b/lib/structures/Guild.js index 567217885..20f285b95 100644 --- a/lib/structures/Guild.js +++ b/lib/structures/Guild.js @@ -391,6 +391,18 @@ class Guild extends Base { return this._client.banGuildMember.call(this._client, this.id, userID, options, reason); } + /** + * Bulk ban users from the guild - Note: Requires both BAN_MEMBERS and MANAGE_GUILD permissions + * @arg {Object} options Options for banning the users + * @arg {Array} options.userIDs Array of user IDs to ban + * @arg {Number} [options.deleteMessageSeconds=0] Number of seconds to delete messages for each user, between 0 and 604800 inclusive + * @arg {String} [options.reason] The reason to be displayed in audit logs + * @returns {Promise} An object with 2 arrays - `banned_users` with the IDs of the successfully banned users, and `failed_users` with the IDs of any users who could not be banned or were already banned + */ + bulkBanMembers(options) { + return this._client.bulkBanGuildMembers.call(this._client, this.id, options); + } + /** * Bulk create/edit the guild application commands * @arg {Array} commands An array of command objects