artintel235 commited on
Commit
7011453
·
verified ·
1 Parent(s): 696839f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -45
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import discord
2
  from discord import app_commands
3
  import os
 
4
  import asyncio
5
- import aiohttp
6
 
7
  # --- Environment Variables & Setup ---
8
  DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
@@ -17,7 +18,7 @@ if not DISCORD_BOT_TOKEN or not GLIF_API_TOKEN:
17
 
18
  # --- Discord Bot Setup ---
19
  intents = discord.Intents.default()
20
- # intents.message_content = True # Uncomment if you need message content intent
21
  client = discord.Client(intents=intents)
22
  tree = app_commands.CommandTree(client)
23
 
@@ -57,7 +58,7 @@ async def generate_image_async(prompt, aspect_ratio):
57
  except aiohttp.ClientError as e:
58
  return f"API request failed: {e}"
59
 
60
- # --- Slash Commands ---
61
  @tree.command(
62
  name="generate", description="Generates an image based on a text prompt"
63
  )
@@ -77,59 +78,51 @@ async def generate_command(
77
  prompt: str,
78
  aspect_ratio: app_commands.Choice[str],
79
  ):
80
- """Generates an image based on the user's prompt and aspect ratio."""
81
- print(f"Received /generate command from {interaction.user.name}")
82
-
83
  try:
84
- await interaction.response.defer(ephemeral=False)
85
- print("Interaction deferred")
86
-
87
- image_url_or_error = await generate_image_async(
88
- prompt, aspect_ratio.value
89
- )
90
- print(f"GLIF API response: {image_url_or_error}")
91
 
92
- if image_url_or_error.startswith("http"):
93
- await interaction.followup.send(
94
- f"Here's your generated image based on the prompt '{prompt}' with aspect ratio {aspect_ratio.name}:\n{image_url_or_error}"
95
- )
96
- else:
97
- await interaction.followup.send(
98
- f"Sorry, I couldn't generate an image. {image_url_or_error}"
99
- )
100
 
101
- print("Followup sent")
 
 
 
102
 
103
- except Exception as e:
104
- print(f"An error occurred: {e}")
105
- await interaction.followup.send("An error occurred while processing your request.")
 
 
 
 
 
106
 
107
  @tree.command(name="hello", description="Says hello!")
108
- async def hello_command(interaction: discord.Interaction):
109
- await interaction.response.send_message("Hello there! I am functional!")
110
-
111
- @tree.command(name="testgen", description="Test command")
112
- async def testgen_command(interaction: discord.Interaction):
113
- print("TestGen command received")
114
- await interaction.response.defer()
115
- print("TestGen deferred")
116
- await interaction.followup.send("TestGen working")
117
-
118
- # --- Bot Event Handlers ---
119
- @client.event
120
  async def on_ready():
121
- await tree.sync() # Sync commands globally
122
- # For specific server: await tree.sync(guild=discord.Object(id=YOUR_GUILD_ID))
123
  print("Bot is ready!")
124
- print(f"Logged in as {client.user} (ID: {client.user.id})")
125
- print(f"Guilds: {client.guilds}")
126
- print("------")
127
 
128
- # --- Main Function ---
 
129
  async def main():
 
130
  async with client:
 
131
  await client.start(DISCORD_BOT_TOKEN)
 
 
 
 
 
132
 
133
- # --- Run the Bot ---
134
  if __name__ == "__main__":
135
- asyncio.run(main())
 
 
1
  import discord
2
  from discord import app_commands
3
  import os
4
+ import requests
5
  import asyncio
6
+ import aiohttp # Use aiohttp for asynchronous HTTP requests
7
 
8
  # --- Environment Variables & Setup ---
9
  DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
 
18
 
19
  # --- Discord Bot Setup ---
20
  intents = discord.Intents.default()
21
+ # intents.message_content = True # If you need message content intent
22
  client = discord.Client(intents=intents)
23
  tree = app_commands.CommandTree(client)
24
 
 
58
  except aiohttp.ClientError as e:
59
  return f"API request failed: {e}"
60
 
61
+ # --- Discord Slash Commands ---
62
  @tree.command(
63
  name="generate", description="Generates an image based on a text prompt"
64
  )
 
78
  prompt: str,
79
  aspect_ratio: app_commands.Choice[str],
80
  ):
 
 
 
81
  try:
82
+ await interaction.response.defer() # MUST be the first await
 
 
 
 
 
 
83
 
84
+ # ... rest of your code ...
85
+ except Exception as e:
86
+ print(e)
87
+ """Generates an image based on the user's prompt and aspect ratio."""
88
+ # await interaction.response.defer() # Acknowledge within 3 seconds
 
 
 
89
 
90
+ # Call the asynchronous image generation function
91
+ image_url_or_error = await generate_image_async(
92
+ prompt, aspect_ratio.value
93
+ )
94
 
95
+ if image_url_or_error.startswith("http"):
96
+ await interaction.followup.send(
97
+ f"Here's your generated image based on the prompt '{prompt}' with aspect ratio {aspect_ratio.name}:\n{image_url_or_error}"
98
+ )
99
+ else:
100
+ await interaction.followup.send(
101
+ f"Sorry, I couldn't generate an image. {image_url_or_error}"
102
+ )
103
 
104
  @tree.command(name="hello", description="Says hello!")
105
+ async def hello_command(interaction):
106
+ await interaction.response.send_message("Hello there!")
107
+
108
+ # --- Bot Initialization and Event Loop ---
 
 
 
 
 
 
 
 
109
  async def on_ready():
110
+ await tree.sync()
 
111
  print("Bot is ready!")
 
 
 
112
 
113
+ client.event(on_ready)
114
+
115
  async def main():
116
+ print("Inside main()")
117
  async with client:
118
+ print("Starting client...")
119
  await client.start(DISCORD_BOT_TOKEN)
120
+ print("Bot has started.")
121
+
122
+ # Keep the main function alive:
123
+ while True:
124
+ await asyncio.sleep(60) # Sleep for 60 seconds
125
 
 
126
  if __name__ == "__main__":
127
+ print("Running main()")
128
+ asyncio.run(main())