-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update random_cog.py to add random image generation command
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import datetime | ||
import discord | ||
from discord import app_commands, ui | ||
from discord.ext import commands | ||
|
||
from utils import * | ||
from constants import * | ||
|
||
class RandomImage(commands.Cog): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
|
||
async def cog_load(self): | ||
await self.bot.wait_until_ready() | ||
|
||
@app_commands.command(name="random", description="Generate Random AI Images") | ||
@app_commands.guild_only() | ||
@app_commands.checks.cooldown(1, 15) | ||
@app_commands.describe( | ||
height="Height of the image", | ||
width="Width of the image", | ||
negative="The things not to include in the image", | ||
nologo="Remove the logo", | ||
private="Only you can see the generated Image if set to True", | ||
) | ||
async def random_image_command( | ||
self, | ||
interaction: discord.Interaction, | ||
width: int = 1000, | ||
height: int = 1000, | ||
negative: str | None = None, | ||
nologo: bool = True, | ||
private: bool = False, | ||
): | ||
await interaction.response.defer(thinking=True, ephemeral=private) | ||
|
||
if width < 16 or height < 16: | ||
raise DimensionTooSmallError("Width and Height must be greater than 16") | ||
|
||
start = datetime.datetime.now() | ||
|
||
dic, image = await generate_image( | ||
"Random Prompt", width, height, negative, False, nologo, None, private | ||
) | ||
|
||
image_file = discord.File(image, filename="image.png") | ||
|
||
if dic["nsfw"]: | ||
image_file.filename = f"SPOILER_{image_file.filename}" | ||
|
||
time_taken = datetime.datetime.now() - start | ||
|
||
embed = discord.Embed( | ||
title=f"Prompt", | ||
description=f"```{dic['enhanced_prompt'][:4000]+"..." if len(dic['enhanced_prompt'])>= 4000 else dic['enhanced_prompt']}```", | ||
timestamp=datetime.datetime.now(datetime.timezone.utc), | ||
url=dic["bookmark_url"] | ||
) | ||
|
||
embed.add_field(name="Seed", value=f"```{dic['seed']}```", inline=True) | ||
|
||
embed.add_field( | ||
name="Time Taken", | ||
value=f"```{round(time_taken.total_seconds(), 2)} s```", | ||
inline=True, | ||
) | ||
|
||
embed.set_image(url=f"attachment://image.png") | ||
|
||
embed.set_footer(text=f"Generated by {interaction.user}") | ||
|
||
if private: | ||
await interaction.followup.send(embed=embed, ephemeral=True) | ||
return | ||
else: | ||
response = await interaction.followup.send( | ||
embed=embed, file=image_file | ||
) | ||
|
||
@random_image_command.error | ||
async def random_image_command_error( | ||
self, interaction: discord.Interaction, error: app_commands.AppCommandError | ||
): | ||
if isinstance(error, app_commands.CommandOnCooldown): | ||
embed = await generate_error_message( | ||
interaction, | ||
error, | ||
cooldown_configuration=["- ```1 time every 15 seconds```"], | ||
) | ||
await interaction.response.send_message(embed=embed, ephemeral=True) | ||
else: | ||
embed = discord.Embed( | ||
title="An error occurred while generating a random image", | ||
description=f"```cmd\n{error}\n```", | ||
color=discord.Color.red(), | ||
) | ||
try: | ||
await interaction.followup.send(embed=embed, ephemeral=True) | ||
except: | ||
try: | ||
await interaction.edit_original_response(embed=embed) | ||
except: | ||
await interaction.response.send_message(embed=embed, ephemeral=True) | ||
|
||
|
||
async def setup(bot): | ||
await bot.add_cog(RandomImage(bot)) | ||
print("Random Image cog loaded") |