diff options
Diffstat (limited to 'discord')
| -rw-r--r-- | discord/__init__.py | 2 | ||||
| -rw-r--r-- | discord/emoji.py | 17 | ||||
| -rw-r--r-- | discord/ext/commands/converter.py | 23 | ||||
| -rw-r--r-- | discord/message.py | 10 | ||||
| -rw-r--r-- | discord/state.py | 8 |
5 files changed, 45 insertions, 15 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 |