|
3 | 3 | import sys |
4 | 4 | import os |
5 | 5 | from dotenv import load_dotenv |
6 | | -from Chat_GPT_Function import gpt |
| 6 | +from Chat_GPT_Function import gpt, dalle3, dalle2 |
7 | 7 | import json |
| 8 | +from datetime import datetime, timedelta |
| 9 | +import time |
8 | 10 |
|
9 | 11 | load_dotenv(override=True) |
10 | 12 |
|
|
39 | 41 |
|
40 | 42 | class MyClient(discord.Client): |
41 | 43 | 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 |
43 | 45 | # A CommandTree is a special type that holds all the application command |
44 | 46 | # state required to make it work. This is a separate class because it |
45 | 47 | # allows all the extra states to be opt-in. |
@@ -469,6 +471,90 @@ async def send(interaction: discord.Interaction, text: str): # noqa: F811 |
469 | 471 | await interaction.followup.send( |
470 | 472 | "An error occurred while processing the command." |
471 | 473 | ) |
| 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 | + ) |
472 | 558 |
|
473 | 559 |
|
474 | 560 | client.run(token) |
0 commit comments