Skip to content

Commit

Permalink
Bulk Banning
Browse files Browse the repository at this point in the history
  • Loading branch information
Droid00000 committed Feb 17, 2025
1 parent b27eb76 commit 413d489
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/discordrb/api/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/discordrb/commands/command_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
36 changes: 36 additions & 0 deletions lib/discordrb/data/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, String, Integer>] 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.
Expand Down Expand Up @@ -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<Integer>] Array of IDs of banned users.
attr_reader :banned_users

# @return [Array<Integer>] 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
3 changes: 3 additions & 0 deletions lib/discordrb/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 413d489

Please sign in to comment.