Browse Source

Implement icon_rl_as and cover_image_url_as for AppInfo

pull/6076/head
SebbyLaw 5 years ago
committed by Rapptz
parent
commit
2e2560126f
  1. 66
      discord/appinfo.py
  2. 18
      discord/asset.py
  3. 35
      discord/channel.py
  4. 35
      discord/team.py

66
discord/appinfo.py

@ -131,17 +131,79 @@ class AppInfo:
def icon_url(self): def icon_url(self):
""":class:`.Asset`: Retrieves the application's icon asset. """:class:`.Asset`: Retrieves the application's icon asset.
This is equivalent to calling :meth:`icon_url_as` with
the default parameters ('webp' format and a size of 1024).
.. versionadded:: 1.3 .. versionadded:: 1.3
""" """
return Asset._from_icon(self._state, self, 'app') return self.icon_url_as()
def icon_url_as(self, *, format='webp', size=1024):
"""Returns an :class:`Asset` for the icon the application has.
The format must be one of 'webp', 'jpeg', 'jpg' or 'png'.
The size must be a power of 2 between 16 and 4096.
.. versionadded:: 1.6
Parameters
-----------
format: :class:`str`
The format to attempt to convert the icon to. Defaults to 'webp'.
size: :class:`int`
The size of the image to display.
Raises
------
InvalidArgument
Bad image format passed to ``format`` or invalid ``size``.
Returns
--------
:class:`Asset`
The resulting CDN asset.
"""
return Asset._from_icon(self._state, self, 'app', format=format, size=size)
@property @property
def cover_image_url(self): def cover_image_url(self):
""":class:`.Asset`: Retrieves the cover image on a store embed. """:class:`.Asset`: Retrieves the cover image on a store embed.
This is equivalent to calling :meth:`cover_image_url_as` with
the default parameters ('webp' format and a size of 1024).
.. versionadded:: 1.3 .. versionadded:: 1.3
""" """
return Asset._from_cover_image(self._state, self) return self.cover_image_url_as()
def cover_image_url_as(self, *, format='webp', size=1024):
"""Returns an :class:`Asset` for the image on store embeds
if this application is a game sold on Discord.
The format must be one of 'webp', 'jpeg', 'jpg' or 'png'.
The size must be a power of 2 between 16 and 4096.
.. versionadded:: 1.6
Parameters
-----------
format: :class:`str`
The format to attempt to convert the image to. Defaults to 'webp'.
size: :class:`int`
The size of the image to display.
Raises
------
InvalidArgument
Bad image format passed to ``format`` or invalid ``size``.
Returns
--------
:class:`Asset`
The resulting CDN asset.
"""
return Asset._from_cover_image(self._state, self, format=format, size=size)
@property @property
def guild(self): def guild(self):

18
discord/asset.py

@ -89,19 +89,29 @@ class Asset:
return cls(state, '/avatars/{0.id}/{0.avatar}.{1}?size={2}'.format(user, format, size)) return cls(state, '/avatars/{0.id}/{0.avatar}.{1}?size={2}'.format(user, format, size))
@classmethod @classmethod
def _from_icon(cls, state, object, path): def _from_icon(cls, state, object, path, *, format='webp', size=1024):
if object.icon is None: if object.icon is None:
return cls(state) return cls(state)
url = '/{0}-icons/{1.id}/{1.icon}.jpg'.format(path, object) if not utils.valid_icon_size(size):
raise InvalidArgument("size must be a power of 2 between 16 and 4096")
if format not in VALID_STATIC_FORMATS:
raise InvalidArgument("format must be None or one of {}".format(VALID_STATIC_FORMATS))
url = '/{0}-icons/{1.id}/{1.icon}.{2}?size={3}'.format(path, object, format, size)
return cls(state, url) return cls(state, url)
@classmethod @classmethod
def _from_cover_image(cls, state, obj): def _from_cover_image(cls, state, obj, *, format='webp', size=1024):
if obj.cover_image is None: if obj.cover_image is None:
return cls(state) return cls(state)
url = '/app-assets/{0.id}/store/{0.cover_image}.jpg'.format(obj) if not utils.valid_icon_size(size):
raise InvalidArgument("size must be a power of 2 between 16 and 4096")
if format not in VALID_STATIC_FORMATS:
raise InvalidArgument("format must be None or one of {}".format(VALID_STATIC_FORMATS))
url = '/app-assets/{0.id}/store/{0.cover_image}.{1}?size={2}'.format(obj, format, size)
return cls(state, url) return cls(state, url)
@classmethod @classmethod

35
discord/channel.py

@ -1154,8 +1154,39 @@ class GroupChannel(discord.abc.Messageable, Hashable):
@property @property
def icon_url(self): def icon_url(self):
""":class:`Asset`: Returns the channel's icon asset if available.""" """:class:`Asset`: Returns the channel's icon asset if available.
return Asset._from_icon(self._state, self, 'channel')
This is equivalent to calling :meth:`icon_url_as` with
the default parameters ('webp' format and a size of 1024).
"""
return self.icon_url_as()
def icon_url_as(self, *, format='webp', size=1024):
"""Returns an :class:`Asset` for the icon the channel has.
The format must be one of 'webp', 'jpeg', 'jpg' or 'png'.
The size must be a power of 2 between 16 and 4096.
.. versionadded:: 2.0
Parameters
-----------
format: :class:`str`
The format to attempt to convert the icon to. Defaults to 'webp'.
size: :class:`int`
The size of the image to display.
Raises
------
InvalidArgument
Bad image format passed to ``format`` or invalid ``size``.
Returns
--------
:class:`Asset`
The resulting CDN asset.
"""
return Asset._from_icon(self._state, self, 'channel', format=format, size=size)
@property @property
def created_at(self): def created_at(self):

35
discord/team.py

@ -68,8 +68,39 @@ class Team:
@property @property
def icon_url(self): def icon_url(self):
""":class:`.Asset`: Retrieves the team's icon asset.""" """:class:`.Asset`: Retrieves the team's icon asset.
return Asset._from_icon(self._state, self, 'team')
This is equivalent to calling :meth:`icon_url_as` with
the default parameters ('webp' format and a size of 1024).
"""
return self.icon_url_as()
def icon_url_as(self, *, format='None', size=1024):
"""Returns an :class:`Asset` for the icon the team has.
The format must be one of 'webp', 'jpeg', 'jpg' or 'png'.
The size must be a power of 2 between 16 and 4096.
.. versionadded:: 2.0
Parameters
-----------
format: :class:`str`
The format to attempt to convert the icon to. Defaults to 'webp'.
size: :class:`int`
The size of the image to display.
Raises
------
InvalidArgument
Bad image format passed to ``format`` or invalid ``size``.
Returns
--------
:class:`Asset`
The resulting CDN asset.
"""
return Asset._from_icon(self._state, self, 'team', format=format, size=size)
@property @property
def owner(self): def owner(self):

Loading…
Cancel
Save