Browse Source

Add support for animated guild icons.

pull/2203/head
Rapptz 6 years ago
parent
commit
9674055c2a
  1. 22
      discord/asset.py
  2. 22
      discord/guild.py
  3. 2
      discord/user.py

22
discord/asset.py

@ -70,7 +70,7 @@ class Asset:
@classmethod @classmethod
def _from_avatar(cls, state, user, *, format=None, static_format='webp', size=1024): def _from_avatar(cls, state, user, *, format=None, static_format='webp', size=1024):
if not utils.valid_icon_size(size): if not utils.valid_icon_size(size):
raise InvalidArgument("size must be a power of 2 between 16 and 1024") raise InvalidArgument("size must be a power of 2 between 16 and 4096")
if format is not None and format not in VALID_AVATAR_FORMATS: if format is not None and format not in VALID_AVATAR_FORMATS:
raise InvalidArgument("format must be None or one of {}".format(VALID_AVATAR_FORMATS)) raise InvalidArgument("format must be None or one of {}".format(VALID_AVATAR_FORMATS))
if format == "gif" and not user.is_avatar_animated(): if format == "gif" and not user.is_avatar_animated():
@ -107,6 +107,26 @@ class Asset:
url = 'https://cdn.discordapp.com/{key}/{0}/{1}.{2}?size={3}' url = 'https://cdn.discordapp.com/{key}/{0}/{1}.{2}?size={3}'
return cls(state, url.format(id, hash, format, size, key=key)) return cls(state, url.format(id, hash, format, size, key=key))
@classmethod
def _from_guild_icon(cls, state, guild, *, format=None, static_format='webp', size=1024):
if not utils.valid_icon_size(size):
raise InvalidArgument("size must be a power of 2 between 16 and 4096")
if format is not None and format not in VALID_AVATAR_FORMATS:
raise InvalidArgument("format must be one of {}".format(VALID_AVATAR_FORMATS))
if format == "gif" and not guild.is_icon_animated():
raise InvalidArgument("non animated guild icons do not support gif format")
if static_format not in VALID_STATIC_FORMATS:
raise InvalidArgument("static_format must be one of {}".format(VALID_STATIC_FORMATS))
if guild.icon is None:
return cls(state)
if format is None:
format = 'gif' if guild.is_icon_animated() else static_format
return cls(state, 'https://cdn.discordapp.com/icons/{0.id}/{0.icon}.{1}?size={2}'.format(guild, format, size))
def __str__(self): def __str__(self):
return self._url if self._url is not None else '' return self._url if self._url is not None else ''

22
discord/guild.py

@ -459,16 +459,26 @@ class Guild(Hashable):
""":class:`Asset`: Returns the guild's icon asset.""" """:class:`Asset`: Returns the guild's icon asset."""
return self.icon_url_as() return self.icon_url_as()
def icon_url_as(self, *, format='webp', size=1024): def is_icon_animated(self):
"""Returns a :class:`Asset`: for the guild's icon. """:class:`bool`: Returns True if the guild has an animated icon."""
return bool(self.icon and self.icon.startswith('a_'))
The format must be one of 'webp', 'jpeg', 'jpg', or 'png'. The def icon_url_as(self, *, format=None, static_format='webp', size=1024):
size must be a power of 2 between 16 and 4096. """Returns a :class:`Asset` for the guild's icon.
The format must be one of 'webp', 'jpeg', 'jpg', 'png' or 'gif', and
'gif' is only valid for animated avatars. The size must be a power of 2
between 16 and 4096.
Parameters Parameters
----------- -----------
format: :class:`str` format: Optional[:class:`str`]
The format to attempt to convert the icon to. The format to attempt to convert the icon to.
If the format is ``None``, then it is automatically
detected into either 'gif' or static_format depending on the
icon being animated or not.
static_format: Optional[:class:`str`]
Format to attempt to convert only non-animated icons to.
size: :class:`int` size: :class:`int`
The size of the image to display. The size of the image to display.
@ -482,7 +492,7 @@ class Guild(Hashable):
:class:`Asset` :class:`Asset`
The resulting CDN asset. The resulting CDN asset.
""" """
return Asset._from_guild_image(self._state, self.id, self.icon, 'icons', format=format, size=size) return Asset._from_guild_icon(self._state, self, format=format, static_format=static_format, size=size)
@property @property
def banner_url(self): def banner_url(self):

2
discord/user.py

@ -136,7 +136,7 @@ class BaseUser(_BaseUser):
The format must be one of 'webp', 'jpeg', 'jpg', 'png' or 'gif', and The format must be one of 'webp', 'jpeg', 'jpg', 'png' or 'gif', and
'gif' is only valid for animated avatars. The size must be a power of 2 'gif' is only valid for animated avatars. The size must be a power of 2
between 16 and 1024. between 16 and 4096.
Parameters Parameters
----------- -----------

Loading…
Cancel
Save