From 2fe5da836c583d121d5834cf16dd5c801f41a428 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Wed, 22 Feb 2017 23:17:52 -0500 Subject: [PATCH] Add User.avatar_url_as to convert a user's avatar. --- discord/user.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/discord/user.py b/discord/user.py index 0cde6ea07..1b8dd766b 100644 --- a/discord/user.py +++ b/discord/user.py @@ -64,15 +64,47 @@ class BaseUser: If the user does not have a traditional avatar, their default avatar URL is returned instead. + + This is equivalent to calling :meth:`avatar_url_as` with + the default parameters (i.e. webp/gif detection and a size of 1024). + """ + return self.avatar_url_as(format=None, size=1024) + + def avatar_url_as(self, *, format, size=1024): + """Returns a friendly URL version of the avatar the user has. + + If the user does not have a traditional avatar, their default + avatar URL is returned instead. + + The format must be one of 'webp', 'jpeg', 'png' or 'gif'. The + size must be a power of 2 (128, 256, 512, 1024). + + Parameters + ----------- + format: Optional[str] + The format to attempt to convert the avatar to. + If the format is ``None``, then it is automatically + detected into either 'gif' or 'webp' depending on the + avatar being animated or not. + size: int + The size of the image to display. + + Returns + -------- + str + The resulting CDN URL. """ + if self.avatar is None: return self.default_avatar_url - url = 'https://cdn.discordapp.com/avatars/{0.id}/{0.avatar}.{1}?size=1024' - if self.avatar.startswith('a_'): - return url.format(self, 'gif') - else: - return url.format(self, 'webp') + if format is None: + if self.avatar.startswith('a_'): + format = 'gif' + else: + format = 'webp' + + return 'https://cdn.discordapp.com/avatars/{0.id}/{0.avatar}.{1}?size={2}'.format(self, format, size) @property def default_avatar(self):