Skip to content

Commit

Permalink
Merge pull request #277 from Droid00000/mem-search
Browse files Browse the repository at this point in the history
Feat: search guild members
  • Loading branch information
swarley authored Oct 13, 2024
2 parents 080c159 + 7a80223 commit 840c533
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions discordrb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
}
spec.require_paths = ['lib']

spec.add_dependency 'base64', '~> 0.2.0'
spec.add_dependency 'ffi', '>= 1.9.24'
spec.add_dependency 'opus-ruby'
spec.add_dependency 'rest-client', '>= 2.0.0'
Expand Down
13 changes: 13 additions & 0 deletions lib/discordrb/api/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ def resolve_members(token, server_id, limit, after = nil)
)
end

# Search for a guild member
# https://discord.com/developers/docs/resources/guild#search-guild-members
def search_guild_members(token, server_id, query, limit)
query_string = URI.encode_www_form({ query: query, limit: limit }.compact)
Discordrb::API.request(
:guilds_sid_members,
server_id,
:get,
"#{Discordrb::API.api_base}/guilds/#{server_id}/members/search?#{query_string}",
Authorization: token
)
end

# Update a user properties
# https://discord.com/developers/docs/resources/guild#modify-guild-member
def update_member(token, server_id, user_id, nick: :undef, roles: :undef, mute: :undef, deaf: :undef, channel_id: :undef,
Expand Down
2 changes: 2 additions & 0 deletions lib/discordrb/data/overwrite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def initialize(object = nil, type: nil, allow: 0, deny: 0)

# Comparison by attributes [:id, :type, :allow, :deny]
def ==(other)
# rubocop:disable Lint/Void
false unless other.is_a? Discordrb::Overwrite
# rubocop:enable Lint/Void
id == other.id &&
type == other.type &&
allow == other.allow &&
Expand Down
11 changes: 11 additions & 0 deletions lib/discordrb/data/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,17 @@ def max_emoji
end
end

# Searches a server for members that matches a username or a nickname.
# @param name [String] The username or nickname to search for.
# @param limit [Integer] The maximum number of members between 1-1000 to return. Returns 1 member by default.
# @return [Array<Member>] An array of member objects that match the given parameters.
def search_members(name:, limit: nil)
response = JSON.parse(API::Server.search_guild_members(@bot.token, @id, name, limit))
return nil if response.empty?

response.map { |mem| Member.new(mem, self, @bot) }
end

# Retrieve banned users from this server.
# @param limit [Integer] Number of users to return (up to maximum 1000, default 1000).
# @param before_id [Integer] Consider only users before given user id.
Expand Down
2 changes: 2 additions & 0 deletions lib/discordrb/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def defined_permissions

# Comparison based on permission bits
def ==(other)
# rubocop:disable Lint/Void
false unless other.is_a? Discordrb::Permissions
# rubocop:enable Lint/Void
bits == other.bits
end
end
Expand Down

0 comments on commit 840c533

Please sign in to comment.