From 6f88f94461653b9cb7b577c00a3f16b099568773 Mon Sep 17 00:00:00 2001 From: Droid000 Date: Fri, 11 Oct 2024 15:28:57 -0400 Subject: [PATCH 1/3] Add search_guild_members endpoint. --- discordrb.gemspec | 1 + lib/discordrb/api/server.rb | 13 +++++++++++++ lib/discordrb/data/overwrite.rb | 2 ++ lib/discordrb/data/server.rb | 11 +++++++++++ lib/discordrb/permissions.rb | 2 ++ 5 files changed, 29 insertions(+) diff --git a/discordrb.gemspec b/discordrb.gemspec index d7b5c5af2..1da179afa 100644 --- a/discordrb.gemspec +++ b/discordrb.gemspec @@ -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' diff --git a/lib/discordrb/api/server.rb b/lib/discordrb/api/server.rb index 18ce0eb37..9b383299e 100644 --- a/lib/discordrb/api/server.rb +++ b/lib/discordrb/api/server.rb @@ -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, diff --git a/lib/discordrb/data/overwrite.rb b/lib/discordrb/data/overwrite.rb index 8a103459c..0c7e9e181 100644 --- a/lib/discordrb/data/overwrite.rb +++ b/lib/discordrb/data/overwrite.rb @@ -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 && diff --git a/lib/discordrb/data/server.rb b/lib/discordrb/data/server.rb index 5795444a3..b332639df 100644 --- a/lib/discordrb/data/server.rb +++ b/lib/discordrb/data/server.rb @@ -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. + # @return [Array] 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.nil? + + 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. diff --git a/lib/discordrb/permissions.rb b/lib/discordrb/permissions.rb index b9194bde7..10d436bed 100644 --- a/lib/discordrb/permissions.rb +++ b/lib/discordrb/permissions.rb @@ -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 From c715a2b8e64e7a765570481abaf1c65c4f667881 Mon Sep 17 00:00:00 2001 From: Droid000 Date: Fri, 11 Oct 2024 15:48:34 -0400 Subject: [PATCH 2/3] Fix ineffective guard clause. --- lib/discordrb/data/server.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/discordrb/data/server.rb b/lib/discordrb/data/server.rb index b332639df..7dafe98ad 100644 --- a/lib/discordrb/data/server.rb +++ b/lib/discordrb/data/server.rb @@ -591,7 +591,7 @@ def max_emoji # @return [Array] 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.nil? + return nil if response.empty? response.map { |mem| Member.new(mem, self, @bot) } end From 7a80223799c02f20d41c322187978f5627070cd0 Mon Sep 17 00:00:00 2001 From: Droid000 Date: Fri, 11 Oct 2024 16:14:25 -0400 Subject: [PATCH 3/3] Make argument KWARG rather than keyword --- lib/discordrb/data/server.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/discordrb/data/server.rb b/lib/discordrb/data/server.rb index 7dafe98ad..2569ee3e6 100644 --- a/lib/discordrb/data/server.rb +++ b/lib/discordrb/data/server.rb @@ -587,9 +587,9 @@ def max_emoji # 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. + # @param limit [Integer] The maximum number of members between 1-1000 to return. Returns 1 member by default. # @return [Array] An array of member objects that match the given parameters. - def search_members(name, limit: nil) + def search_members(name:, limit: nil) response = JSON.parse(API::Server.search_guild_members(@bot.token, @id, name, limit)) return nil if response.empty?