aboutsummaryrefslogtreecommitdiff
path: root/discord/partial_emoji.py
diff options
context:
space:
mode:
authorRapptz <[email protected]>2021-05-28 04:54:39 -0400
committerRapptz <[email protected]>2021-05-28 05:34:21 -0400
commit6874aa73c41c4b1ee8d7630171a65bb8e39d10e5 (patch)
treef589c9efd9b66e1cb6d58b8516e1311e24a142c0 /discord/partial_emoji.py
parentAdd support for reading SelectMenu components from messages (diff)
downloaddiscord.py-6874aa73c41c4b1ee8d7630171a65bb8e39d10e5.tar.xz
discord.py-6874aa73c41c4b1ee8d7630171a65bb8e39d10e5.zip
Add PartialEmoji.from_str helper
Diffstat (limited to 'discord/partial_emoji.py')
-rw-r--r--discord/partial_emoji.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/discord/partial_emoji.py b/discord/partial_emoji.py
index c9ddd650..fc40ff55 100644
--- a/discord/partial_emoji.py
+++ b/discord/partial_emoji.py
@@ -25,6 +25,7 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations
from typing import Any, Dict, Optional, TYPE_CHECKING, Type, TypeVar
+import re
from .asset import Asset, AssetMixin
from .errors import InvalidArgument
@@ -88,6 +89,8 @@ class PartialEmoji(_EmojiTag, AssetMixin):
__slots__ = ('animated', 'name', 'id', '_state')
+ _CUSTOM_EMOJI_RE = re.compile(r'<?(?P<animated>a)?:?(?P<name>[A-Za-z0-9\_]+):(?P<id>[0-9]{13,20})>?')
+
if TYPE_CHECKING:
id: Optional[int]
@@ -105,6 +108,41 @@ class PartialEmoji(_EmojiTag, AssetMixin):
name=data.get('name', ''),
)
+ @classmethod
+ def from_str(cls: Type[PE], value: str) -> PE:
+ """Converts a Discord string representation of an emoji to a :class:`PartialEmoji`.
+
+ The formats accepted are:
+
+ - ``a:name:id``
+ - ``<a:name:id>``
+ - ``name:id``
+ - ``<:name:id>``
+
+ If the format does not match then it is assumed to be a unicode emoji.
+
+ .. versionadded:: 2.0
+
+ Parameters
+ ------------
+ value: :class:`str`
+ The string representation of an emoji.
+
+ Returns
+ --------
+ :class:`PartialEmoji`
+ The partial emoji from this string.
+ """
+ match = cls._CUSTOM_EMOJI_RE.match(value)
+ if match is not None:
+ groups = match.groupdict()
+ animated = bool(groups['animated'])
+ emoji_id = int(groups['id'])
+ name = groups['name']
+ return cls(name=name, animated=animated, id=emoji_id)
+
+ return cls(name=value, id=None, animated=False)
+
def to_dict(self) -> Dict[str, Any]:
o: Dict[str, Any] = {'name': self.name}
if self.id: