diff --git a/lib/discordrb/api/server.rb b/lib/discordrb/api/server.rb index 135ea0c9b..f59317e29 100644 --- a/lib/discordrb/api/server.rb +++ b/lib/discordrb/api/server.rb @@ -570,4 +570,19 @@ def add_member(token, server_id, user_id, access_token, nick = nil, roles = [], Authorization: token ) end + + # Ban multiple users in one go + # https://discord.com/developers/docs/resources/guild#bulk-guild-ban + def bulk_ban(token, server_id, users, message_seconds, reason = nil) + Discordrb::API.request( + :guilds_sid_bulk_bans, + server_id, + :post, + "#{Discordrb::API.api_base}/guilds/#{server_id}/bulk-ban", + { user_ids: users, delete_message_seconds: message_seconds }.compact.to_json, + content_type: :json, + Authorization: token, + 'X-Audit-Log-Reason': reason + ) + end end diff --git a/lib/discordrb/commands/command_bot.rb b/lib/discordrb/commands/command_bot.rb index ddf68ea37..2f315628b 100644 --- a/lib/discordrb/commands/command_bot.rb +++ b/lib/discordrb/commands/command_bot.rb @@ -274,7 +274,7 @@ def arg_check(args, types = nil, server = nil) rescue ArgumentError nil end - elsif types[i] == TrueClass || types[i] == FalseClass + elsif [TrueClass, FalseClass].include?(types[i]) if arg.casecmp('true').zero? || arg.downcase.start_with?('y') true elsif arg.casecmp('false').zero? || arg.downcase.start_with?('n') diff --git a/lib/discordrb/data/server.rb b/lib/discordrb/data/server.rb index 8a6f2a062..2bfb24d2c 100644 --- a/lib/discordrb/data/server.rb +++ b/lib/discordrb/data/server.rb @@ -630,6 +630,20 @@ def unban(user, reason = nil) API::Server.unban_user(@bot.token, @id, user.resolve_id, reason) end + # Ban up to 200 users from this server in one go. + # @param users [Array] Array of up to 200 users to ban. + # @param message_seconds [Integer] How many seconds of messages sent by the users should be deleted. + # @param reason [String] The reason these users are being banned. + # @return [nil, BulkBan] nil if only a single user was provided or a bulk ban object otherwise. + def bulk_ban(users, message_seconds: 0, reason: nil) + raise ArgumentError, 'Can only ban between 1 and 200 users!' unless users.size.between?(1, 200) + + return ban(users.first, 0, message_seconds: message_seconds, reason: reason) if users.size == 1 + + response = API::Server.bulk_ban(@bot.token, @id, users.map(&:resolve_id), message_seconds, reason) + BulkBan.new(JSON.parse(response), self, reason) + end + # Kicks a user from this server. # @param user [User, String, Integer] The user to kick. # @param reason [String] The reason the user is being kicked. @@ -1019,4 +1033,26 @@ def remove(reason = nil) alias_method :unban, :remove alias_method :lift, :remove end + + # A bulk ban entry on a server + class BulkBan + # @return [Server] The server this bulk ban belongs to. + attr_reader :server + + # @return [String, nil] The reason these users were banned. + attr_reader :reason + + # @return [Array] Array of IDs of banned users. + attr_reader :banned_users + + # @return [Array] Array of IDs of users who couldn't be banned. + attr_reader :failed_users + + def initialize(data, server, reason) + @server = server + @reason = reason + @banned_users = data['banned_users'].map(&:resolve_id) + @failed_users = data['failed_users'].map(&:resolve_id) + end + end end diff --git a/lib/discordrb/errors.rb b/lib/discordrb/errors.rb index c5fd1f96b..ee9b56006 100644 --- a/lib/discordrb/errors.rb +++ b/lib/discordrb/errors.rb @@ -174,6 +174,9 @@ def self.error_class_for(code) # Unauthorized Unauthorized = Unauthorised = Code(40_001) + # Unable to bulk ban any members + UnableToBulkBanMembers = Code(500_000) + # Missing Access MissingAccess = Code(50_001)