feat(emojiinfo): added most of the cog's functionality

This commit is contained in:
SeaswimmerTheFsh 2024-05-08 14:26:03 -04:00
parent d87c37239d
commit b910d8c614
Signed by: cswimr
GPG key ID: 5D671B5D03D65A7F
3 changed files with 206 additions and 152 deletions

View file

@ -1,5 +1,8 @@
import io
from typing import Any, Literal
import discord
from colorthief import ColorThief
from red_commons.logging import RedTraceLogger, getLogger
from redbot.core import commands
from redbot.core.bot import Red
@ -29,7 +32,42 @@ class EmojiInfo(commands.Cog):
]
return "\n".join(text)
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
@commands.hybrid_command(name="emoji")
async def emoji(self, ctx: commands.Context, emoji: str, ephemeral: bool = False) -> None:
"""Retrieve information about an emoji."""
await ctx.send(content=f"Emoji: {emoji}", ephemeral=ephemeral)
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)