kbdev
kbdev
import json
from typing import Union
import discord
from PIL import Image, ImageDraw, ImageFont
from discord.ext import commands
import aiohttp
import io
import emoji
COLORS = {
(0, 0, 0): "⬛",
(0, 0, 255): "🟦",
(255, 0, 0): "🟥",
(255, 255, 0): "🟨",
(255, 165, 0): "🟧",
(255, 255, 255): "⬜",
(0, 255, 0): "🟩",
}
def find_closest_emoji(color):
c = sorted(list(COLORS), key=lambda k: euclidean_distance(color, k))
return COLORS[c[0]]
emoji = ""
for y in range(HEIGHT):
for x in range(WIDTH):
emoji += find_closest_emoji(small_img.getpixel((x, y)))
emoji += "\n"
return emoji
emoji_string = emoji.emojize(emoji_string)
byte_io = io.BytesIO()
img.save(byte_io, 'PNG')
byte_io.seek(0)
return byte_io
class Emojify(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.cooldown(1, 20, commands.BucketType.user)
async def emojify(self, ctx, url: Union[discord.Member, str], size: int = 14):
if ctx.channel.id not in ALLOWED_CHANNELS:
await ctx.send("❌ This command can only be used in specific channels.")
return
if isinstance(url, discord.Member):
url = url.display_avatar.url
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as r:
if r.status == 200:
image_data = await r.read()
image = Image.open(io.BytesIO(image_data)).convert("RGB")
emoji_string = emojify_image(image, size)
await message.reply(emoji_string)
await ctx.send(file=discord.File(emoji_image_file,
filename="emoji_image.png"))
else:
await ctx.send(f"❌ Failed to fetch the image. Received
status code: {r.status}")
except aiohttp.ClientError as e:
await ctx.send(f"❌ Network error: Unable to fetch image. Error:
{str(e)}")
except Exception as e:
await ctx.send(f"❌ An unexpected error occurred: {str(e)}")
@emojify.error
async def emojify_error(self, ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(f"❌ Please wait {error.retry_after:.2f} seconds before
using this command again.")