import discord from discord.ext import commands import random from io import BytesIO from PIL import Image, ImageDraw, ImageFont import aiohttp import math class ShipCog(commands.Cog): def __init__(self, bot): self.bot = bot self.session = None async def cog_load(self): self.session = aiohttp.ClientSession() async def cog_unload(self): await self.session.close() @commands.command(name='ship') @commands.cooldown(1, 3, commands.BucketType.user) async def ship(self, ctx, first_user: discord.Member, second_user: discord.Member = None): second_user = second_user or ctx.author first_user_id = first_user.id second_user_id = second_user.id random.seed(first_user_id + second_user_id) percent = random.randint(1, 100) first_name = first_user.name second_name = second_user.name ship_name = first_name[:len(first_name) // 2] + second_name[len(second_name) // 2:] message = f"Ship Name: **{ship_name}**\nLove Percentage: **{percent}%**" ship_image = await self.generate_ship_image(first_user.avatar.url, second_user.avatar.url, percent) file = discord.File(ship_image, filename="ship.png") view = self.get_ship_buttons(ctx.author.id, first_user, second_user) await ctx.send(message, file=file, view=view) async def generate_ship_image(self, first_image_url, second_image_url, percent): async with self.session.get(first_image_url) as response: first_image_data = await response.read() async with self.session.get(second_image_url) as response: second_image_data = await response.read() first_image = Image.open(BytesIO(first_image_data)).convert("RGBA") second_image = Image.open(BytesIO(second_image_data)).convert("RGBA") first_image = first_image.resize((100, 100)) second_image = second_image.resize((100, 100)) combined_width = first_image.width + second_image.width + 100 combined_height = max(first_image.height, second_image.height) combined_image = Image.new("RGBA", (combined_width, combined_height)) combined_image.paste(first_image, (0, 0)) combined_image.paste(second_image, (first_image.width + 100, 0)) draw = ImageDraw.Draw(combined_image) heart_size = 100 heart_position = (first_image.width + 50, (combined_height - heart_size) // 2) heart_shape = self.create_heart_shape(heart_position, heart_size) draw.polygon(heart_shape, fill="red") fill_height = int(heart_size * (percent / 100)) fill_box = (heart_position[0], heart_position[1] + (heart_size - fill_height), heart_position[0] + heart_size, heart_position[1] + heart_size) draw.rectangle(fill_box, fill="red") font = ImageFont.load_default() text = f"{percent}%" text_bbox = draw.textbbox((0, 0), text, font=font) text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1] text_position = (heart_position[0] + (heart_size - text_width) // 2, heart_position[1] + (heart_size - text_height) // 2) draw.text(text_position, text, fill="white", font=font) output_buffer = BytesIO() combined_image.save(output_buffer, "PNG") output_buffer.seek(0) return output_buffer def create_heart_shape(self, position, size): x, y = position top_curve_radius = size * 0.25 bottom_curve_radius = size * 0.5 top_curve_left = [(x + top_curve_radius * math.cos(t), y + top_curve_radius * math.sin(t)) for t in [math.radians(i) for i in range(180, 360)]] top_curve_right = [(x + size - top_curve_radius + top_curve_radius * math.cos(t), y + top_curve_radius * math.sin(t)) for t in [math.radians(i) for i in range(180, 360)]] bottom_curve = [(x + size * 0.5 + bottom_curve_radius * math.cos(t), y + size - bottom_curve_radius + bottom_curve_radius * math.sin(t)) for t in [math.radians(i) for i in range(0, 180)]] return top_curve_left + bottom_curve + top_curve_right def get_ship_buttons(self, author_id, first_user, second_user): class ShipButtons(discord.ui.View): def __init__(self, author_id, first_user, second_user): super().__init__() self.author_id = author_id self.first_user = first_user self.second_user = second_user self.add_item(discord.ui.Button(label="Swipe Left", emoji="⬅", custom_id=f"ship_swipe_left:{author_id}:{first_user.id}", style=discord.ButtonStyle.primary)) if not first_user.bot and not second_user.bot and (first_user.id == author_id or second_user.id == author_id): self.add_item(discord.ui.Button(label="Swipe Right", emoji="➡", custom_id=f"ship_swipe_right:{author_id}:{first_user.id if second_user.id == author_id else second_user.id}", style=discord.ButtonStyle.primary)) @discord.ui.button(label="Swipe Left", emoji="⬅", style=discord.ButtonStyle.primary, custom_id="persistent_view:swipe_left") async def swipe_left_button(self, interaction: discord.Interaction, button: discord.ui.Button): await interaction.response.send_message("You swiped left!") @discord.ui.button(label="Swipe Right", emoji="➡", style=discord.ButtonStyle.primary, custom_id="persistent_view:swipe_right") async def swipe_right_button(self, interaction: discord.Interaction, button: discord.ui.Button): await interaction.response.send_message("You swiped right!") return ShipButtons(author_id, first_user, second_user) async def setup(bot): await bot.add_cog(ShipCog(bot))