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:30:07 -04:00
|
|
|
import aiohttp
|
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 15:48:18 -04:00
|
|
|
from redbot.core import app_commands, commands
|
2024-05-08 13:47:33 -04:00
|
|
|
from redbot.core.bot import Red
|
2024-05-08 14:34:28 -04:00
|
|
|
from redbot.core.utils.chat_formatting import bold, humanize_list
|
2024-05-08 13:43:33 -04:00
|
|
|
|
2024-05-08 15:34:04 -04:00
|
|
|
from .model import PartialEmoji
|
|
|
|
|
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"
|
2024-05-08 15:48:18 -04:00
|
|
|
__documentation__: str = "https://seacogs.coastalcommits.com/emojiinfo/"
|
2024-05-08 13:47:33 -04:00
|
|
|
|
|
|
|
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:
|
2024-05-08 14:30:07 -04:00
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.get(emoji_url) as response:
|
|
|
|
if response.status != 200:
|
|
|
|
return None
|
|
|
|
image = await response.read()
|
2024-05-08 14:26:03 -04:00
|
|
|
dominant_color = ColorThief(io.BytesIO(image)).get_color(quality=1)
|
|
|
|
color = discord.Color.from_rgb(*dominant_color)
|
|
|
|
return color
|
|
|
|
|
2024-05-08 15:58:26 -04:00
|
|
|
async def get_emoji_info(self, emoji: PartialEmoji) -> tuple[str, str]:
|
2024-05-08 14:26:03 -04:00
|
|
|
if emoji.is_unicode_emoji():
|
2024-05-08 14:45:46 -04:00
|
|
|
try:
|
|
|
|
emoji_url = await self.fetch_twemoji(unicode_emoji=emoji.name)
|
2024-05-08 16:02:56 -04:00
|
|
|
except IndexError as e:
|
2024-05-08 15:58:26 -04:00
|
|
|
raise e
|
2024-05-08 14:26:03 -04:00
|
|
|
else:
|
|
|
|
emoji_url = emoji.url
|
2024-05-08 14:38:39 -04:00
|
|
|
|
2024-05-17 00:46:25 -04:00
|
|
|
if emoji.id is not None:
|
2024-05-08 14:35:47 -04:00
|
|
|
emoji_id = f"{bold('ID:')} `{emoji.id}`\n"
|
2024-05-08 14:38:39 -04:00
|
|
|
markdown = f"`<{'a' if emoji.animated else ''}:{emoji.name}:{emoji.id}>`"
|
2024-05-08 15:34:04 -04:00
|
|
|
name = f"{bold('Name:')} {emoji.name}\n"
|
|
|
|
aliases = ""
|
2024-05-08 15:40:58 -04:00
|
|
|
group = ""
|
2024-05-08 14:34:28 -04:00
|
|
|
else:
|
|
|
|
emoji_id = ""
|
2024-05-08 14:38:39 -04:00
|
|
|
markdown = f"`{emoji}`"
|
2024-05-08 15:34:04 -04:00
|
|
|
name = f"{bold('Name:')} {emoji.aliases.pop(0)}\n"
|
|
|
|
aliases = f"{bold('Aliases:')} {', '.join(emoji.aliases)}\n" if emoji.aliases else ""
|
2024-05-08 15:40:58 -04:00
|
|
|
group = f"{bold('Group:')} {emoji.group}\n"
|
2024-05-08 14:38:39 -04:00
|
|
|
|
2024-05-08 15:58:26 -04:00
|
|
|
return (
|
2024-05-08 15:34:04 -04:00
|
|
|
f"{name}"
|
2024-05-08 14:34:28 -04:00
|
|
|
f"{emoji_id}"
|
2024-05-08 14:35:47 -04:00
|
|
|
f"{bold('Native:')} {emoji.is_unicode_emoji()}\n"
|
2024-05-08 15:40:58 -04:00
|
|
|
f"{group}"
|
2024-05-08 15:34:04 -04:00
|
|
|
f"{aliases}"
|
2024-05-08 14:35:47 -04:00
|
|
|
f"{bold('Animated:')} {emoji.animated}\n"
|
2024-05-08 14:38:39 -04:00
|
|
|
f"{bold('Markdown:')} {markdown}\n"
|
2024-05-08 14:35:47 -04:00
|
|
|
f"{bold('URL:')} [Click Here]({emoji_url})"
|
2024-05-08 15:58:26 -04:00
|
|
|
), emoji_url
|
|
|
|
|
|
|
|
@app_commands.command(name="emoji")
|
|
|
|
@app_commands.describe(
|
|
|
|
emoji="What emoji would you like to get information on?",
|
|
|
|
ephemeral="Would you like the response to be hidden?"
|
|
|
|
)
|
2024-05-17 00:44:19 -04:00
|
|
|
async def emoji_slash(self, interaction: discord.Interaction, emoji: str, ephemeral: bool = True) -> None:
|
2024-05-08 15:58:26 -04:00
|
|
|
"""Retrieve information about an emoji."""
|
2024-05-08 15:59:10 -04:00
|
|
|
await interaction.response.defer(ephemeral=ephemeral)
|
2024-05-08 15:58:26 -04:00
|
|
|
|
|
|
|
try:
|
2024-05-08 16:02:56 -04:00
|
|
|
emoji: PartialEmoji = PartialEmoji.from_str(self, value=emoji)
|
2024-05-08 15:58:26 -04:00
|
|
|
string, emoji_url, = await self.get_emoji_info(emoji)
|
2024-05-17 00:48:14 -04:00
|
|
|
self.logger.verbose(f"Emoji:\n{string}")
|
2024-05-08 16:02:56 -04:00
|
|
|
except (IndexError, UnboundLocalError):
|
2024-05-08 15:58:26 -04:00
|
|
|
return await interaction.followup.send("Please provide a valid emoji!")
|
|
|
|
|
|
|
|
if await self.bot.embed_requested(channel=interaction.channel):
|
|
|
|
embed = embed = discord.Embed(title="Emoji Information", description=string, color = await self.fetch_primary_color(emoji_url) or await self.bot.get_embed_color(interaction.channel))
|
|
|
|
embed.set_thumbnail(url=emoji_url)
|
|
|
|
|
|
|
|
await interaction.followup.send(embed=embed)
|
|
|
|
else:
|
|
|
|
await interaction.followup.send(content=string)
|
|
|
|
|
|
|
|
@commands.command(name="emoji")
|
|
|
|
async def emoji(self, ctx: commands.Context, *, emoji: str) -> None:
|
|
|
|
"""Retrieve information about an emoji."""
|
|
|
|
try:
|
2024-05-08 16:02:56 -04:00
|
|
|
emoji: PartialEmoji = PartialEmoji.from_str(self, value=emoji)
|
2024-05-08 15:58:26 -04:00
|
|
|
string, emoji_url, = await self.get_emoji_info(emoji)
|
2024-05-17 00:57:30 -04:00
|
|
|
self.logger.verbose(f"Emoji:\n{string}")
|
2024-05-08 16:02:56 -04:00
|
|
|
except (IndexError, UnboundLocalError):
|
2024-05-08 15:58:26 -04:00
|
|
|
return await ctx.send("Please provide a valid emoji!")
|
2024-05-08 14:38:39 -04:00
|
|
|
|
2024-05-08 14:26:03 -04:00
|
|
|
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)
|
2024-05-08 14:38:39 -04:00
|
|
|
|
2024-05-08 15:58:26 -04:00
|
|
|
await ctx.send(embed=embed)
|
2024-05-08 14:26:03 -04:00
|
|
|
else:
|
2024-05-08 15:58:26 -04:00
|
|
|
await ctx.send(content=string)
|