aboutsummaryrefslogtreecommitdiff
path: root/discord/user.py
diff options
context:
space:
mode:
authorRapptz <[email protected]>2017-02-22 23:17:52 -0500
committerRapptz <[email protected]>2017-02-22 23:17:52 -0500
commit2fe5da836c583d121d5834cf16dd5c801f41a428 (patch)
treeb5c0f26bf39f8a9762ee8248f445f1b9ad473d68 /discord/user.py
parent[commands] Register cog listeners with the name of the attribute. (diff)
downloaddiscord.py-2fe5da836c583d121d5834cf16dd5c801f41a428.tar.xz
discord.py-2fe5da836c583d121d5834cf16dd5c801f41a428.zip
Add User.avatar_url_as to convert a user's avatar.
Diffstat (limited to 'discord/user.py')
-rw-r--r--discord/user.py42
1 files changed, 37 insertions, 5 deletions
diff --git a/discord/user.py b/discord/user.py
index 0cde6ea0..1b8dd766 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):