pycord icon indicating copy to clipboard operation
pycord copied to clipboard

fix: support emoji aliases like `:smile:` in PartialEmoji.from_str

Open Lumabots opened this issue 8 months ago • 19 comments

Summary

Information

  • [x] This PR fixes an issue. Fixes #1582
  • [ ] This PR adds something new (e.g. new method or parameters).
  • [ ] This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • [ ] This PR is not a code change (e.g. documentation, README, typehinting, examples, ...).

Checklist

  • [x] I have searched the open pull requests for duplicates.
  • [x] If code changes were made then they have been tested.
    • [x] I have updated the documentation to reflect the changes.
  • [x] If type: ignore comments were used, a comment is also left explaining why.
  • [x] I have updated the changelog to include these changes.

Lumabots avatar May 02 '25 21:05 Lumabots

fix of #1582

Lumabots avatar May 02 '25 21:05 Lumabots

so my current fix is using the emoji lib : https://pypi.org/project/emoji/ how should i do, just add this to the requirement txt ?

Lumabots avatar May 02 '25 21:05 Lumabots

@Lumabots What concerns me is that the emoji lib does not necessarily always include all of discord's nomenclature for markdown emojis. Notably, see this and this reddit threads.

Also, if this fixes #1582, please edit your pr description to add Fixes #1582 so that the pr is linked to the issue.

Paillat-dev avatar May 03 '25 10:05 Paillat-dev

@Lumabots What concerns me is that the emoji lib does not necessarily always include all of discord's nomenclature for markdown emojis. Notably, see this and this reddit threads.

Also, if this fixes #1582, please edit your pr description to add Fixes #1582 so that the pr is linked to the issue.

I have been using this for years and I never really had any issue of missing emoji, but I'll try to do a test with every emoji of discord to see if there is lacking one. Also in case there is lacking one do you have an alternative ?

Lumabots avatar May 03 '25 10:05 Lumabots

Im thinking else I can do a json file with every discord emoji so I can just replace it

Lumabots avatar May 03 '25 10:05 Lumabots

@Lumabots dmed you on discord a huge json from discord with all the emojis. Maybe it can be helpful to you in some way ? Specifically the nameToEmoji and emoji keys (I think you can ignore the other two). However i am not sure storing a json like that in the lib is the best strategy.

Paillat-dev avatar May 03 '25 11:05 Paillat-dev

I'm not sure this is really ideal; users are expected to pass actual unicode emojis, not :name:

NeloBlivion avatar May 03 '25 14:05 NeloBlivion

Lala added a bug tag in the related issue so I assumed it was

Lumabots avatar May 03 '25 14:05 Lumabots

should be ready for review using Paillait Lib dismoji

Lumabots avatar May 03 '25 23:05 Lumabots

I'm strongly against adding a new dependency for this.

Dorukyum avatar May 04 '25 08:05 Dorukyum

To be honest I agree that adding a dependency is not a good idea. To be clear, while this issue made me create that lib, I did not create it with the intent of it specifically be used in py-cord. Maybe a note should simply be added indicating to use a custom converter with a link to this pr &/ the issue.

An alternative could be to modify PartialEmoji to support storing an emoji exclusively based on a :some_emoji: string, however there wouldn't be anything to make sure that emoji is valid.

Paillat-dev avatar May 04 '25 10:05 Paillat-dev

Why not just use ur system so we do a requests at first to get the emoji list from discord, and then just replace it directly ? So no need to install a new one

Lumabots avatar May 04 '25 14:05 Lumabots

so i implemented a way to just download paillait file and then replace the emoji, i just need to find a way to load this file wihout blocking the bot. if you want to proceed differently tell me

Lumabots avatar May 04 '25 15:05 Lumabots

@Lumabots cor security reasons it is not possible to have it download at startup. Instead, you would need to download the file and store it next to the python code and load from there, and the file should be updated periodically.

To open the file you can use pathlib like this:

import json
from pathlib import Path

EMOJIS_MAP_PATH = Path(__file__).parent / "emojis.json"

with EMOJIS_MAP_PATH.open("r", encoding="utf-8") as f:
    EMOJIS_MAP = json.loads(f.read())

Paillat-dev avatar May 05 '25 10:05 Paillat-dev

should be ready for review

Lumabots avatar May 05 '25 11:05 Lumabots

We'd have to maintain this huge json of emojis. It also just makes the package larger.

i managed to drop it to 250kb, so i think the size is no longer an issue, i dont think maintain it is gonna be an issue, paillat create a script to get all of that data, and discord does not upload new emojis often

Lumabots avatar May 25 '25 14:05 Lumabots

If the issue is updating the blob I can handle that every 6 months when it's needed, ~~and if the issue is the size, maybe @Lumabots try preprocessing the json to see if you can make it even smaller (generate the name to surrogates and use that as json directly)~~

Paillat-dev avatar May 25 '25 14:05 Paillat-dev

Screenshot_2025-05-25-16-36-27-594_com.discord.png

Paillat-dev avatar May 25 '25 14:05 Paillat-dev

it as been tested and it works

Lumabots avatar May 29 '25 07:05 Lumabots

@Lumabots The emoji json was updated, do you mind re-creating it with the updated data ? Also if you're using a script to convert the json feel free to send it here for future reference, thx !

Paillat-dev avatar Jun 19 '25 17:06 Paillat-dev

@Lumabots The emoji json was updated, do you mind re-creating it with the updated data ? Also if you're using a script to convert the json feel free to send it here for future reference, thx !

import json
from pathlib import Path

EMOJIS_MAP_PATH = Path(__file__).parent / "emojis.json"


with EMOJIS_MAP_PATH.open("r", encoding="utf-8") as f:
    data = json.load(f)

EMOJIS_MAP = {}
for emoji_entry in data["emojis"]:
    for name in emoji_entry["names"]:
        EMOJIS_MAP[name] = emoji_entry["surrogates"]

with open("emojis.json", "w", encoding="utf-8") as f:
    json.dump(EMOJIS_MAP, f, ensure_ascii=False)

Lumabots avatar Jun 19 '25 19:06 Lumabots

@Lumabots please also recreate the emoji file, discord changed some emojis, other than that I think we're gtg

~~again ? i already change it yesterday~~ done

Lumabots avatar Jun 20 '25 06:06 Lumabots