mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-12 16:59:50 +00:00
Move PartialEmoji over to a new namespace to avoid circular imports
This commit is contained in:
parent
e1a237a0d3
commit
15f0b000e1
@ -23,7 +23,8 @@ import logging
|
|||||||
from .client import Client
|
from .client import Client
|
||||||
from .appinfo import AppInfo
|
from .appinfo import AppInfo
|
||||||
from .user import User, ClientUser, Profile
|
from .user import User, ClientUser, Profile
|
||||||
from .emoji import Emoji, PartialEmoji
|
from .emoji import Emoji
|
||||||
|
from .partial_emoji import PartialEmoji
|
||||||
from .activity import *
|
from .activity import *
|
||||||
from .channel import *
|
from .channel import *
|
||||||
from .guild import Guild, SystemChannelFlags
|
from .guild import Guild, SystemChannelFlags
|
||||||
|
103
discord/emoji.py
103
discord/emoji.py
@ -26,107 +26,10 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
from .asset import Asset
|
from .asset import Asset
|
||||||
from . import utils
|
from . import utils
|
||||||
|
from .partial_emoji import _EmojiTag
|
||||||
from .user import User
|
from .user import User
|
||||||
|
|
||||||
class PartialEmoji:
|
class Emoji(_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, *, 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:
|
|
||||||
"""Represents a custom emoji.
|
"""Represents a custom emoji.
|
||||||
|
|
||||||
Depending on the way this object was created, some of the attributes can
|
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)
|
return '<Emoji id={0.id} name={0.name!r} animated={0.animated} managed={0.managed}>'.format(self)
|
||||||
|
|
||||||
def __eq__(self, other):
|
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):
|
def __ne__(self, other):
|
||||||
return not self.__eq__(other)
|
return not self.__eq__(other)
|
||||||
|
@ -31,7 +31,8 @@ import io
|
|||||||
|
|
||||||
from . import utils
|
from . import utils
|
||||||
from .reaction import Reaction
|
from .reaction import Reaction
|
||||||
from .emoji import Emoji, PartialEmoji
|
from .emoji import Emoji
|
||||||
|
from .partial_emoji import PartialEmoji
|
||||||
from .calls import CallMessage
|
from .calls import CallMessage
|
||||||
from .enums import MessageType, try_enum
|
from .enums import MessageType, try_enum
|
||||||
from .errors import InvalidArgument, ClientException, HTTPException
|
from .errors import InvalidArgument, ClientException, HTTPException
|
||||||
|
141
discord/partial_emoji.py
Normal file
141
discord/partial_emoji.py
Normal file
@ -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)
|
@ -38,7 +38,8 @@ import gc
|
|||||||
from .guild import Guild
|
from .guild import Guild
|
||||||
from .activity import _ActivityTag
|
from .activity import _ActivityTag
|
||||||
from .user import User, ClientUser
|
from .user import User, ClientUser
|
||||||
from .emoji import Emoji, PartialEmoji
|
from .emoji import Emoji
|
||||||
|
from .partial_emoji import PartialEmoji
|
||||||
from .message import Message
|
from .message import Message
|
||||||
from .relationship import Relationship
|
from .relationship import Relationship
|
||||||
from .channel import *
|
from .channel import *
|
||||||
|
Loading…
x
Reference in New Issue
Block a user