From d4a836ecefcb976c1e6a15af703151a13762023a Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 28 Mar 2022 15:27:05 -0400 Subject: [PATCH 1/2] feat: add server avatar --- lib/discordrb/api/server.rb | 9 +++++++++ lib/discordrb/data/member.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/discordrb/api/server.rb b/lib/discordrb/api/server.rb index 6751fa303..4df88e0fa 100644 --- a/lib/discordrb/api/server.rb +++ b/lib/discordrb/api/server.rb @@ -540,4 +540,13 @@ def add_member(token, server_id, user_id, access_token, nick = nil, roles = [], Authorization: token ) end + + def avatar_url(guild_id, user_id, avatar_id, format = nil) + format ||= if avatar_id.start_with?('a_') + 'gif' + else + 'webp' + end + "#{Discordrb::API.cdn_url}/guilds/#{guild_id}/users/#{user_id}/avatars/#{avatar_id}.#{format}" + end end diff --git a/lib/discordrb/data/member.rb b/lib/discordrb/data/member.rb index 3681662a4..75491258c 100644 --- a/lib/discordrb/data/member.rb +++ b/lib/discordrb/data/member.rb @@ -22,6 +22,13 @@ module MemberAttributes # @return [Time] When the user's timeout will expire. attr_reader :communication_disabled_until alias_method :timeout, :communication_disabled_until + + # @!attribute [r] avatar_id + # @return [String, nil] the ID of this user's current avatar, can be used to generate an avatar URL. + # @see User#avatar_url + def avatar_id + @avatar || @user.avatar_id + end end # A member is a user on a server. It differs from regular users in that it has roles, voice statuses and things like @@ -77,6 +84,7 @@ def initialize(data, server, bot) timeout_until = data['communication_disabled_until'] @communication_disabled_until = timeout_until ? Time.parse(timeout_until) : nil @permissions = Permissions.new(data['permissions']) if data['permissions'] + @avatar_id = data['avatar'] end # @return [Server] the server this member is on. @@ -294,6 +302,13 @@ def display_name nickname || username end + # (BoSee User#avatar_url) + def avatar_url(format = nil) + return @user.avatar_url(format) unless @avatar_id + + API::Server.avatar_url(@server_id, @user.id, @avatar_id, format) + end + # Update this member's roles # @note For internal use only. # @!visibility private @@ -335,6 +350,7 @@ def update_data(data) update_nick(data['nick']) if data.key?('nick') @mute = data['mute'] if data.key?('mute') @deaf = data['deaf'] if data.key?('deaf') + @avatar_id = data['avatar'] if data.key?('avatar') @joined_at = Time.parse(data['joined_at']) if data['joined_at'] timeout_until = data['communication_disabled_until'] From 0ec92da03194ffc26d8099b04e879fb3f5165d43 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 28 Mar 2022 15:41:54 -0400 Subject: [PATCH 2/2] fix: correct documentation --- lib/discordrb/api/server.rb | 1 + lib/discordrb/data/member.rb | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/discordrb/api/server.rb b/lib/discordrb/api/server.rb index 4df88e0fa..f66cd8149 100644 --- a/lib/discordrb/api/server.rb +++ b/lib/discordrb/api/server.rb @@ -541,6 +541,7 @@ def add_member(token, server_id, user_id, access_token, nick = nil, roles = [], ) end + # Make an avatar URL from the guild, user and avatar IDs def avatar_url(guild_id, user_id, avatar_id, format = nil) format ||= if avatar_id.start_with?('a_') 'gif' diff --git a/lib/discordrb/data/member.rb b/lib/discordrb/data/member.rb index 75491258c..6fd06d7e8 100644 --- a/lib/discordrb/data/member.rb +++ b/lib/discordrb/data/member.rb @@ -24,8 +24,8 @@ module MemberAttributes alias_method :timeout, :communication_disabled_until # @!attribute [r] avatar_id - # @return [String, nil] the ID of this user's current avatar, can be used to generate an avatar URL. - # @see User#avatar_url + # @return [String, nil] the ID of this member's current avatar, can be used to generate an avatar URL. + # @see Member#avatar_url def avatar_id @avatar || @user.avatar_id end @@ -302,7 +302,7 @@ def display_name nickname || username end - # (BoSee User#avatar_url) + # (See User#avatar_url) def avatar_url(format = nil) return @user.avatar_url(format) unless @avatar_id