Skip to content

Commit ced9d0b

Browse files
authored
Create remix.py
1 parent befa6e4 commit ced9d0b

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

cogs/remix.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import io
2+
import discord
3+
from discord.ext import commands
4+
from discord import app_commands, ui
5+
from helpers.ai import edit_image
6+
from PIL import Image
7+
8+
class Button(ui.Button):
9+
def __init__(self, label='Regenerate', disabled=False):
10+
super().__init__(label=label, disabled=disabled)
11+
12+
async def callback(self, interaction: discord.Interaction):
13+
await interaction.response.defer()
14+
self.view.button_state('Regenerating...', True)
15+
await interaction.message.edit(view=self.view)
16+
await self.view.edit_image()
17+
18+
class View(ui.View):
19+
def __init__(self, prompt, image_bytes, message, api_content):
20+
super().__init__()
21+
self.prompt = prompt
22+
self.image_bytes = image_bytes
23+
self.message = message
24+
self.api_content = api_content
25+
self.add_item(Button())
26+
27+
def button_state(self, label, disabled):
28+
self.clear_items()
29+
self.add_item(Button(label=label, disabled=disabled))
30+
31+
async def edit_image(self):
32+
try:
33+
remix_image = edit_image(self.image_bytes, self.api_content)
34+
if remix_image and isinstance(remix_image, bytes):
35+
img_file = io.BytesIO(remix_image)
36+
await self.message.edit(attachments=[discord.File(img_file, "image.png")])
37+
img_file.close()
38+
else:
39+
await self.message.edit(content="Failed to regenerate the image.")
40+
except Exception:
41+
pass
42+
finally:
43+
self.button_state('Regenerate', False)
44+
await self.message.edit(view=self)
45+
46+
class Remix(commands.Cog):
47+
def __init__(self, bot: commands.Bot):
48+
self.bot = bot
49+
50+
async def check_image_dimensions(self, interaction, image_bytes):
51+
try:
52+
image = Image.open(io.BytesIO(image_bytes))
53+
width, height = image.size
54+
55+
if width < 320 or width > 1536 or height < 320 or height > 1536:
56+
await interaction.response.send_message("Image dimensions must be between 320 and 1536 pixels.", ephemeral=True)
57+
return False
58+
59+
return True
60+
except Exception:
61+
return False
62+
63+
@app_commands.command(name="remix", description="Remix an image")
64+
@app_commands.describe(image="The image to remix", prompt="What changes you would like in the image?")
65+
async def remix(self, interaction: discord.Interaction, image: discord.Attachment, prompt: str):
66+
image_bytes = await image.read()
67+
68+
await interaction.response.defer()
69+
message = await interaction.followup.send("Remixing...")
70+
71+
try:
72+
if not await self.check_image_dimensions(interaction, image_bytes):
73+
return
74+
75+
remix_image = edit_image(image_bytes, prompt)
76+
77+
if remix_image and isinstance(remix_image, bytes):
78+
img_file = io.BytesIO(remix_image)
79+
await message.edit(content=None, attachments=[discord.File(img_file, "image.png")])
80+
view = View(prompt, image_bytes, message, prompt)
81+
await message.edit(view=view)
82+
img_file.close()
83+
else:
84+
await message.edit(content="Failed to generate the image.")
85+
except Exception:
86+
await message.edit(content="An error occurred while processing your request.")
87+
88+
async def setup(bot: commands.Bot):
89+
await bot.add_cog(Remix(bot))

0 commit comments

Comments
 (0)