aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGorialis <[email protected]>2018-01-02 07:59:51 +0900
committerRapptz <[email protected]>2018-01-06 17:32:25 -0500
commit04d9dd9c0dc82b4d870c6269ecc3a9e46cd7292e (patch)
tree4f3060a57302181cf9287786be2655aad5f985a0
parentAdd intersphinx (diff)
downloaddiscord.py-04d9dd9c0dc82b4d870c6269ecc3a9e46cd7292e.tar.xz
discord.py-04d9dd9c0dc82b4d870c6269ecc3a9e46cd7292e.zip
Change PartialReactionEmoji to PartialEmoji, add a PartialEmojiConverter
-rw-r--r--discord/__init__.py2
-rw-r--r--discord/emoji.py17
-rw-r--r--discord/ext/commands/converter.py23
-rw-r--r--discord/message.py10
-rw-r--r--discord/state.py8
-rw-r--r--docs/api.rst8
-rw-r--r--docs/ext/commands/api.rst3
-rw-r--r--docs/ext/commands/commands.rst3
8 files changed, 55 insertions, 19 deletions
diff --git a/discord/__init__.py b/discord/__init__.py
index 7973cd4a..20c81853 100644
--- a/discord/__init__.py
+++ b/discord/__init__.py
@@ -20,7 +20,7 @@ __version__ = '1.0.0a'
from .client import Client, AppInfo
from .user import User, ClientUser, Profile
from .game import Game
-from .emoji import Emoji, PartialReactionEmoji
+from .emoji import Emoji, PartialEmoji
from .channel import *
from .guild import Guild
from .relationship import Relationship
diff --git a/discord/emoji.py b/discord/emoji.py
index a04a2ad5..82a4a69a 100644
--- a/discord/emoji.py
+++ b/discord/emoji.py
@@ -30,8 +30,8 @@ from collections import namedtuple
from . import utils
from .mixins import Hashable
-class PartialReactionEmoji(namedtuple('PartialReactionEmoji', 'name id')):
- """Represents a "partial" reaction emoji.
+class PartialEmoji(namedtuple('PartialEmoji', 'animated name id')):
+ """Represents a "partial" emoji.
This model will be given in two scenarios:
@@ -61,6 +61,8 @@ class PartialReactionEmoji(namedtuple('PartialReactionEmoji', 'name id')):
name: :class:`str`
The custom emoji name, if applicable, or the unicode codepoint
of the non-custom emoji.
+ animated: :class:`bool`
+ Whether the emoji is animated or not.
id: Optional[:class:`int`]
The ID of the custom emoji, if applicable.
"""
@@ -70,7 +72,7 @@ class PartialReactionEmoji(namedtuple('PartialReactionEmoji', 'name id')):
def __str__(self):
if self.id is None:
return self.name
- return '<:%s:%s>' % (self.name, self.id)
+ return '<%s:%s:%s>' % ('a' if self.animated else '', self.name, self.id)
def is_custom_emoji(self):
"""Checks if this is a custom non-Unicode emoji."""
@@ -85,6 +87,15 @@ class PartialReactionEmoji(namedtuple('PartialReactionEmoji', 'name id')):
return self.name
return ':%s:%s' % (self.name, self.id)
+ @property
+ def url(self):
+ """Returns a URL version of the emoji, if it is custom."""
+ if self.is_unicode_emoji():
+ return None
+
+ _format = 'gif' if self.animated else 'png'
+ return "https://cdn.discordapp.com/emojis/{0.id}.{1}".format(self, _format)
+
class Emoji(Hashable):
"""Represents a custom emoji.
diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py
index e2397ad2..9e3718b0 100644
--- a/discord/ext/commands/converter.py
+++ b/discord/ext/commands/converter.py
@@ -35,8 +35,8 @@ from .view import StringView
__all__ = [ 'Converter', 'MemberConverter', 'UserConverter',
'TextChannelConverter', 'InviteConverter', 'RoleConverter',
'GameConverter', 'ColourConverter', 'VoiceChannelConverter',
- 'EmojiConverter','CategoryChannelConverter', 'IDConverter',
- 'clean_content' ]
+ 'EmojiConverter', 'PartialEmojiConverter', 'CategoryChannelConverter',
+ 'IDConverter', 'clean_content' ]
def _get_from_guilds(bot, getter, argument):
result = None
@@ -397,6 +397,25 @@ class EmojiConverter(IDConverter):
return result
+class PartialEmojiConverter(Converter):
+ """Converts to a :class:`PartialEmoji`.
+
+
+ This is done by extracting the animated flag, name and ID from the emoji.
+ """
+ @asyncio.coroutine
+ def convert(self, ctx, argument):
+ match = re.match(r'<(a?):([a-zA-Z0-9\_]+):([0-9]+)>$', argument)
+
+ if match:
+ emoji_animated = bool(match.group(1))
+ emoji_name = match.group(2)
+ emoji_id = int(match.group(3))
+
+ return discord.PartialEmoji(animated=emoji_animated, name=emoji_name, id=emoji_id)
+
+ raise BadArgument('Couldn\'t convert "{}" to PartialEmoji.'.format(argument))
+
class clean_content(Converter):
"""Converts the argument to mention scrubbed version of
said content.
diff --git a/discord/message.py b/discord/message.py
index ea2d852b..875defe6 100644
--- a/discord/message.py
+++ b/discord/message.py
@@ -29,7 +29,7 @@ import re
from . import utils, compat
from .reaction import Reaction
-from .emoji import Emoji, PartialReactionEmoji
+from .emoji import Emoji, PartialEmoji
from .calls import CallMessage
from .enums import MessageType, try_enum
from .errors import InvalidArgument, ClientException, HTTPException, NotFound
@@ -627,7 +627,7 @@ class Message:
Parameters
------------
- emoji: Union[:class:`Emoji`, :class:`Reaction`, :class:`PartialReactionEmoji`, str]
+ emoji: Union[:class:`Emoji`, :class:`Reaction`, :class:`PartialEmoji`, str]
The emoji to react with.
Raises
@@ -647,7 +647,7 @@ class Message:
if isinstance(emoji, Emoji):
emoji = '%s:%s' % (emoji.name, emoji.id)
- elif isinstance(emoji, PartialReactionEmoji):
+ elif isinstance(emoji, PartialEmoji):
emoji = emoji._as_reaction()
elif isinstance(emoji, str):
pass # this is okay
@@ -672,7 +672,7 @@ class Message:
Parameters
------------
- emoji: Union[:class:`Emoji`, :class:`Reaction`, :class:`PartialReactionEmoji`, str]
+ emoji: Union[:class:`Emoji`, :class:`Reaction`, :class:`PartialEmoji`, str]
The emoji to remove.
member: :class:`abc.Snowflake`
The member for which to remove the reaction.
@@ -694,7 +694,7 @@ class Message:
if isinstance(emoji, Emoji):
emoji = '%s:%s' % (emoji.name, emoji.id)
- elif isinstance(emoji, PartialReactionEmoji):
+ elif isinstance(emoji, PartialEmoji):
emoji = emoji._as_reaction()
elif isinstance(emoji, str):
pass # this is okay
diff --git a/discord/state.py b/discord/state.py
index b145c7ec..470c4eeb 100644
--- a/discord/state.py
+++ b/discord/state.py
@@ -26,7 +26,7 @@ DEALINGS IN THE SOFTWARE.
from .guild import Guild
from .user import User, ClientUser
-from .emoji import Emoji, PartialReactionEmoji
+from .emoji import Emoji, PartialEmoji
from .message import Message
from .relationship import Relationship
from .channel import *
@@ -372,7 +372,7 @@ class ConnectionState:
emoji_data = data['emoji']
emoji_id = utils._get_as_snowflake(emoji_data, 'id')
- emoji = PartialReactionEmoji(id=emoji_id, name=emoji_data['name'])
+ emoji = PartialEmoji(animated=emoji_data['animated'], id=emoji_id, name=emoji_data['name'])
self.dispatch('raw_reaction_add', emoji, message_id, channel_id, user_id)
# rich interface here
@@ -402,7 +402,7 @@ class ConnectionState:
emoji_data = data['emoji']
emoji_id = utils._get_as_snowflake(emoji_data, 'id')
- emoji = PartialReactionEmoji(id=emoji_id, name=emoji_data['name'])
+ emoji = PartialEmoji(animated=emoji_data['animated'], id=emoji_id, name=emoji_data['name'])
self.dispatch('raw_reaction_remove', emoji, message_id, channel_id, user_id)
message = self._get_message(message_id)
@@ -846,7 +846,7 @@ class ConnectionState:
try:
return self._emojis[emoji_id]
except KeyError:
- return PartialReactionEmoji(id=emoji_id, name=data['name'])
+ return PartialEmoji(animated=data['animated'], id=emoji_id, name=data['name'])
def _upgrade_partial_emoji(self, emoji):
emoji_id = emoji.id
diff --git a/docs/api.rst b/docs/api.rst
index 16254a75..214fd8c8 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -304,7 +304,7 @@ to handle it, which defaults to print a traceback and ignoring the exception.
called regardless of the state of the internal message cache.
:param emoji: The custom or unicode emoji being reacted to.
- :type emoji: :class:`PartialReactionEmoji`
+ :type emoji: :class:`PartialEmoji`
:param int message_id: The message ID of the message being reacted.
:param int channel_id: The channel ID where the message belongs to.
:param int user_id: The user ID of the user who did the reaction.
@@ -328,7 +328,7 @@ to handle it, which defaults to print a traceback and ignoring the exception.
called regardless of the state of the internal message cache.
:param emoji: The custom or unicode emoji that got un-reacted.
- :type emoji: :class:`PartialReactionEmoji`
+ :type emoji: :class:`PartialEmoji`
:param int message_id: The message ID of the message being un-reacted.
:param int channel_id: The channel ID where the message belongs to.
:param int user_id: The user ID of the user who removed the reaction.
@@ -1888,10 +1888,10 @@ Emoji
.. autoclass:: Emoji()
:members:
-PartialReactionEmoji
+PartialEmoji
~~~~~~~~~~~~~~~~~~~~~~
-.. autoclass:: PartialReactionEmoji()
+.. autoclass:: PartialEmoji()
:members:
Role
diff --git a/docs/ext/commands/api.rst b/docs/ext/commands/api.rst
index 9fae2355..0f61be2f 100644
--- a/docs/ext/commands/api.rst
+++ b/docs/ext/commands/api.rst
@@ -173,6 +173,9 @@ Converters
.. autoclass:: discord.ext.commands.EmojiConverter
:members:
+.. autoclass:: discord.ext.commands.PartialEmojiConverter
+ :members:
+
.. autoclass:: discord.ext.commands.clean_content
:members:
diff --git a/docs/ext/commands/commands.rst b/docs/ext/commands/commands.rst
index d1957438..336ed5de 100644
--- a/docs/ext/commands/commands.rst
+++ b/docs/ext/commands/commands.rst
@@ -310,6 +310,7 @@ A lot of discord models work out of the gate as a parameter:
- :class:`Invite`
- :class:`Game`
- :class:`Emoji`
+- :class:`PartialEmoji`
- :class:`Colour`
Having any of these set as the converter will intelligently convert the argument to the appropriate target type you
@@ -339,6 +340,8 @@ converter is given below:
+-----------------------+-------------------------------------------------+
| :class:`Emoji` | :class:`~ext.commands.EmojiConverter` |
+-----------------------+-------------------------------------------------+
+| :class:`PartialEmoji` | :class:`~ext.commands.PartialEmojiConverter` |
++-----------------------+-------------------------------------------------+
| :class:`Colour` | :class:`~ext.commands.ColourConverter` |
+-----------------------+-------------------------------------------------+