fix(emojiinfo): use aiohttp.ClientSession
This commit is contained in:
parent
7ae8226ca5
commit
80dcfea95e
1 changed files with 7 additions and 4 deletions
|
@ -1,6 +1,7 @@
|
||||||
import io
|
import io
|
||||||
from typing import Any, Literal
|
from typing import Any, Literal
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
from colorthief import ColorThief
|
from colorthief import ColorThief
|
||||||
from red_commons.logging import RedTraceLogger, getLogger
|
from red_commons.logging import RedTraceLogger, getLogger
|
||||||
|
@ -41,10 +42,11 @@ class EmojiInfo(commands.Cog):
|
||||||
return emoji_url
|
return emoji_url
|
||||||
|
|
||||||
async def fetch_primary_color(self, emoji_url: str) -> discord.Color | None:
|
async def fetch_primary_color(self, emoji_url: str) -> discord.Color | None:
|
||||||
async with self.bot.http_session.get(emoji_url) as response:
|
async with aiohttp.ClientSession() as session:
|
||||||
if response.status != 200:
|
async with session.get(emoji_url) as response:
|
||||||
return None
|
if response.status != 200:
|
||||||
image = await response.read()
|
return None
|
||||||
|
image = await response.read()
|
||||||
dominant_color = ColorThief(io.BytesIO(image)).get_color(quality=1)
|
dominant_color = ColorThief(io.BytesIO(image)).get_color(quality=1)
|
||||||
color = discord.Color.from_rgb(*dominant_color)
|
color = discord.Color.from_rgb(*dominant_color)
|
||||||
return color
|
return color
|
||||||
|
@ -52,6 +54,7 @@ class EmojiInfo(commands.Cog):
|
||||||
@commands.hybrid_command(name="emoji")
|
@commands.hybrid_command(name="emoji")
|
||||||
async def emoji(self, ctx: commands.Context, emoji: str, ephemeral: bool = False) -> None:
|
async def emoji(self, ctx: commands.Context, emoji: str, ephemeral: bool = False) -> None:
|
||||||
"""Retrieve information about an emoji."""
|
"""Retrieve information about an emoji."""
|
||||||
|
await ctx.defer(ephemeral=ephemeral)
|
||||||
emoji: discord.PartialEmoji = discord.PartialEmoji.from_str(value=emoji)
|
emoji: discord.PartialEmoji = discord.PartialEmoji.from_str(value=emoji)
|
||||||
if emoji.is_unicode_emoji():
|
if emoji.is_unicode_emoji():
|
||||||
emoji_url = await self.fetch_twemoji(unicode_emoji=emoji.name)
|
emoji_url = await self.fetch_twemoji(unicode_emoji=emoji.name)
|
||||||
|
|
Loading…
Reference in a new issue