From 42a7c4f7e5caa7baf555e86b52249419ce33acfc Mon Sep 17 00:00:00 2001 From: SnowyLuma Date: Sat, 9 Mar 2019 05:48:23 +0100 Subject: Add support for guild banners Document banner attribute of Guild and Invite Update discord/utils.py Co-Authored-By: SnowyLuma <38926001+SnowyLuma@users.noreply.github.com> --- discord/guild.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 5 deletions(-) (limited to 'discord/guild.py') diff --git a/discord/guild.py b/discord/guild.py index 122ebb0f..194f54e0 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -95,6 +95,8 @@ class Guild(Hashable): all be None. It is best to not do anything with the guild if it is unavailable. Check the :func:`on_guild_unavailable` and :func:`on_guild_available` events. + banner: Optional[:class:`str`] + The guild's banner. mfa_level: :class:`int` Indicates the guild's two factor authorisation level. If this value is 0 then the guild does not require 2FA for their administrative members. If the value is @@ -119,7 +121,7 @@ class Guild(Hashable): """ __slots__ = ('afk_timeout', 'afk_channel', '_members', '_channels', 'icon', - 'name', 'id', 'unavailable', 'name', 'region', '_state', + 'name', 'id', 'unavailable', 'banner', 'region', '_state', '_default_role', '_roles', '_member_count', '_large', 'owner_id', 'mfa_level', 'emojis', 'features', 'verification_level', 'explicit_content_filter', 'splash', @@ -211,6 +213,7 @@ class Guild(Hashable): self.explicit_content_filter = try_enum(ContentFilter, guild.get('explicit_content_filter', 0)) self.afk_timeout = guild.get('afk_timeout') self.icon = guild.get('icon') + self.banner = guild.get('banner') self.unavailable = guild.get('unavailable', False) self.id = int(guild['id']) self._roles = {} @@ -409,7 +412,7 @@ class Guild(Hashable): """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 - size must be a power of 2 between 16 and 2048. + size must be a power of 2 between 16 and 4096. Parameters ----------- @@ -429,7 +432,7 @@ class Guild(Hashable): 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") + raise InvalidArgument("size must be a power of 2 between 16 and 4096") if format not in VALID_ICON_FORMATS: raise InvalidArgument("format must be one of {}".format(VALID_ICON_FORMATS)) @@ -438,6 +441,44 @@ class Guild(Hashable): return 'https://cdn.discordapp.com/icons/{0.id}/{0.icon}.{1}?size={2}'.format(self, format, size) + @property + def banner_url(self): + """Returns the URL version of the guild's banner. Returns an empty string if it has no banner.""" + return self.banner_url_as() + + def banner_url_as(self, *, format='webp', size=2048): + """Returns a friendly URL version of the guild's banner. Returns an empty string if it has no banner. + + The format must be one of 'webp', 'jpeg', or 'png'. The + size must be a power of 2 between 16 and 4096. + + Parameters + ----------- + format: str + The format to attempt to convert the banner 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 4096") + if format not in VALID_ICON_FORMATS: + raise InvalidArgument("format must be one of {}".format(VALID_ICON_FORMATS)) + + if self.banner is None: + return '' + + return 'https://cdn.discordapp.com/banners/{0.id}/{0.banner}.{1}?size={2}'.format(self, format, size) + @property def splash_url(self): """Returns the URL version of the guild's invite splash. Returns an empty string if it has no splash.""" @@ -447,7 +488,7 @@ class Guild(Hashable): """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. + size must be a power of 2 between 16 and 4096. Parameters ----------- @@ -467,7 +508,7 @@ class Guild(Hashable): 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") + raise InvalidArgument("size must be a power of 2 between 16 and 4096") if format not in VALID_ICON_FORMATS: raise InvalidArgument("format must be one of {}".format(VALID_ICON_FORMATS)) @@ -764,6 +805,9 @@ class Guild(Hashable): icon: bytes A :term:`py:bytes-like object` representing the icon. Only PNG/JPEG supported. Could be ``None`` to denote removal of the icon. + banner: bytes + A :term:`py:bytes-like object` representing the banner. + Could be ``None`` to denote removal of the banner. splash: bytes A :term:`py:bytes-like object` representing the invite splash. Only PNG/JPEG supported. Could be ``None`` to denote removing the @@ -814,6 +858,16 @@ class Guild(Hashable): else: icon = None + try: + banner_bytes = fields['banner'] + except KeyError: + banner = self.banner + else: + if banner_bytes is not None: + banner = utils._bytes_to_base64_data(banner_bytes) + else: + banner = None + try: vanity_code = fields['vanity_code'] except KeyError: @@ -832,6 +886,7 @@ class Guild(Hashable): splash = None fields['icon'] = icon + fields['banner'] = banner fields['splash'] = splash try: -- cgit v1.2.3