Browse Source

ref: improve User.get_avatar_url and Guild.get_icon_url

pull/154/head
Andrei 6 years ago
parent
commit
ced24273ce
No known key found for this signature in database GPG Key ID: 4D2A02C7D500E9D9
  1. 13
      disco/types/guild.py
  2. 13
      disco/types/user.py
  3. 33
      tests/types/user.py

13
disco/types/guild.py

@ -526,15 +526,18 @@ class Guild(SlottedModel, Permissible):
def get_emoji(self, emoji): def get_emoji(self, emoji):
return self.client.api.guilds_emojis_get(self.id, emoji) return self.client.api.guilds_emojis_get(self.id, emoji)
def get_icon_url(self, fmt=None, size=1024): def get_icon_url(self, still_format='webp', animated_format='gif', size=1024):
if not self.icon: if not self.icon:
return '' return ''
if fmt is not None:
return 'https://cdn.discordapp.com/icons/{}/{}.{}?size={}'.format(self.id, self.icon, fmt, size)
if self.icon.startswith('a_'): if self.icon.startswith('a_'):
return 'https://cdn.discordapp.com/icons/{}/{}.gif?size={}'.format(self.id, self.icon, size) return 'https://cdn.discordapp.com/icons/{}/{}.{}?size={}'.format(
self.id, self.icon, animated_format, size
)
else: else:
return 'https://cdn.discordapp.com/icons/{}/{}.webp?size={}'.format(self.id, self.icon, size) return 'https://cdn.discordapp.com/icons/{}/{}.{}?size={}'.format(
self.id, self.icon, still_format, size
)
def get_splash_url(self, fmt='webp', size=1024): def get_splash_url(self, fmt='webp', size=1024):
if not self.splash: if not self.splash:

13
disco/types/user.py

@ -27,15 +27,18 @@ class User(SlottedModel, with_equality('id'), with_hash('id')):
presence = Field(None) presence = Field(None)
def get_avatar_url(self, fmt=None, size=1024): def get_avatar_url(self, still_format='webp', animated_format='gif', size=1024):
if not self.avatar: if not self.avatar:
return 'https://cdn.discordapp.com/embed/avatars/{}.png'.format(self.default_avatar) return 'https://cdn.discordapp.com/embed/avatars/{}.png'.format(self.default_avatar)
if fmt is not None:
return 'https://cdn.discordapp.com/avatars/{}/{}.{}?size={}'.format(self.id, self.avatar, fmt, size)
if self.avatar.startswith('a_'): if self.avatar.startswith('a_'):
return 'https://cdn.discordapp.com/avatars/{}/{}.gif?size={}'.format(self.id, self.avatar, size) return 'https://cdn.discordapp.com/avatars/{}/{}.{}?size={}'.format(
self.id, self.avatar, animated_format, size
)
else: else:
return 'https://cdn.discordapp.com/avatars/{}/{}.webp?size={}'.format(self.id, self.avatar, size) return 'https://cdn.discordapp.com/avatars/{}/{}.{}?size={}'.format(
self.id, self.avatar, still_format, size
)
@property @property
def default_avatar(self): def default_avatar(self):

33
tests/types/user.py

@ -1,22 +1,21 @@
from unittest import TestCase
from disco.types.user import User, DefaultAvatars from disco.types.user import User, DefaultAvatars
class TestChannel(TestCase): def test_user_avatar_url():
def test_user_avatar(self): u = User(id=12345, avatar='1234567890abcdefghijkl')
u = User( assert u.avatar_url == 'https://cdn.discordapp.com/avatars/12345/1234567890abcdefghijkl.webp?size=1024'
id=12345, avatar_url = u.get_avatar_url(still_format='png')
username='test123', assert avatar_url == 'https://cdn.discordapp.com/avatars/12345/1234567890abcdefghijkl.png?size=1024'
avatar='1234567890abcdefghijkl',
discriminator='1234',
bot=False) def test_user_animated_avatar_url():
u = User(id=12345, avatar='a_1234567890abcdefghijkl')
assert u.avatar_url == 'https://cdn.discordapp.com/avatars/12345/a_1234567890abcdefghijkl.gif?size=1024'
avatar_url = u.get_avatar_url(animated_format='webp')
assert avatar_url == 'https://cdn.discordapp.com/avatars/12345/a_1234567890abcdefghijkl.webp?size=1024'
self.assertEqual(
u.avatar_url, 'https://cdn.discordapp.com/avatars/12345/1234567890abcdefghijkl.webp?size=1024'
)
def test_user_default_avatar(self): def test_user_default_avatar_url():
u = User(id=123456, discriminator='1234') u = User(id=12345, discriminator='1234')
self.assertEqual(u.default_avatar, DefaultAvatars.RED) assert u.default_avatar == DefaultAvatars.RED
self.assertEqual(u.avatar_url, 'https://cdn.discordapp.com/embed/avatars/4.png') assert u.avatar_url == 'https://cdn.discordapp.com/embed/avatars/4.png'

Loading…
Cancel
Save