Add Guild.splash_url_as

This commit is contained in:
Matt (IPv4) 2018-10-05 16:42:13 +01:00 committed by Rapptz
parent fed01c5e15
commit 3727ea9811
2 changed files with 38 additions and 7 deletions

View File

@ -404,10 +404,10 @@ class Guild(Hashable):
return self.icon_url_as() return self.icon_url_as()
def icon_url_as(self, *, format='webp', size=1024): def icon_url_as(self, *, format='webp', size=1024):
"""Returns a friendly URL version of the guild's icon. Returns and empty string if it has no icon. """Returns a friendly URL version of the guild's icon. Returns an empty string if it has no icon.
The format must be one of 'webp', 'jpeg', 'jpg', or 'png'. The The format must be one of 'webp', 'jpeg', 'jpg', or 'png'. The
size must be a power of 2 between 16 and 1024. size must be a power of 2 between 16 and 2048.
Parameters Parameters
----------- -----------
@ -427,7 +427,7 @@ class Guild(Hashable):
Bad image format passed to ``format`` or invalid ``size``. Bad image format passed to ``format`` or invalid ``size``.
""" """
if not valid_icon_size(size): if not 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 2048")
if format not in VALID_ICON_FORMATS: if format not in VALID_ICON_FORMATS:
raise InvalidArgument("format must be one of {}".format(VALID_ICON_FORMATS)) raise InvalidArgument("format must be one of {}".format(VALID_ICON_FORMATS))
@ -435,13 +435,44 @@ class Guild(Hashable):
return '' return ''
return 'https://cdn.discordapp.com/icons/{0.id}/{0.icon}.{1}?size={2}'.format(self, format, size) return 'https://cdn.discordapp.com/icons/{0.id}/{0.icon}.{1}?size={2}'.format(self, format, size)
@property @property
def splash_url(self): def splash_url(self):
"""Returns the URL version of the guild's invite splash. Returns an empty string if it has no splash.""" """Returns the URL version of the guild's invite splash. Returns an empty string if it has no splash."""
return self.icon_url_as()
def splash_url_as(self, *, format='webp', size=2048):
"""Returns a friendly URL version of the guild's invite splash. Returns an empty string if it has no splash.
The format must be one of 'webp', 'jpeg', 'jpg', or 'png'. The
size must be a power of 2 between 16 and 2048.
Parameters
-----------
format: str
The format to attempt to convert the splash to.
size: int
The size of the image to display.
Returns
--------
str
The resulting CDN URL.
Raises
------
InvalidArgument
Bad image format passed to ``format`` or invalid ``size``.
"""
if not valid_icon_size(size):
raise InvalidArgument("size must be a power of 2 between 16 and 2048")
if format not in VALID_ICON_FORMATS:
raise InvalidArgument("format must be one of {}".format(VALID_ICON_FORMATS))
if self.splash is None: if self.splash is None:
return '' return ''
return 'https://cdn.discordapp.com/splashes/{0.id}/{0.splash}.jpg?size=2048'.format(self)
return 'https://cdn.discordapp.com/splashes/{0.id}/{0.splash}.{1}?size={2}'.format(self, format, size)
@property @property
def member_count(self): def member_count(self):

View File

@ -290,8 +290,8 @@ async def sane_wait_for(futures, *, timeout, loop):
raise asyncio.TimeoutError() raise asyncio.TimeoutError()
def valid_icon_size(size): def valid_icon_size(size):
"""Icons must be power of 2 within [16, 1024].""" """Icons must be power of 2 within [16, 2048]."""
return ((size != 0) and not (size & (size - 1))) and size in range(16, 1025) return ((size != 0) and not (size & (size - 1))) and size in range(16, 2049)
class SnowflakeList(array.array): class SnowflakeList(array.array):
"""Internal data storage class to efficiently store a list of snowflakes. """Internal data storage class to efficiently store a list of snowflakes.