generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
184 lines (140 loc) · 5.64 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import datetime
import os
import sys
import time
import concurrent.futures
import discord
from dalle3 import Dalle
from discord.ext import commands
import asyncio
from dotenv import load_dotenv
import glob
import boto3
load_dotenv()
# AWS S3 Configuration (commented out in your original code)
# ... (keep this part unchanged if you decide to use it)
# Fetch keys from environment variables
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
DALLE_TOKEN = os.getenv("DALLE_TOKEN")
SAVE_DIRECTORY = "images/"
intents = discord.Intents.default()
intents.messages = True
intents.guilds = True
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
bot.launch_time = time.time()
dalle_instance = Dalle(DALLE_TOKEN)
# Initialize the ThreadPoolExecutor
executor = concurrent.futures.ThreadPoolExecutor()
@bot.event
async def on_ready():
print(f"Logged in as {bot.user.name}")
@bot.command()
async def generate(ctx, *, prompt: str):
"""Generates images based on the provided prompt"""
await ctx.send(f"Generating images for prompt: `{prompt}`...")
loop = asyncio.get_event_loop()
# Initialize a Future object for the DALLE instance
future = loop.run_in_executor(executor, dalle_instance.run, prompt)
try:
# Wait for the DALLE request to complete, with a timeout of 60 seconds
await asyncio.wait_for(future, timeout=300)
print("Done generating images!")
# List all files in the SAVE_DIRECTORY
all_files = [os.path.join(root, file) for root, _, files in os.walk(SAVE_DIRECTORY) for file in files]
# Sort files by their creation time (latest first)
sorted_files = sorted(all_files, key=os.path.getctime, reverse=True)
# Get the 4 most recent files
latest_files = sorted_files[:4]
print(f"Sending {len(latest_files)} images to Discord...")
# Send all the latest images in a single message
await ctx.send(files=[discord.File(filepath) for filepath in latest_files])
except asyncio.TimeoutError:
await ctx.send("The request took too long! It might have been censored or you're out of boosts. Please try entering the prompt again.")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
# @bot.command()
# async def generate(ctx, *, prompt: str):
# """Generates images based on the provided prompt"""
# await ctx.send(f"Generating images for prompt: `{prompt}`...")
# # Make a unique directory for this generation session using the message ID
# session_save_directory = os.path.join(SAVE_DIRECTORY, str(ctx.message.id))
# os.makedirs(session_save_directory, exist_ok=True)
# # Offload the image generation to another thread.
# loop = asyncio.get_event_loop()
# await loop.run_in_executor(executor, dalle_instance.run, prompt, session_save_directory)
# # Get all the images saved in the session's directory.
# generated_images = sorted(glob.glob(os.path.join(session_save_directory, '*')), key=os.path.getctime)
# # Upload to S3 and send the URL to the user:
# for image_path in generated_images:
# file_name = os.path.basename(image_path)
# s3.upload_file(image_path, S3_BUCKET_NAME, file_name, ExtraArgs={'ACL': 'public-read'})
# image_url = f"https://{S3_BUCKET_NAME}.s3.amazonaws.com/{file_name}"
# await ctx.send(image_url)
# # Cleanup: Optionally, delete the session's directory after sending the images.
# for image_path in generated_images:
# os.remove(image_path)
# os.rmdir(session_save_directory)
@bot.command()
async def helpme(ctx):
"""Displays help about bot usage"""
help_text = (
"Commands available:\n"
"!generate <prompt>: Generate images based on prompt\n"
"!ping: Check bot's responsiveness\n"
"!setdir <directory>: Set directory for saving images\n"
"!viewlast: View last generated image\n"
"!clear: Clear bot messages\n"
"!uptime: Get bot's uptime\n"
"!restart: Restart the bot (owner only)"
)
await ctx.send(help_text)
@bot.command()
async def ping(ctx):
"""Pings the bot"""
await ctx.send("Pong!")
@bot.command()
async def setdir(ctx, directory: str):
"""Set directory for saving images"""
global SAVE_DIRECTORY
SAVE_DIRECTORY = directory
await ctx.send(f"Directory set to: {SAVE_DIRECTORY}")
@bot.command()
async def viewlast(ctx):
"""View last generated image"""
images = sorted(
[
os.path.join(SAVE_DIRECTORY, f)
for f in os.listdir(SAVE_DIRECTORY)
if os.path.isfile(os.path.join(SAVE_DIRECTORY, f))
],
key=os.path.getctime,
)
if images:
await ctx.send(file=discord.File(images[-1]))
else:
await ctx.send("No images generated yet!")
@bot.command()
async def clear(ctx, amount: int = 10):
"""Clear bot messages for neatness"""
await ctx.channel.purge(limit=amount)
@bot.command()
async def uptime(ctx):
"""Gets the bot's uptime"""
current_time = time.time()
difference = int(round(current_time - bot.launch_time))
text = str(datetime.timedelta(seconds=difference))
await ctx.send(f"Bot has been up for: {text}")
@bot.command()
@commands.is_owner()
async def restart(ctx):
"""Restarts the bot (only accessible by the bot owner)"""
await ctx.send("Restarting...")
os.execv(sys.executable, ["python"] + sys.argv)
@generate.error
async def generate_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("You must provide a prompt for image generation!")
else:
await ctx.send(f"An error occurred: {error}")
bot.run(DISCORD_TOKEN)