Skip to content

Commit 78e0091

Browse files
committed
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.
1 parent ce07e86 commit 78e0091

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

discord/ext/commands/converter.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import discord
4444

4545
from .errors import *
46+
from discord.utils import UNICODE_EMOJIS
4647

4748
if TYPE_CHECKING:
4849
from discord.message import PartialMessageableChannel
@@ -851,7 +852,8 @@ async def convert(self, ctx: Context, argument: str) -> discord.GuildEmoji:
851852
class PartialEmojiConverter(Converter[discord.PartialEmoji]):
852853
"""Converts to a :class:`~discord.PartialEmoji`.
853854
854-
This is done by extracting the animated flag, name and ID from the emoji.
855+
This is done by extracting the animated flag, name, and ID for custom emojis,
856+
or by using the standard Unicode emojis supported by Discord.
855857
856858
.. versionchanged:: 1.5
857859
Raise :exc:`.PartialEmojiConversionFailure` instead of generic :exc:`.BadArgument`
@@ -872,6 +874,14 @@ async def convert(self, ctx: Context, argument: str) -> discord.PartialEmoji:
872874
id=emoji_id,
873875
)
874876

877+
if argument in UNICODE_EMOJIS:
878+
return discord.PartialEmoji.with_state(
879+
ctx.bot._connection,
880+
animated=False,
881+
name=argument,
882+
id=None,
883+
)
884+
875885
raise PartialEmojiConversionFailure(argument)
876886

877887

@@ -1094,7 +1104,11 @@ def get_converter(param: inspect.Parameter) -> Any:
10941104

10951105

10961106
def is_generic_type(tp: Any, *, _GenericAlias: type = _GenericAlias) -> bool:
1097-
return isinstance(tp, type) and issubclass(tp, Generic) or isinstance(tp, _GenericAlias) # type: ignore
1107+
return (
1108+
isinstance(tp, type)
1109+
and issubclass(tp, Generic)
1110+
or isinstance(tp, _GenericAlias)
1111+
) # type: ignore
10981112

10991113

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

discord/partial_emoji.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@
3434
from .asset import Asset, AssetMixin
3535
from .errors import InvalidArgument
3636

37-
with (
38-
importlib.resources.files(__package__)
39-
.joinpath("emojis.json")
40-
.open(encoding="utf-8") as f
41-
):
42-
EMOJIS_MAP = json.load(f)
4337

4438
__all__ = ("PartialEmoji",)
4539

@@ -152,7 +146,7 @@ def from_str(cls: type[PE], value: str) -> PE:
152146
"""
153147
if value.startswith(":") and value.endswith(":") and len(value) > 2:
154148
name = value[1:-1]
155-
if unicode_emoji := EMOJIS_MAP.get(name):
149+
if unicode_emoji := utils.EMOJIS_MAP.get(name):
156150
return cls(name=unicode_emoji, id=None, animated=False)
157151
match = cls._CUSTOM_EMOJI_RE.match(value)
158152
if match is not None:

discord/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import re
3636
import sys
3737
import types
38+
import importlib.resources
3839
import unicodedata
3940
import warnings
4041
from base64 import b64encode
@@ -97,10 +98,21 @@
9798
"generate_snowflake",
9899
"basic_autocomplete",
99100
"filter_params",
101+
"EMOJIS_MAP",
102+
"UNICODE_EMOJIS",
100103
)
101104

102105
DISCORD_EPOCH = 1420070400000
103106

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

105117
class _MissingSentinel:
106118
def __eq__(self, other) -> bool:

0 commit comments

Comments
 (0)