8000 feat: Added support for emoji aliases like `:smile:` in PartialEmoji.from_str (redo) by Lulalaby · Pull Request #2815 · Pycord-Development/pycord · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Added support for emoji aliases like :smile: in PartialEmoji.from_str (redo) #2815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2761](https://github.com/Pycord-Development/pycord/pull/2761))
- Updated `valid_locales` to support `in` and `es-419`.
([#2767](https://github.com/Pycord-Development/pycord/pull/2767))
- Added support for emoji aliases like `:smile:` in PartialEmoji.from_str.
([#2815](https://github.com/Pycord-Development/pycord/pull/2815))
- Fixed `Webhook.edit` not working with `attachments=[]`.
([#2779](https://github.com/Pycord-Development/pycord/pull/2779))
- Fixed GIF-based `Sticker` returning the wrong `url`.
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include LICENSE
include requirements.txt
include discord/bin/*.dll
include discord/py.typed
include discord/emojis.json

prune .github
prune docs
Expand Down
1 change: 1 addition & 0 deletions discord/emojis.json

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions discord/ext/commands/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
)

import discord
from discord.utils import UNICODE_EMOJIS

from .errors import *

Expand Down Expand Up @@ -851,7 +852,8 @@ async def convert(self, ctx: Context, argument: str) -> discord.GuildEmoji:
class PartialEmojiConverter(Converter[discord.PartialEmoji]):
"""Converts to a :class:`~discord.PartialEmoji`.

This is done by extracting the animated flag, name and ID from the emoji.
This is done by extracting the animated flag, name, and ID for custom emojis,
or by using the standard Unicode emojis supported by Discord.

.. versionchanged:: 1.5
Raise :exc:`.PartialEmojiConversionFailure` instead of generic :exc:`.BadArgument`
Expand All @@ -872,6 +874,14 @@ async def convert(self, ctx: Context, argument: str) -> discord.PartialEmoji:
id=emoji_id,
)

if argument in UNICODE_EMOJIS:
return discord.PartialEmoji.with_state(
ctx.bot._connection,
animated=False,
name=argument,
id=None,
)

raise PartialEmojiConversionFailure(argument)


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


def is_generic_type(tp: Any, *, _GenericAlias: type = _GenericAlias) -> bool:
return isinstance(tp, type) and issubclass(tp, Generic) or isinstance(tp, _GenericAlias) # type: ignore
return (
isinstance(tp, type)
and issubclass(tp, Generic)
or isinstance(tp, _GenericAlias)
) # type: ignore


CONVERTER_MAPPING: dict[type[Any], Any] = {
Expand Down
7 changes: 6 additions & 1 deletion discord/partial_emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def from_str(cls: type[PE], value: str) -> PE:
- ``name:id``
- ``<:name:id>``

If the format does not match then it is assumed to be a unicode emoji.
If the format does not match then it is assumed to be a unicode emoji, either as Unicode characters or as a Discord alias (``:smile:``).

.. versionadded:: 2.0

Expand All @@ -141,6 +141,11 @@ def from_str(cls: type[PE], value: str) -> PE:
:class:`PartialEmoji`
The partial emoji from this string.
"""
if unicode_emoji := utils.EMOJIS_MAP.get(
value.removeprefix(":").removesuffix(":")
):
return cls(name=unicode_emoji, id=None, animated=False)

match = cls._CUSTOM_EMOJI_RE.match(value)
if match is not None:
groups = match.groupdict()
Expand Down
12 changes: 12 additions & 0 deletions discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import collections.abc
import datetime
import functools
import importlib.resources
import itertools
import json
import re
Expand Down Expand Up @@ -97,10 +98,21 @@
"generate_snowflake",
"basic_autocomplete",
"filter_params",
"EMOJIS_MAP",
"UNICODE_EMOJIS",
)

DISCORD_EPOCH = 1420070400000

with (
importlib.resources.files(__package__)
.joinpath("emojis.json")
.open(encoding="utf-8") as f
):
EMOJIS_MAP = json.load(f)

UNICODE_EMOJIS = set(EMOJIS_MAP.values())


class _MissingSentinel:
def __eq__(self, other) -> bool:
Expand Down
0