Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 0ab6c8b

Browse files
committed
Feat: Add dalle-3 and dalle-2 image generation commands
1 parent 25a6d0d commit 0ab6c8b

2 files changed

Lines changed: 113 additions & 3 deletions

File tree

projects/Chat-GPT-Discord-Bot/Chat_GPT_Function.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,28 @@ def gpt(model: str, prompt: str, sys_prompt: str, temp: float):
2525
top_p=1
2626
)
2727
output = response.choices[0].message.content.strip()
28-
return output
28+
return output
29+
30+
def dalle3(prompt: str, quality: str, size: str, style: str):
31+
client = OpenAI(api_key= gpt_api_key)
32+
response = client.images.generate(
33+
model = "dall-e-3",
34+
prompt = prompt,
35+
size = size,
36+
quality = quality,
37+
style = style,
38+
n=1,
39+
)
40+
image_url = response.data[0].url
41+
return image_url
42+
43+
def dalle2(prompt: str, size: str):
44+
client = OpenAI(api_key= gpt_api_key)
45+
response = client.images.generate(
46+
model = "dall-e-2",
47+
prompt = prompt,
48+
size = size,
49+
n=1,
50+
)
51+
image_url = response.data[0].url
52+
return image_url

projects/Chat-GPT-Discord-Bot/main.py

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import sys
44
import os
55
from dotenv import load_dotenv
6-
from Chat_GPT_Function import gpt
6+
from Chat_GPT_Function import gpt, dalle3, dalle2
77
import json
8+
from datetime import datetime, timedelta
9+
import time
810

911
load_dotenv(override=True)
1012

@@ -39,7 +41,7 @@
3941

4042
class MyClient(discord.Client):
4143
def __init__(self, *, intents: discord.Intents):
42-
super().__init__(intents=intents)
44+
super().__init__(heartbeat_timeout=90, intents=intents) # heartbeat_timeout=90 prevents the bot disconnecting from discord when running the dalle commands
4345
# A CommandTree is a special type that holds all the application command
4446
# state required to make it work. This is a separate class because it
4547
# allows all the extra states to be opt-in.
@@ -469,6 +471,90 @@ async def send(interaction: discord.Interaction, text: str): # noqa: F811
469471
await interaction.followup.send(
470472
"An error occurred while processing the command."
471473
)
474+
475+
# -------------------------- DALLE 3 ----------------------------------
476+
@client.tree.command(name="dalle_3", description="Generates an image with DALL·E 3")
477+
@app_commands.describe(prompt="Describe the image you want DALL·E 3 to create")
478+
@app_commands.describe(img_dimensions="Must be either 1024x1024, 1792x1024, or 1024x1792 for dall-e-3")
479+
@app_commands.describe(img_quality="Must be either hd or standard. HD = images with finer details and greater consistency across the image.")
480+
@app_commands.describe(img_style="Must be either vivid or natural. Vivid = hyper-real and dramatic images. Natural = more natural, less hyper-real looking images.")
481+
async def send(interaction: discord.Interaction, prompt: str, img_dimensions: str, img_quality: str, img_style: str): # noqa: F811
482+
try:
483+
await interaction.response.defer(
484+
ephemeral=False
485+
) # Defer the response to prevent command timeout
486+
487+
# Get the current time
488+
current_time = datetime.now()
489+
490+
# Add 1 hour to the current time
491+
future_time = current_time + timedelta(hours=1)
492+
493+
if img_dimensions.lower() not in ("1024x1024", "1792x1024", "1024x1792"):
494+
await interaction.followup.send(
495+
"Invalid image dimension. Must be either 1024x1024, 1792x1024, or 1024x1792."
496+
)
497+
return
498+
else:
499+
img_dimensions = img_dimensions.lower()
500+
501+
if img_quality.lower() not in ("hd", "standard"):
502+
await interaction.followup.send(
503+
"Invalid image quality. Must be either hd or standard."
504+
)
505+
return
506+
else:
507+
img_quality = img_quality.lower()
508+
509+
if img_style.lower() not in ("vivid", "natural"):
510+
await interaction.followup.send(
511+
"Invalid image style. Must be either vivid or natural."
512+
)
513+
return
514+
else:
515+
img_style = img_style.lower()
516+
517+
# Send as followup message
518+
await interaction.followup.send(f"{dalle3(prompt, img_quality, img_dimensions, img_style)} IMAGE LINK EXPIRES IN <t:{int(time.mktime(future_time.timetuple()))}:R>")
519+
except Exception as e:
520+
# Handle exceptions
521+
print(f"An error occurred: {str(e)}")
522+
await interaction.followup.send(
523+
"An error occurred while processing the command."
524+
)
525+
526+
# -------------------------- DALLE 2 ----------------------------------
527+
@client.tree.command(name="dalle_2", description="Generates an image with DALL·E 2")
528+
@app_commands.describe(prompt="Describe the image you want DALL·E 2 to create")
529+
@app_commands.describe(img_dimensions="Must be either 256x256, 512x512, or 1024x1024 for dall-e-2")
530+
async def send(interaction: discord.Interaction, prompt: str, img_dimensions: str): # noqa: F811
531+
try:
532+
await interaction.response.defer(
533+
ephemeral=False
534+
) # Defer the response to prevent command timeout
535+
536+
# Get the current time
537+
current_time = datetime.now()
538+
539+
# Add 1 hour to the current time
540+
future_time = current_time + timedelta(hours=1)
541+
542+
if img_dimensions.lower() not in ("256x256", "512x512", "1024x1024"):
543+
await interaction.followup.send(
544+
"Invalid image dimension. Must be either 256x256, 512x512, or 1024x1024."
545+
)
546+
return
547+
else:
548+
img_dimensions = img_dimensions.lower()
549+
550+
# Send as followup message
551+
await interaction.followup.send(f"{dalle2(prompt, img_dimensions)} IMAGE LINK EXPIRES IN <t:{int(time.mktime(future_time.timetuple()))}:R>")
552+
except Exception as e:
553+
# Handle exceptions
554+
print(f"An error occurred: {str(e)}")
555+
await interaction.followup.send(
556+
"An error occurred while processing the command."
557+
)
472558

473559

474560
client.run(token)

0 commit comments

Comments
 (0)