diff options
| author | Rapptz <[email protected]> | 2019-11-20 00:50:19 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2019-11-20 00:50:19 -0500 |
| commit | 15f0b000e1896dbe5bab59fb83d7668daf60f4da (patch) | |
| tree | a779aa4badb84d868b641cb77b648c445aacb08f /discord/partial_emoji.py | |
| parent | Revert "[tasks] Add Loop.exception for more reliable exception retrieval." (diff) | |
| download | discord.py-15f0b000e1896dbe5bab59fb83d7668daf60f4da.tar.xz discord.py-15f0b000e1896dbe5bab59fb83d7668daf60f4da.zip | |
Move PartialEmoji over to a new namespace to avoid circular imports
Diffstat (limited to 'discord/partial_emoji.py')
| -rw-r--r-- | discord/partial_emoji.py | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/discord/partial_emoji.py b/discord/partial_emoji.py new file mode 100644 index 00000000..9a748b32 --- /dev/null +++ b/discord/partial_emoji.py @@ -0,0 +1,141 @@ +# -*- coding: utf-8 -*- + +""" +The MIT License (MIT) + +Copyright (c) 2015-2019 Rapptz + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +""" + +from .asset import Asset + +class _EmojiTag: + __slots__ = () + +class PartialEmoji(_EmojiTag): + """Represents a "partial" emoji. + + This model will be given in two scenarios: + + - "Raw" data events such as :func:`on_raw_reaction_add` + - Custom emoji that the bot cannot see from e.g. :attr:`Message.reactions` + + .. container:: operations + + .. describe:: x == y + + Checks if two emoji are the same. + + .. describe:: x != y + + Checks if two emoji are not the same. + + .. describe:: hash(x) + + Return the emoji's hash. + + .. describe:: str(x) + + Returns the emoji rendered for discord. + + Attributes + ----------- + name: Optional[:class:`str`] + The custom emoji name, if applicable, or the unicode codepoint + of the non-custom emoji. This can be ``None`` if the emoji + got deleted (e.g. removing a reaction with a deleted emoji). + animated: :class:`bool` + Whether the emoji is animated or not. + id: Optional[:class:`int`] + The ID of the custom emoji, if applicable. + """ + + __slots__ = ('animated', 'name', 'id', '_state') + + def __init__(self, *, name, animated=False, id=None): + self.animated = animated + self.name = name + self.id = id + self._state = None + + @classmethod + def from_dict(cls, data): + return cls(animated=data.get('animated', False), id=data.get('id'), name=data.get('name')) + + def to_dict(self): + o = { 'name': self.name } + if self.id: + o['id'] = self.id + if self.animated: + o['animated'] = self.animated + return o + + @classmethod + def with_state(cls, state, *, animated, name, id=None): + self = cls(animated=animated, name=name, id=id) + self._state = state + return self + + def __str__(self): + if self.id is None: + return self.name + if self.animated: + return '<a:%s:%s>' % (self.name, self.id) + return '<:%s:%s>' % (self.name, self.id) + + def __repr__(self): + return '<{0.__class__.__name__} animated={0.animated} name={0.name!r} id={0.id}>'.format(self) + + def __eq__(self, other): + if self.is_unicode_emoji(): + return isinstance(other, PartialEmoji) and self.name == other.name + + if isinstance(other, _EmojiTag): + return self.id == other.id + return False + + def __ne__(self, other): + return not self.__eq__(other) + + def __hash__(self): + return hash((self.id, self.name)) + + def is_custom_emoji(self): + """Checks if this is a custom non-Unicode emoji.""" + return self.id is not None + + def is_unicode_emoji(self): + """Checks if this is a Unicode emoji.""" + return self.id is None + + def _as_reaction(self): + if self.id is None: + return self.name + return '%s:%s' % (self.name, self.id) + + @property + def url(self): + """:class:`Asset`:Returns an asset of the emoji, if it is custom.""" + if self.is_unicode_emoji(): + return Asset(self._state) + + _format = 'gif' if self.animated else 'png' + url = "/emojis/{0.id}.{1}".format(self, _format) + return Asset(self._state, url) |