Handle cases efficiently when user avatar hash is None. Add method to return Discord default avatar url equivalent.

This commit is contained in:
thec0sm0s
2020-06-02 13:15:00 +05:30
parent f5be182c2f
commit 1fc91d24c2
4 changed files with 16 additions and 3 deletions

View File

@@ -14,4 +14,4 @@ __all__ = [
] ]
__version__ = "0.1.51" __version__ = "0.1.52"

View File

@@ -15,9 +15,11 @@ DISCORD_OAUTH_DEFAULT_SCOPES = [
DISCORD_IMAGE_BASE_URL = "https://cdn.discordapp.com/" DISCORD_IMAGE_BASE_URL = "https://cdn.discordapp.com/"
DISCORD_EMBED_BASE_BASE_URL = "https://cdn.discordapp.com/"
DISCORD_IMAGE_FORMAT = "png" DISCORD_IMAGE_FORMAT = "png"
DISCORD_ANIMATED_IMAGE_FORMAT = "gif" DISCORD_ANIMATED_IMAGE_FORMAT = "gif"
DISCORD_USER_AVATAR_BASE_URL = DISCORD_IMAGE_BASE_URL + "avatars/{user_id}/{avatar_hash}.{format}" DISCORD_USER_AVATAR_BASE_URL = DISCORD_IMAGE_BASE_URL + "avatars/{user_id}/{avatar_hash}.{format}"
DISCORD_DEFAULT_USER_AVATAR_BASE_URL = DISCORD_EMBED_BASE_BASE_URL + "embed/avatars/{modulo5}.png"
DISCORD_GUILD_ICON_BASE_URL = DISCORD_IMAGE_BASE_URL + "icons/{guild_id}/{icon_hash}.png" DISCORD_GUILD_ICON_BASE_URL = DISCORD_IMAGE_BASE_URL + "icons/{guild_id}/{icon_hash}.png"
DISCORD_USERS_CACHE_DEFAULT_MAX_LIMIT = 100 DISCORD_USERS_CACHE_DEFAULT_MAX_LIMIT = 100

View File

@@ -102,15 +102,25 @@ class User(DiscordModelsBase):
@property @property
def avatar_url(self): def avatar_url(self):
"""A property returning direct URL to user's avatar.""" """A property returning direct URL to user's avatar."""
if not self.avatar_hash:
return
image_format = configs.DISCORD_ANIMATED_IMAGE_FORMAT \ image_format = configs.DISCORD_ANIMATED_IMAGE_FORMAT \
if self.is_avatar_animated else configs.DISCORD_IMAGE_FORMAT if self.is_avatar_animated else configs.DISCORD_IMAGE_FORMAT
return configs.DISCORD_USER_AVATAR_BASE_URL.format( return configs.DISCORD_USER_AVATAR_BASE_URL.format(
user_id=self.id, avatar_hash=self.avatar_hash, format=image_format) user_id=self.id, avatar_hash=self.avatar_hash, format=image_format)
@property
def default_avatar_url(self):
"""A property which returns the default avatar URL as when user doesn't has any avatar set."""
return configs.DISCORD_DEFAULT_USER_AVATAR_BASE_URL.format(modulo5=int(self.discriminator) % 5)
@property @property
def is_avatar_animated(self): def is_avatar_animated(self):
"""A boolean representing if avatar of user is animated. Meaning user has GIF avatar.""" """A boolean representing if avatar of user is animated. Meaning user has GIF avatar."""
return self.avatar_hash.startswith("a_") try:
return self.avatar_hash.startswith("a_")
except AttributeError:
return False
@classmethod @classmethod
def fetch_from_api(cls, guilds=False, connections=False): def fetch_from_api(cls, guilds=False, connections=False):

View File

@@ -50,7 +50,8 @@ def me():
<head> <head>
<title>{user.name}</title> <title>{user.name}</title>
</head> </head>
<body><img src='{user.avatar_url}' /> <body><img src='{user.avatar_url or user.default_avatar_url}' />
<p>Is avatar animated: {str(user.is_avatar_animated)}</p>
<a href={url_for("my_connections")}>Connections</a> <a href={url_for("my_connections")}>Connections</a>
<br /> <br />
</body> </body>