From 13bba3afc228ae3e01a3890cff223202721c0447 Mon Sep 17 00:00:00 2001 From: Daggy1234 Date: Fri, 18 Dec 2020 08:50:00 +0530 Subject: [PATCH] Add Emoji.url_as --- discord/asset.py | 13 +++++++++++++ discord/emoji.py | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/discord/asset.py b/discord/asset.py index bf8d8f9d0..29a86769b 100644 --- a/discord/asset.py +++ b/discord/asset.py @@ -153,6 +153,19 @@ class Asset: return cls(state, '/stickers/{0.id}/{0.image}.png?size={2}'.format(sticker, format, size)) + @classmethod + def _from_emoji(cls, state, emoji, *, format=None, static_format='png'): + 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)) + if format == "gif" and not emoji.animated: + raise InvalidArgument("non animated emoji's 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 format is None: + format = 'gif' if emoji.animated else static_format + + return cls(state, '/emojis/{0.id}.{1}'.format(emoji, format)) + def __str__(self): return self.BASE + self._url if self._url is not None else '' diff --git a/discord/emoji.py b/discord/emoji.py index 5feb1c904..05338b74c 100644 --- a/discord/emoji.py +++ b/discord/emoji.py @@ -131,10 +131,12 @@ class Emoji(_EmojiTag): @property def url(self): - """:class:`Asset`: Returns the asset of the emoji.""" - _format = 'gif' if self.animated else 'png' - url = "/emojis/{0.id}.{1}".format(self, _format) - return Asset(self._state, url) + """:class:`Asset`: Returns the asset of the emoji. + + This is equivalent to calling :meth:`url_as` with + the default parameters (i.e. png/gif detection). + """ + return self.url_as(format=None) @property def roles(self): @@ -153,6 +155,37 @@ class Emoji(_EmojiTag): """:class:`Guild`: The guild this emoji belongs to.""" return self._state._get_guild(self.guild_id) + + def url_as(self, *, format=None, static_format="png"): + """Returns an :class:`Asset` for the emoji's url. + + The format must be one of 'webp', 'jpeg', 'jpg', 'png' or 'gif'. + 'gif' is only valid for animated emojis. + + Parameters + ----------- + format: Optional[:class:`str`] + The format to attempt to convert the emojis to. + If the format is ``None``, then it is automatically + detected as either 'gif' or static_format, depending on whether the + emoji is animated or not. + static_format: Optional[:class:`str`] + Format to attempt to convert only non-animated emoji's to. + Defaults to 'png' + + Raises + ------- + InvalidArgument + Bad image format passed to ``format`` or ``static_format``. + + Returns + -------- + :class:`Asset` + The resulting CDN asset. + """ + return Asset._from_emoji(self._state, self, format=format, static_format=static_format) + + def is_usable(self): """:class:`bool`: Whether the bot can use this emoji.