aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--discord/__init__.py3
-rw-r--r--discord/emoji.py103
-rw-r--r--discord/message.py3
-rw-r--r--discord/partial_emoji.py141
-rw-r--r--discord/state.py3
5 files changed, 150 insertions, 103 deletions
diff --git a/discord/__init__.py b/discord/__init__.py
index f6b8dfed..eade921e 100644
--- a/discord/__init__.py
+++ b/discord/__init__.py
@@ -23,7 +23,8 @@ import logging
from .client import Client
from .appinfo import AppInfo
from .user import User, ClientUser, Profile
-from .emoji import Emoji, PartialEmoji
+from .emoji import Emoji
+from .partial_emoji import PartialEmoji
from .activity import *
from .channel import *
from .guild import Guild, SystemChannelFlags
diff --git a/discord/emoji.py b/discord/emoji.py
index 6ef1b021..00f40447 100644
--- a/discord/emoji.py
+++ b/discord/emoji.py
@@ -26,107 +26,10 @@ DEALINGS IN THE SOFTWARE.
from .asset import Asset
from . import utils
+from .partial_emoji import _EmojiTag
from .user import User
-class PartialEmoji:
- """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, *, animated, name, id=None):
- self.animated = animated
- self.name = name
- self.id = id
- self._state = None
-
- @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, (PartialEmoji, Emoji)):
- return self.id == other.id
-
- 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)
-
-class Emoji:
+class Emoji(_EmojiTag):
"""Represents a custom emoji.
Depending on the way this object was created, some of the attributes can
@@ -213,7 +116,7 @@ class Emoji:
return '<Emoji id={0.id} name={0.name!r} animated={0.animated} managed={0.managed}>'.format(self)
def __eq__(self, other):
- return isinstance(other, (PartialEmoji, Emoji)) and self.id == other.id
+ return isinstance(other, _EmojiTag) and self.id == other.id
def __ne__(self, other):
return not self.__eq__(other)
diff --git a/discord/message.py b/discord/message.py
index 0c9f3988..3b7962dc 100644
--- a/discord/message.py
+++ b/discord/message.py
@@ -31,7 +31,8 @@ import io
from . import utils
from .reaction import Reaction
-from .emoji import Emoji, PartialEmoji
+from .emoji import Emoji
+from .partial_emoji import PartialEmoji
from .calls import CallMessage
from .enums import MessageType, try_enum
from .errors import InvalidArgument, ClientException, HTTPException
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)
diff --git a/discord/state.py b/discord/state.py
index 2c302197..08222aa4 100644
--- a/discord/state.py
+++ b/discord/state.py
@@ -38,7 +38,8 @@ import gc
from .guild import Guild
from .activity import _ActivityTag
from .user import User, ClientUser
-from .emoji import Emoji, PartialEmoji
+from .emoji import Emoji
+from .partial_emoji import PartialEmoji
from .message import Message
from .relationship import Relationship
from .channel import *