Nevaehni commited on
Commit
93a9fe8
Β·
1 Parent(s): 7cda025
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -52,12 +52,14 @@ GRADIO_CLIENT = Client("Nevaehni/FLUX.1-schnell", hf_token=HF_TOKEN)
52
  # Initialize Hugging Face API client
53
  hf_api = HfApi()
54
 
 
55
  @bot.event
56
  async def on_ready():
57
  """Event handler triggered when the bot is ready."""
58
  logger.info(f'Logged in as {bot.user} (ID: {bot.user.id})')
59
  logger.info('------')
60
 
 
61
  def parse_prompt(command: str) -> str:
62
  """
63
  Parse the prompt from the command string.
@@ -73,6 +75,7 @@ def parse_prompt(command: str) -> str:
73
  return match.group(1).strip()
74
  return ''
75
 
 
76
  def create_example_embed() -> Embed:
77
  """
78
  Create an embed message with an example !generate command.
@@ -88,6 +91,7 @@ def create_example_embed() -> Embed:
88
  )
89
  return embed
90
 
 
91
  @bot.command(name='generate')
92
  async def generate(ctx: commands.Context, *, args: str = None):
93
  """
@@ -205,6 +209,19 @@ async def generate(ctx: commands.Context, *, args: str = None):
205
  else:
206
  # Response is neither dict, str, nor list
207
  await ctx.send("❌ **Error:** Unexpected response format from the API.")
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  except asyncio.TimeoutError:
209
  logger.error("API request timed out.")
210
  await ctx.send("⏰ **Error:** The request to the API timed out. Please try again later.")
@@ -219,7 +236,16 @@ async def generate(ctx: commands.Context, *, args: str = None):
219
  await ctx.send(f"❌ **Error:** An unexpected error occurred: {str(e)}")
220
  finally:
221
  # Delete the processing message
222
- await processing_message.delete()
 
 
 
 
 
 
 
 
 
223
 
224
  @bot.event
225
  async def on_command_error(ctx: commands.Context, error):
@@ -238,9 +264,11 @@ async def on_command_error(ctx: commands.Context, error):
238
  await ctx.send(f"❌ **Error:** {str(error)}")
239
  logger.error(f"Unhandled command error: {str(error)}")
240
 
 
241
  async def handle_root(request):
242
  return web.Response(text="DarkMuse GOES VROOOOM", status=200)
243
 
 
244
  async def start_web_server():
245
  app = web.Application()
246
  app.router.add_get('/', handle_root)
@@ -250,9 +278,11 @@ async def start_web_server():
250
  await site.start()
251
  logger.info("Web server started on port 7860")
252
 
 
253
  async def start_bot():
254
  await bot.start(DISCORD_BOT_TOKEN)
255
 
 
256
  async def schedule_restart():
257
  """
258
  Schedule the bot to restart the Hugging Face Space at a specified interval.
@@ -273,6 +303,7 @@ async def schedule_restart():
273
  # Optional: Add a short delay to prevent rapid retries in case of failure
274
  await asyncio.sleep(5) # Wait 5 seconds before the next restart cycle
275
 
 
276
  async def main():
277
  # Start the scheduler as a background task
278
  asyncio.create_task(schedule_restart())
@@ -282,6 +313,7 @@ async def main():
282
  start_web_server()
283
  )
284
 
 
285
  # Run the bot and web server concurrently
286
  if __name__ == '__main__':
287
  try:
 
52
  # Initialize Hugging Face API client
53
  hf_api = HfApi()
54
 
55
+
56
  @bot.event
57
  async def on_ready():
58
  """Event handler triggered when the bot is ready."""
59
  logger.info(f'Logged in as {bot.user} (ID: {bot.user.id})')
60
  logger.info('------')
61
 
62
+
63
  def parse_prompt(command: str) -> str:
64
  """
65
  Parse the prompt from the command string.
 
75
  return match.group(1).strip()
76
  return ''
77
 
78
+
79
  def create_example_embed() -> Embed:
80
  """
81
  Create an embed message with an example !generate command.
 
91
  )
92
  return embed
93
 
94
+
95
  @bot.command(name='generate')
96
  async def generate(ctx: commands.Context, *, args: str = None):
97
  """
 
209
  else:
210
  # Response is neither dict, str, nor list
211
  await ctx.send("❌ **Error:** Unexpected response format from the API.")
212
+
213
+ # **Delete the user's command message after successfully processing**
214
+ try:
215
+ await ctx.message.delete()
216
+ logger.info(f"Deleted command message from user {ctx.author} (ID: {ctx.author.id}).")
217
+ except discord.Forbidden:
218
+ logger.warning("Insufficient permissions to delete the user's message.")
219
+ await ctx.send("⚠️ **Warning:** I don't have permission to delete your command message.")
220
+ except discord.NotFound:
221
+ logger.warning("The message to delete was not found.")
222
+ except Exception as e:
223
+ logger.error(f"Unexpected error when trying to delete the message: {e}")
224
+
225
  except asyncio.TimeoutError:
226
  logger.error("API request timed out.")
227
  await ctx.send("⏰ **Error:** The request to the API timed out. Please try again later.")
 
236
  await ctx.send(f"❌ **Error:** An unexpected error occurred: {str(e)}")
237
  finally:
238
  # Delete the processing message
239
+ try:
240
+ await processing_message.delete()
241
+ logger.info("Deleted the processing message.")
242
+ except discord.Forbidden:
243
+ logger.warning("Insufficient permissions to delete the processing message.")
244
+ except discord.NotFound:
245
+ logger.warning("The processing message to delete was not found.")
246
+ except Exception as e:
247
+ logger.error(f"Unexpected error when trying to delete the processing message: {e}")
248
+
249
 
250
  @bot.event
251
  async def on_command_error(ctx: commands.Context, error):
 
264
  await ctx.send(f"❌ **Error:** {str(error)}")
265
  logger.error(f"Unhandled command error: {str(error)}")
266
 
267
+
268
  async def handle_root(request):
269
  return web.Response(text="DarkMuse GOES VROOOOM", status=200)
270
 
271
+
272
  async def start_web_server():
273
  app = web.Application()
274
  app.router.add_get('/', handle_root)
 
278
  await site.start()
279
  logger.info("Web server started on port 7860")
280
 
281
+
282
  async def start_bot():
283
  await bot.start(DISCORD_BOT_TOKEN)
284
 
285
+
286
  async def schedule_restart():
287
  """
288
  Schedule the bot to restart the Hugging Face Space at a specified interval.
 
303
  # Optional: Add a short delay to prevent rapid retries in case of failure
304
  await asyncio.sleep(5) # Wait 5 seconds before the next restart cycle
305
 
306
+
307
  async def main():
308
  # Start the scheduler as a background task
309
  asyncio.create_task(schedule_restart())
 
313
  start_web_server()
314
  )
315
 
316
+
317
  # Run the bot and web server concurrently
318
  if __name__ == '__main__':
319
  try: