2024-05-08 14:26:03 -04:00
|
|
|
import io
|
2024-05-08 13:47:33 -04:00
|
|
|
from typing import Any, Literal
|
|
|
|
|
2024-05-08 14:26:03 -04:00
|
|
|
import discord
|
|
|
|
from colorthief import ColorThief
|
2024-05-08 13:47:33 -04:00
|
|
|
from red_commons.logging import RedTraceLogger, getLogger
|
2024-05-08 13:43:33 -04:00
|
|
|
from redbot.core import commands
|
2024-05-08 13:47:33 -04:00
|
|
|
from redbot.core.bot import Red
|
|
|
|
from redbot.core.utils.chat_formatting import humanize_list
|
2024-05-08 13:43:33 -04:00
|
|
|
|
|
|
|
|
2024-05-08 13:54:55 -04:00
|
|
|
class EmojiInfo(commands.Cog):
|
2024-05-08 13:43:33 -04:00
|
|
|
"""Retrieve information about emojis."""
|
|
|
|
|
2024-05-08 13:47:33 -04:00
|
|
|
__author__: list[str] = ["SeaswimmerTheFsh"]
|
|
|
|
__version__: str = "1.0.0"
|
|
|
|
__documentation__: str = "https://seacogs.coastalcommits.com/emoji/"
|
|
|
|
|
|
|
|
def __init__(self, bot: Red) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.bot: Red = bot
|
|
|
|
self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.Emoji")
|
|
|
|
|
|
|
|
def format_help_for_context(self, ctx: commands.Context) -> str:
|
|
|
|
pre_processed: Any | Literal[''] = super().format_help_for_context(ctx) or ""
|
|
|
|
n: Literal['\n'] | Literal[''] = "\n" if "\n\n" not in pre_processed else ""
|
|
|
|
text: list[str] = [
|
|
|
|
f"{pre_processed}{n}",
|
|
|
|
f"Cog Version: **{self.__version__}**",
|
|
|
|
f"Author: {humanize_list(items=self.__author__)}",
|
|
|
|
f"Documentation: {self.__documentation__}",
|
|
|
|
]
|
|
|
|
return "\n".join(text)
|
|
|
|
|
2024-05-08 14:26:03 -04:00
|
|
|
async def fetch_twemoji(self, unicode_emoji) -> str:
|
|
|
|
base_url = "https://cdn.jsdelivr.net/gh/jdecked/twemoji@latest/assets/72x72/"
|
|
|
|
emoji_codepoint = "-".join([hex(ord(char))[2:] for char in unicode_emoji])
|
|
|
|
segments = emoji_codepoint.split("-")
|
|
|
|
valid_segments = [seg for seg in segments if len(seg) >= 4]
|
|
|
|
emoji_url = f"{base_url}{valid_segments[0]}.png"
|
|
|
|
return emoji_url
|
|
|
|
|
|
|
|
async def fetch_primary_color(self, emoji_url: str) -> discord.Color | None:
|
|
|
|
async with self.bot.http_session.get(emoji_url) as response:
|
|
|
|
if response.status != 200:
|
|
|
|
return None
|
|
|
|
image = await response.read()
|
|
|
|
dominant_color = ColorThief(io.BytesIO(image)).get_color(quality=1)
|
|
|
|
color = discord.Color.from_rgb(*dominant_color)
|
|
|
|
return color
|
|
|
|
|
2024-05-08 13:50:46 -04:00
|
|
|
@commands.hybrid_command(name="emoji")
|
2024-05-08 13:58:58 -04:00
|
|
|
async def emoji(self, ctx: commands.Context, emoji: str, ephemeral: bool = False) -> None:
|
2024-05-08 13:43:33 -04:00
|
|
|
"""Retrieve information about an emoji."""
|
2024-05-08 14:26:03 -04:00
|
|
|
emoji: discord.PartialEmoji = discord.PartialEmoji.from_str(value=emoji)
|
|
|
|
if emoji.is_unicode_emoji():
|
|
|
|
emoji_url = await self.fetch_twemoji(unicode_emoji=emoji.name)
|
|
|
|
else:
|
|
|
|
emoji_url = emoji.url
|
|
|
|
string: str = (
|
|
|
|
f"Name: {emoji}\n"
|
|
|
|
f"ID: {emoji.id}\n" if emoji.id else ""
|
|
|
|
f"Native: {emoji.is_unicode_emoji()}\n"
|
|
|
|
f"Animated: {emoji.animated}\n"
|
|
|
|
f"Markdown: `:{emoji.name}:`\n"
|
|
|
|
f"URL: [Click Here]({emoji_url})"
|
|
|
|
)
|
|
|
|
if await ctx.embed_requested():
|
|
|
|
embed = embed = discord.Embed(title="Emoji Information", description=string, color = await self.fetch_primary_color(emoji_url) or await ctx.embed_color)
|
|
|
|
embed.set_thumbnail(url=emoji_url)
|
|
|
|
await ctx.send(embed=embed, ephemeral=ephemeral)
|
|
|
|
else:
|
|
|
|
await ctx.send(content=string, ephemeral=ephemeral)
|