feat(emojiinfo): added aliases and groups

This commit is contained in:
Seaswimmer 2024-05-08 15:34:04 -04:00
parent 59097b676d
commit 9ed16a44f2
Signed by: cswimr
GPG key ID: 5D671B5D03D65A7F
3 changed files with 5406 additions and 2 deletions

5309
emojiinfo/data/emojis.json Normal file

File diff suppressed because it is too large Load diff

View file

@ -9,6 +9,8 @@ from redbot.core import commands
from redbot.core.bot import Red
from redbot.core.utils.chat_formatting import bold, humanize_list
from .model import PartialEmoji
class EmojiInfo(commands.Cog):
"""Retrieve information about emojis."""
@ -56,7 +58,7 @@ class EmojiInfo(commands.Cog):
"""Retrieve information about an emoji."""
await ctx.defer(ephemeral=ephemeral)
emoji: discord.PartialEmoji = discord.PartialEmoji.from_str(value=emoji)
emoji: PartialEmoji = PartialEmoji.from_str(self, value=emoji)
if emoji.is_unicode_emoji():
try:
@ -70,14 +72,19 @@ class EmojiInfo(commands.Cog):
if emoji.id:
emoji_id = f"{bold('ID:')} `{emoji.id}`\n"
markdown = f"`<{'a' if emoji.animated else ''}:{emoji.name}:{emoji.id}>`"
name = f"{bold('Name:')} {emoji.name}\n"
aliases = ""
else:
emoji_id = ""
markdown = f"`{emoji}`"
name = f"{bold('Name:')} {emoji.aliases.pop(0)}\n"
aliases = f"{bold('Aliases:')} {', '.join(emoji.aliases)}\n" if emoji.aliases else ""
string: str = (
f"{bold('Name:')} {emoji.name}\n"
f"{name}"
f"{emoji_id}"
f"{bold('Native:')} {emoji.is_unicode_emoji()}\n"
f"{aliases}"
f"{bold('Animated:')} {emoji.animated}\n"
f"{bold('Markdown:')} {markdown}\n"
f"{bold('URL:')} [Click Here]({emoji_url})"

88
emojiinfo/model.py Normal file
View file

@ -0,0 +1,88 @@
import json
import discord
from redbot.core import commands, data_manager
class PartialEmoji(discord.PartialEmoji):
"""Represents a "partial" emoji. Subclasses `discord.PartialEmoji`
.. container:: operations
.. describe:: x == y
Checks if two emoji are the same.
.. describe:: x != y
Checks if two emoji are not the same.
.. describe:: hash(x)
Return the emoji's hash.
.. describe:: str(x)
Returns the emoji rendered for discord.
Attributes
-----------
name: Optional[:class:`str`]
The custom emoji name, if applicable, or the unicode codepoint
of the non-custom emoji. This can be ``None`` if the emoji
got deleted (e.g. removing a reaction with a deleted emoji).
animated: :class:`bool`
Whether the emoji is animated or not.
id: Optional[:class:`int`]
The ID of the custom emoji, if applicable.
group: Optional[:class:`str`]
The group name of the emoji if it is a native emoji.
"""
def __init__(self, *, name: str, animated: bool = False, id: int | None = None, group: str | None = None, aliases: list | None = None) -> None:
super().__init__(name=name, animated=animated, id=id)
self.group = group
self.aliases = aliases
@classmethod
def from_str(cls, coginstance: commands.Cog, value: str) -> "PartialEmoji":
"""Converts a Discord string representation of an emoji to a :class:`PartialEmoji`.
The formats accepted are:
- ``a:name:id``
- ``<a:name:id>``
- ``name:id``
- ``<:name:id>``
If the format does not match then it is assumed to be a unicode emoji.
.. versionadded:: 2.0
Parameters
------------
value: :class:`str`
The string representation of an emoji.
Returns
--------
:class:`PartialEmoji`
The partial emoji from this string.
"""
match = cls._CUSTOM_EMOJI_RE.match(value)
if match is not None:
groups = match.groupdict()
animated = bool(groups['animated'])
emoji_id = int(groups['id'])
name = groups['name']
return cls(name=name, animated=animated, id=emoji_id)
emojis: dict = json.load(data_manager.bundled_data_path(coginstance) / "emojis.json")
emoji_aliases = []
for dict_name, group in emojis.items:
for key, value in group.items:
if value == name:
emoji_group = dict_name
if key not in emoji_aliases:
emoji_aliases.append(key)
return cls(name=name, animated=animated, id=emoji_id, group=emoji_group, aliases=emoji_aliases)