Skip to content

Commit 4ccb0c0

Browse files
LulalabyPaillat-devLumabotspre-commit-ci[bot]
authored
feat: Added support for emoji aliases like :smile: in PartialEmoji.from_str (redo) (#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 (#2819) * Update converter.py * fix: enhance PartialEmojiConverter to support direct emoji names * update doc * Revert 2814 revert 2774 emoji (#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:` (#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]>
1 parent 58c0dfc commit 4ccb0c0

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-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`.

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ include LICENSE
33
include requirements.txt
44
include discord/bin/*.dll
55
include discord/py.typed
6+
include discord/emojis.json
67

78
prune .github
89
prune docs

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

@@ -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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def from_str(cls: type[PE], value: str) -> PE:
127127
- ``name:id``
128128
- ``<:name:id>``
129129
130-
If the format does not match then it is assumed to be a unicode emoji.
130+
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:``).
131131
132132
.. versionadded:: 2.0
133133
@@ -141,6 +141,11 @@ def from_str(cls: type[PE], value: str) -> PE:
141141
:class:`PartialEmoji`
142142
The partial emoji from this string.
143143
"""
144+
if unicode_emoji := utils.EMOJIS_MAP.get(
145+
value.removeprefix(":").removesuffix(":")
146+
):
147+
return cls(name=unicode_emoji, id=None, animated=False)
148+
144149
match = cls._CUSTOM_EMOJI_RE.match(value)
145150
if match is not None:
146151
groups = match.groupdict()

discord/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import collections.abc
3131
import datetime
3232
import functools
33+
import importlib.resources
3334
import itertools
3435
import json
3536
import re
@@ -101,6 +102,15 @@
101102

102103
DISCORD_EPOCH = 1420070400000
103104

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

105115
class _MissingSentinel:
106116
def __eq__(self, other) -> bool:

0 commit comments

Comments
 (0)