Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add server avatar #146

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/discordrb/api/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,14 @@ def add_member(token, server_id, user_id, access_token, nick = nil, roles = [],
Authorization: token
)
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'
else
'webp'
end
"#{Discordrb::API.cdn_url}/guilds/#{guild_id}/users/#{user_id}/avatars/#{avatar_id}.#{format}"
end
end
16 changes: 16 additions & 0 deletions lib/discordrb/data/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 member's current avatar, can be used to generate an avatar URL.
# @see Member#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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -294,6 +302,13 @@ def display_name
nickname || username
end

# (See User#avatar_url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# (See User#avatar_url)
@see User#avatar_url

def avatar_url(format = nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include a parameter to get the global avatar_url, regardless of a Member's avatar_id, since the Member doesn't provide easy access to the underlying @user, and this overwrites one of the methods.

Also, would be more appropriate on MemberAttributes for consistency (#avatar_url is on UserAttributes) before the v4 rework

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
Expand Down Expand Up @@ -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']
Expand Down