Change PartialReactionEmoji to PartialEmoji, add a PartialEmojiConverter
This commit is contained in:
parent
3112e1c17e
commit
04d9dd9c0d
@ -20,7 +20,7 @@ __version__ = '1.0.0a'
|
|||||||
from .client import Client, AppInfo
|
from .client import Client, AppInfo
|
||||||
from .user import User, ClientUser, Profile
|
from .user import User, ClientUser, Profile
|
||||||
from .game import Game
|
from .game import Game
|
||||||
from .emoji import Emoji, PartialReactionEmoji
|
from .emoji import Emoji, PartialEmoji
|
||||||
from .channel import *
|
from .channel import *
|
||||||
from .guild import Guild
|
from .guild import Guild
|
||||||
from .relationship import Relationship
|
from .relationship import Relationship
|
||||||
|
@ -30,8 +30,8 @@ from collections import namedtuple
|
|||||||
from . import utils
|
from . import utils
|
||||||
from .mixins import Hashable
|
from .mixins import Hashable
|
||||||
|
|
||||||
class PartialReactionEmoji(namedtuple('PartialReactionEmoji', 'name id')):
|
class PartialEmoji(namedtuple('PartialEmoji', 'animated name id')):
|
||||||
"""Represents a "partial" reaction emoji.
|
"""Represents a "partial" emoji.
|
||||||
|
|
||||||
This model will be given in two scenarios:
|
This model will be given in two scenarios:
|
||||||
|
|
||||||
@ -61,6 +61,8 @@ class PartialReactionEmoji(namedtuple('PartialReactionEmoji', 'name id')):
|
|||||||
name: :class:`str`
|
name: :class:`str`
|
||||||
The custom emoji name, if applicable, or the unicode codepoint
|
The custom emoji name, if applicable, or the unicode codepoint
|
||||||
of the non-custom emoji.
|
of the non-custom emoji.
|
||||||
|
animated: :class:`bool`
|
||||||
|
Whether the emoji is animated or not.
|
||||||
id: Optional[:class:`int`]
|
id: Optional[:class:`int`]
|
||||||
The ID of the custom emoji, if applicable.
|
The ID of the custom emoji, if applicable.
|
||||||
"""
|
"""
|
||||||
@ -70,7 +72,7 @@ class PartialReactionEmoji(namedtuple('PartialReactionEmoji', 'name id')):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
if self.id is None:
|
if self.id is None:
|
||||||
return self.name
|
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):
|
def is_custom_emoji(self):
|
||||||
"""Checks if this is a custom non-Unicode emoji."""
|
"""Checks if this is a custom non-Unicode emoji."""
|
||||||
@ -85,6 +87,15 @@ class PartialReactionEmoji(namedtuple('PartialReactionEmoji', 'name id')):
|
|||||||
return self.name
|
return self.name
|
||||||
return ':%s:%s' % (self.name, self.id)
|
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):
|
class Emoji(Hashable):
|
||||||
"""Represents a custom emoji.
|
"""Represents a custom emoji.
|
||||||
|
|
||||||
|
@ -35,8 +35,8 @@ from .view import StringView
|
|||||||
__all__ = [ 'Converter', 'MemberConverter', 'UserConverter',
|
__all__ = [ 'Converter', 'MemberConverter', 'UserConverter',
|
||||||
'TextChannelConverter', 'InviteConverter', 'RoleConverter',
|
'TextChannelConverter', 'InviteConverter', 'RoleConverter',
|
||||||
'GameConverter', 'ColourConverter', 'VoiceChannelConverter',
|
'GameConverter', 'ColourConverter', 'VoiceChannelConverter',
|
||||||
'EmojiConverter','CategoryChannelConverter', 'IDConverter',
|
'EmojiConverter', 'PartialEmojiConverter', 'CategoryChannelConverter',
|
||||||
'clean_content' ]
|
'IDConverter', 'clean_content' ]
|
||||||
|
|
||||||
def _get_from_guilds(bot, getter, argument):
|
def _get_from_guilds(bot, getter, argument):
|
||||||
result = None
|
result = None
|
||||||
@ -397,6 +397,25 @@ class EmojiConverter(IDConverter):
|
|||||||
|
|
||||||
return result
|
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):
|
class clean_content(Converter):
|
||||||
"""Converts the argument to mention scrubbed version of
|
"""Converts the argument to mention scrubbed version of
|
||||||
said content.
|
said content.
|
||||||
|
@ -29,7 +29,7 @@ import re
|
|||||||
|
|
||||||
from . import utils, compat
|
from . import utils, compat
|
||||||
from .reaction import Reaction
|
from .reaction import Reaction
|
||||||
from .emoji import Emoji, PartialReactionEmoji
|
from .emoji import Emoji, 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, NotFound
|
from .errors import InvalidArgument, ClientException, HTTPException, NotFound
|
||||||
@ -627,7 +627,7 @@ class Message:
|
|||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
------------
|
------------
|
||||||
emoji: Union[:class:`Emoji`, :class:`Reaction`, :class:`PartialReactionEmoji`, str]
|
emoji: Union[:class:`Emoji`, :class:`Reaction`, :class:`PartialEmoji`, str]
|
||||||
The emoji to react with.
|
The emoji to react with.
|
||||||
|
|
||||||
Raises
|
Raises
|
||||||
@ -647,7 +647,7 @@ class Message:
|
|||||||
|
|
||||||
if isinstance(emoji, Emoji):
|
if isinstance(emoji, Emoji):
|
||||||
emoji = '%s:%s' % (emoji.name, emoji.id)
|
emoji = '%s:%s' % (emoji.name, emoji.id)
|
||||||
elif isinstance(emoji, PartialReactionEmoji):
|
elif isinstance(emoji, PartialEmoji):
|
||||||
emoji = emoji._as_reaction()
|
emoji = emoji._as_reaction()
|
||||||
elif isinstance(emoji, str):
|
elif isinstance(emoji, str):
|
||||||
pass # this is okay
|
pass # this is okay
|
||||||
@ -672,7 +672,7 @@ class Message:
|
|||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
------------
|
------------
|
||||||
emoji: Union[:class:`Emoji`, :class:`Reaction`, :class:`PartialReactionEmoji`, str]
|
emoji: Union[:class:`Emoji`, :class:`Reaction`, :class:`PartialEmoji`, str]
|
||||||
The emoji to remove.
|
The emoji to remove.
|
||||||
member: :class:`abc.Snowflake`
|
member: :class:`abc.Snowflake`
|
||||||
The member for which to remove the reaction.
|
The member for which to remove the reaction.
|
||||||
@ -694,7 +694,7 @@ class Message:
|
|||||||
|
|
||||||
if isinstance(emoji, Emoji):
|
if isinstance(emoji, Emoji):
|
||||||
emoji = '%s:%s' % (emoji.name, emoji.id)
|
emoji = '%s:%s' % (emoji.name, emoji.id)
|
||||||
elif isinstance(emoji, PartialReactionEmoji):
|
elif isinstance(emoji, PartialEmoji):
|
||||||
emoji = emoji._as_reaction()
|
emoji = emoji._as_reaction()
|
||||||
elif isinstance(emoji, str):
|
elif isinstance(emoji, str):
|
||||||
pass # this is okay
|
pass # this is okay
|
||||||
|
@ -26,7 +26,7 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
from .guild import Guild
|
from .guild import Guild
|
||||||
from .user import User, ClientUser
|
from .user import User, ClientUser
|
||||||
from .emoji import Emoji, PartialReactionEmoji
|
from .emoji import Emoji, PartialEmoji
|
||||||
from .message import Message
|
from .message import Message
|
||||||
from .relationship import Relationship
|
from .relationship import Relationship
|
||||||
from .channel import *
|
from .channel import *
|
||||||
@ -372,7 +372,7 @@ class ConnectionState:
|
|||||||
|
|
||||||
emoji_data = data['emoji']
|
emoji_data = data['emoji']
|
||||||
emoji_id = utils._get_as_snowflake(emoji_data, 'id')
|
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)
|
self.dispatch('raw_reaction_add', emoji, message_id, channel_id, user_id)
|
||||||
|
|
||||||
# rich interface here
|
# rich interface here
|
||||||
@ -402,7 +402,7 @@ class ConnectionState:
|
|||||||
|
|
||||||
emoji_data = data['emoji']
|
emoji_data = data['emoji']
|
||||||
emoji_id = utils._get_as_snowflake(emoji_data, 'id')
|
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)
|
self.dispatch('raw_reaction_remove', emoji, message_id, channel_id, user_id)
|
||||||
|
|
||||||
message = self._get_message(message_id)
|
message = self._get_message(message_id)
|
||||||
@ -846,7 +846,7 @@ class ConnectionState:
|
|||||||
try:
|
try:
|
||||||
return self._emojis[emoji_id]
|
return self._emojis[emoji_id]
|
||||||
except KeyError:
|
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):
|
def _upgrade_partial_emoji(self, emoji):
|
||||||
emoji_id = emoji.id
|
emoji_id = emoji.id
|
||||||
|
@ -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.
|
called regardless of the state of the internal message cache.
|
||||||
|
|
||||||
:param emoji: The custom or unicode emoji being reacted to.
|
: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 message_id: The message ID of the message being reacted.
|
||||||
:param int channel_id: The channel ID where the message belongs to.
|
: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.
|
: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.
|
called regardless of the state of the internal message cache.
|
||||||
|
|
||||||
:param emoji: The custom or unicode emoji that got un-reacted.
|
: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 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 channel_id: The channel ID where the message belongs to.
|
||||||
:param int user_id: The user ID of the user who removed the reaction.
|
:param int user_id: The user ID of the user who removed the reaction.
|
||||||
@ -1888,10 +1888,10 @@ Emoji
|
|||||||
.. autoclass:: Emoji()
|
.. autoclass:: Emoji()
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
PartialReactionEmoji
|
PartialEmoji
|
||||||
~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: PartialReactionEmoji()
|
.. autoclass:: PartialEmoji()
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
Role
|
Role
|
||||||
|
@ -173,6 +173,9 @@ Converters
|
|||||||
.. autoclass:: discord.ext.commands.EmojiConverter
|
.. autoclass:: discord.ext.commands.EmojiConverter
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
.. autoclass:: discord.ext.commands.PartialEmojiConverter
|
||||||
|
:members:
|
||||||
|
|
||||||
.. autoclass:: discord.ext.commands.clean_content
|
.. autoclass:: discord.ext.commands.clean_content
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
@ -310,6 +310,7 @@ A lot of discord models work out of the gate as a parameter:
|
|||||||
- :class:`Invite`
|
- :class:`Invite`
|
||||||
- :class:`Game`
|
- :class:`Game`
|
||||||
- :class:`Emoji`
|
- :class:`Emoji`
|
||||||
|
- :class:`PartialEmoji`
|
||||||
- :class:`Colour`
|
- :class:`Colour`
|
||||||
|
|
||||||
Having any of these set as the converter will intelligently convert the argument to the appropriate target type you
|
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:`Emoji` | :class:`~ext.commands.EmojiConverter` |
|
||||||
+-----------------------+-------------------------------------------------+
|
+-----------------------+-------------------------------------------------+
|
||||||
|
| :class:`PartialEmoji` | :class:`~ext.commands.PartialEmojiConverter` |
|
||||||
|
+-----------------------+-------------------------------------------------+
|
||||||
| :class:`Colour` | :class:`~ext.commands.ColourConverter` |
|
| :class:`Colour` | :class:`~ext.commands.ColourConverter` |
|
||||||
+-----------------------+-------------------------------------------------+
|
+-----------------------+-------------------------------------------------+
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user