Skip to content

Commit 2dfe1dc

Browse files
LulalabyPaillat-dev
authored andcommitted
feat: Added support for emoji aliases like :smile: in PartialEmoji.from_str (redo) (Pycord-Development#2815)
* Revert "Revert "fix: support emoji aliases like `:smile:` in PartialEmoji.fro…" This reverts commit 8619b69. * Update discord/partial_emoji.py Co-authored-by: Paillat <[email protected]> Signed-off-by: Lala Sabathil <[email protected]> * Update CHANGELOG.md Signed-off-by: Lala Sabathil <[email protected]> * Update CHANGELOG.md Signed-off-by: Lala Sabathil <[email protected]> * Update MANIFEST.in Signed-off-by: Lala Sabathil <[email protected]> * feat: allow usage of unicode emoji in PartialEmojiConverter (Pycord-Development#2819) * Update converter.py * fix: enhance PartialEmojiConverter to support direct emoji names * update doc * Revert 2814 revert 2774 emoji (Pycord-Development#2820) * Add Unicode emoji support to PartialEmojiConverter PartialEmojiConverter now recognizes standard Unicode emojis using a new UNICODE_EMOJIS set loaded from emojis.json. The emoji mapping and set are moved to discord.utils for reuse, and references in partial_emoji.py are updated accordingly. * style(pre-commit): auto fixes from pre-commit.com hooks * Update converter.py Signed-off-by: Lumouille <[email protected]> * Update converter.py * fix(utils): update UNICODE_EMOJIS to use values from EMOJIS_MAP --------- Signed-off-by: Lumouille <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * added back support of smile and `:smile:` (Pycord-Development#2822) * Add Unicode emoji support to PartialEmojiConverter PartialEmojiConverter now recognizes standard Unicode emojis using a new UNICODE_EMOJIS set loaded from emojis.json. The emoji mapping and set are moved to discord.utils for reuse, and references in partial_emoji.py are updated accordingly. * style(pre-commit): auto fixes from pre-commit.com hooks * Update converter.py Signed-off-by: Lumouille <[email protected]> * Update converter.py * fix(utils): update UNICODE_EMOJIS to use values from EMOJIS_MAP * fix(partial_emoji): simplify emoji name extraction by using removeprefix and removesuffix * style(pre-commit): auto fixes from pre-commit.com hooks --------- Signed-off-by: Lumouille <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * Update discord/partial_emoji.py Signed-off-by: Lala Sabathil <[email protected]> * style(pre-commit): auto fixes from pre-commit.com hooks * Update discord/partial_emoji.py Co-authored-by: Paillat <[email protected]> Signed-off-by: Lala Sabathil <[email protected]> * Update CHANGELOG.md Co-authored-by: Lumouille <[email protected]> Signed-off-by: Lala Sabathil <[email protected]> * style(pre-commit): auto fixes from pre-commit.com hooks * Update discord/utils.py Co-authored-by: Lumouille <[email protected]> Signed-off-by: Lala Sabathil <[email protected]> --------- Signed-off-by: Lala Sabathil <[email protected]> Signed-off-by: Lala Sabathil <[email protected]> Signed-off-by: Lumouille <[email protected]> Co-authored-by: Paillat <[email protected]> Co-authored-by: Lumouille <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paillat <[email protected]> (cherry picked from commit 4ccb0c0)
1 parent 16058a9 commit 2dfe1dc

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ These changes are available on the `master` branch, but have not yet been releas
123123
([#2761](https://github.com/Pycord-Development/pycord/pull/2761))
124124
- Updated `valid_locales` to support `in` and `es-419`.
125125
([#2767](https://github.com/Pycord-Development/pycord/pull/2767))
126+
- Added support for emoji aliases like `:smile:` in PartialEmoji.from_str. Also applied
127+
the same logic in PartialEmojiConverter.
128+
([#2815](https://github.com/Pycord-Development/pycord/pull/2815))
126129
- Fixed `Webhook.edit` not working with `attachments=[]`.
127130
([#2779](https://github.com/Pycord-Development/pycord/pull/2779))
128131
- Fixed GIF-based `Sticker` returning the wrong `url`.

discord/emojis.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

discord/ext/commands/converter.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242

4343
import discord
44+
from discord.utils import UNICODE_EMOJIS
4445

4546
from .errors import *
4647

@@ -811,7 +812,8 @@ async def convert(self, ctx: Context, argument: str) -> discord.GuildEmoji:
811812
class PartialEmojiConverter(Converter[discord.PartialEmoji]):
812813
"""Converts to a :class:`~discord.PartialEmoji`.
813814
814-
This is done by extracting the animated flag, name and ID from the emoji.
815+
This is done by extracting the animated flag, name, and ID for custom emojis,
816+
or by using the standard Unicode emojis supported by Discord.
815817
816818
.. versionchanged:: 1.5
817819
Raise :exc:`.PartialEmojiConversionFailure` instead of generic :exc:`.BadArgument`
@@ -832,6 +834,14 @@ async def convert(self, ctx: Context, argument: str) -> discord.PartialEmoji:
832834
id=emoji_id,
833835
)
834836

837+
if argument in UNICODE_EMOJIS:
838+
return discord.PartialEmoji.with_state(
839+
ctx.bot._connection,
840+
animated=False,
841+
name=argument,
842+
id=None,
843+
)
844+
835845
raise PartialEmojiConversionFailure(argument)
836846

837847

@@ -1040,7 +1050,11 @@ def get_converter(param: inspect.Parameter) -> Any:
10401050

10411051

10421052
def is_generic_type(tp: Any, *, _GenericAlias: type = _GenericAlias) -> bool:
1043-
return isinstance(tp, type) and issubclass(tp, Generic) or isinstance(tp, _GenericAlias) # type: ignore
1053+
return (
1054+
isinstance(tp, type)
1055+
and issubclass(tp, Generic)
1056+
or isinstance(tp, _GenericAlias)
1057+
) # type: ignore
10441058

10451059

10461060
CONVERTER_MAPPING: dict[type[Any], Any] = {

discord/partial_emoji.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def from_str(cls: type[PE], value: str) -> PE:
123123
- ``name:id``
124124
- ``<:name:id>``
125125
126-
If the format does not match then it is assumed to be a unicode emoji.
126+
If the format does not match then it is assumed to be a Unicode emoji block, either as Unicode characters or as a Discord alias (``:smile:``).
127127
128128
.. versionadded:: 2.0
129129
@@ -137,6 +137,11 @@ def from_str(cls: type[PE], value: str) -> PE:
137137
:class:`PartialEmoji`
138138
The partial emoji from this string.
139139
"""
140+
if unicode_emoji := utils.EMOJIS_MAP.get(
141+
value.removeprefix(":").removesuffix(":")
142+
):
143+
return cls(name=unicode_emoji, id=None, animated=False)
144+
140145
match = cls._CUSTOM_EMOJI_RE.match(value)
141146
if match is not None:
142147
groups = match.groupdict()

discord/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import datetime
3232
from enum import Enum, auto
3333
import functools
34+
import importlib.resources
3435
import itertools
3536
import json
3637
import re
@@ -102,6 +103,15 @@
102103

103104
DISCORD_EPOCH = 1420070400000
104105

106+
with (
107+
importlib.resources.files(__package__)
108+
.joinpath("emojis.json")
109+
.open(encoding="utf-8") as f
110+
):
111+
EMOJIS_MAP = json.load(f)
112+
113+
UNICODE_EMOJIS = set(EMOJIS_MAP.values())
114+
105115

106116
class Undefined(Enum):
107117
MISSING = auto()

0 commit comments

Comments
 (0)