Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,99 +1,33 @@
|
|
1 |
import discord
|
2 |
from discord import app_commands
|
3 |
import os
|
4 |
-
import requests
|
5 |
import asyncio
|
6 |
-
from threading import Thread
|
7 |
|
8 |
-
# --- Environment Variables & Setup ---
|
9 |
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
|
10 |
-
GLIF_API_TOKEN = os.getenv("GLIF_API_TOKEN")
|
11 |
-
GLIF_API_URL = "https://simple-api.glif.app"
|
12 |
|
13 |
-
# Check if tokens are set
|
14 |
-
if not DISCORD_BOT_TOKEN or not GLIF_API_TOKEN:
|
15 |
-
raise ValueError(
|
16 |
-
"Both DISCORD_BOT_TOKEN and GLIF_API_TOKEN must be set as environment variables."
|
17 |
-
)
|
18 |
-
|
19 |
-
# --- Discord Bot Setup ---
|
20 |
intents = discord.Intents.default()
|
21 |
-
# intents.message_content = True # If you need message content intent in the future
|
22 |
client = discord.Client(intents=intents)
|
23 |
tree = app_commands.CommandTree(client)
|
24 |
|
25 |
-
# --- GLIF API Interaction ---
|
26 |
-
def generate_image(prompt, aspect_ratio):
|
27 |
-
payload = {
|
28 |
-
"id": "cm3ugmzv2002gnckiosrwk6xi", # Your GLIF model ID
|
29 |
-
"inputs": [prompt, aspect_ratio],
|
30 |
-
}
|
31 |
-
headers = {"Authorization": f"Bearer {GLIF_API_TOKEN}"}
|
32 |
-
|
33 |
-
response = requests.post(GLIF_API_URL, json=payload, headers=headers)
|
34 |
-
|
35 |
-
if response.status_code == 200:
|
36 |
-
response_data = response.json()
|
37 |
-
if "output" in response_data:
|
38 |
-
return response_data["output"]
|
39 |
-
elif "error" in response_data:
|
40 |
-
return f"Error: {response_data['error']}"
|
41 |
-
else:
|
42 |
-
return f"API request failed with status code: {response.status_code}"
|
43 |
-
|
44 |
-
# --- Discord Slash Commands ---
|
45 |
-
@tree.command(
|
46 |
-
name="generate", description="Generates an image based on a text prompt"
|
47 |
-
)
|
48 |
-
@app_commands.choices(
|
49 |
-
aspect_ratio=[
|
50 |
-
app_commands.Choice(name="1:1 (Square)", value="1:1"),
|
51 |
-
app_commands.Choice(name="9:16 (Vertical)", value="9:16"),
|
52 |
-
app_commands.Choice(name="16:9 (Horizontal)", value="16:9"),
|
53 |
-
app_commands.Choice(name="4:5 (Portrait)", value="4:5"),
|
54 |
-
app_commands.Choice(name="5:4 (Landscape)", value="5:4"),
|
55 |
-
app_commands.Choice(name="3:4 (Portrait)", value="3:4"),
|
56 |
-
app_commands.Choice(name="4:3 (Landscape)", value="4:3"),
|
57 |
-
]
|
58 |
-
)
|
59 |
-
async def generate_command(
|
60 |
-
interaction: discord.Interaction,
|
61 |
-
prompt: str,
|
62 |
-
aspect_ratio: app_commands.Choice[str],
|
63 |
-
):
|
64 |
-
"""Generates an image based on the user's prompt and aspect ratio."""
|
65 |
-
await interaction.response.defer()
|
66 |
-
|
67 |
-
try:
|
68 |
-
image_url = generate_image(prompt, aspect_ratio.value)
|
69 |
-
if image_url.startswith("http"):
|
70 |
-
await interaction.followup.send(
|
71 |
-
f"Here's your generated image based on the prompt '{prompt}' with aspect ratio {aspect_ratio.name}:\n{image_url}"
|
72 |
-
)
|
73 |
-
else:
|
74 |
-
await interaction.followup.send(
|
75 |
-
f"Sorry, I couldn't generate an image. {image_url}"
|
76 |
-
)
|
77 |
-
except Exception as e:
|
78 |
-
await interaction.followup.send(f"An error occurred: {e}")
|
79 |
-
|
80 |
@tree.command(name="hello", description="Says hello!")
|
81 |
async def hello_command(interaction):
|
|
|
82 |
await interaction.response.send_message("Hello there!")
|
83 |
|
84 |
-
# --- Bot Initialization and Event Loop ---
|
85 |
-
|
86 |
async def on_ready():
|
|
|
87 |
await tree.sync()
|
88 |
print("Bot is ready!")
|
89 |
|
90 |
client.event(on_ready)
|
91 |
|
92 |
async def main():
|
93 |
-
|
94 |
async with client:
|
|
|
95 |
await client.start(DISCORD_BOT_TOKEN)
|
96 |
print("Bot has stopped.")
|
97 |
|
98 |
if __name__ == "__main__":
|
|
|
99 |
asyncio.run(main())
|
|
|
1 |
import discord
|
2 |
from discord import app_commands
|
3 |
import os
|
|
|
4 |
import asyncio
|
|
|
5 |
|
|
|
6 |
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
intents = discord.Intents.default()
|
|
|
9 |
client = discord.Client(intents=intents)
|
10 |
tree = app_commands.CommandTree(client)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
@tree.command(name="hello", description="Says hello!")
|
13 |
async def hello_command(interaction):
|
14 |
+
print("Hello command triggered")
|
15 |
await interaction.response.send_message("Hello there!")
|
16 |
|
|
|
|
|
17 |
async def on_ready():
|
18 |
+
print("on_ready() called")
|
19 |
await tree.sync()
|
20 |
print("Bot is ready!")
|
21 |
|
22 |
client.event(on_ready)
|
23 |
|
24 |
async def main():
|
25 |
+
print("Inside main()")
|
26 |
async with client:
|
27 |
+
print("Starting client...")
|
28 |
await client.start(DISCORD_BOT_TOKEN)
|
29 |
print("Bot has stopped.")
|
30 |
|
31 |
if __name__ == "__main__":
|
32 |
+
print("Running main()")
|
33 |
asyncio.run(main())
|