seawolf2357 commited on
Commit
e552e5d
ยท
verified ยท
1 Parent(s): 3d78f38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -22
app.py CHANGED
@@ -2,30 +2,36 @@ import discord
2
  from discord.ext import commands
3
  from gradio_client import Client
4
  import asyncio
5
- import multiprocessing
 
6
  import subprocess
7
  import sys
8
 
9
- # Set up Discord bot
 
 
 
10
  intents = discord.Intents.default()
11
  intents.message_content = True
 
 
12
  bot = commands.Bot(command_prefix='!', intents=intents)
13
 
14
- # Gradio client setup
15
  gradio_client = Client("http://211.233.58.202:7960/")
16
 
17
- # Discord channel ID to monitor
18
  CHANNEL_ID = 1269529561914413106
19
 
20
  @bot.event
21
  async def on_ready():
22
- print(f'{bot.user} has connected to Discord!')
23
  try:
24
- # Simple API call to test connection
25
  gradio_client.predict("test", api_name="/infer_t2i")
26
- print("Gradio API connection successful")
27
  except Exception as e:
28
- print(f"Gradio API connection failed: {str(e)}")
29
 
30
  @bot.event
31
  async def on_message(message):
@@ -51,40 +57,44 @@ async def on_message(message):
51
  timeout=60 # 60 seconds timeout
52
  )
53
 
54
- print(f"Predict result: {result}")
55
 
56
- # ํŒŒ์ผ ๊ฒฝ๋กœ๋งŒ ์ถ”์ถœํ•˜์—ฌ ์‚ฌ์šฉ
57
- file_path = result[0] # ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ํŒŒ์ผ ๊ฒฝ๋กœ
58
  await message.channel.send(file=discord.File(file_path))
59
  except asyncio.TimeoutError:
60
  await message.channel.send("Image generation timed out. Please try again.")
61
  except Exception as e:
62
  error_message = f"An error occurred: {str(e)}"
63
  await message.channel.send(error_message)
64
- print(f"Error in on_message: {error_message}")
65
-
66
 
67
  @bot.event
68
  async def on_disconnect():
69
- print("Bot disconnected. Attempting to reconnect...")
70
 
71
  @bot.event
72
  async def on_resume():
73
- print("Bot connection resumed.")
74
 
75
  def run_web():
76
  subprocess.run([sys.executable, "web.py"])
77
 
78
  def run_discord_bot():
79
- bot.run('MTIyODQyNTQxNDk0MzQ0MTEwNw.Gfd_ri.rrG_6-Sfp0FYvSIbv-zZ98dpHI-G_Fh9MFCzco')
 
 
 
 
 
80
 
81
  if __name__ == "__main__":
82
- # Start web.py in a separate process
83
- web_process = multiprocessing.Process(target=run_web)
84
- web_process.start()
85
 
86
- # Run the Discord bot
87
  run_discord_bot()
88
 
89
- # Wait for the web process to finish (which it likely won't)
90
- web_process.join()
 
2
  from discord.ext import commands
3
  from gradio_client import Client
4
  import asyncio
5
+ import os
6
+ import logging
7
  import subprocess
8
  import sys
9
 
10
+ # ๋กœ๊น… ์„ค์ •
11
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
12
+
13
+ # ์ธํ…ํŠธ ์„ค์ •
14
  intents = discord.Intents.default()
15
  intents.message_content = True
16
+
17
+ # Discord Bot ์„ค์ •
18
  bot = commands.Bot(command_prefix='!', intents=intents)
19
 
20
+ # Gradio API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
21
  gradio_client = Client("http://211.233.58.202:7960/")
22
 
23
+ # Discord ์ฑ„๋„ ID
24
  CHANNEL_ID = 1269529561914413106
25
 
26
  @bot.event
27
  async def on_ready():
28
+ logging.info(f'{bot.user.name} has connected to Discord!')
29
  try:
30
+ # Gradio ์—ฐ๊ฒฐ ํ…Œ์ŠคํŠธ
31
  gradio_client.predict("test", api_name="/infer_t2i")
32
+ logging.info("Gradio API connection successful")
33
  except Exception as e:
34
+ logging.error(f"Gradio API connection failed: {str(e)}")
35
 
36
  @bot.event
37
  async def on_message(message):
 
57
  timeout=60 # 60 seconds timeout
58
  )
59
 
60
+ logging.debug(f"Predict result: {result}")
61
 
62
+ if isinstance(result, tuple):
63
+ file_path = result[0]
64
  await message.channel.send(file=discord.File(file_path))
65
  except asyncio.TimeoutError:
66
  await message.channel.send("Image generation timed out. Please try again.")
67
  except Exception as e:
68
  error_message = f"An error occurred: {str(e)}"
69
  await message.channel.send(error_message)
70
+ logging.error(f"Error in on_message: {error_message}")
 
71
 
72
  @bot.event
73
  async def on_disconnect():
74
+ logging.info("Bot disconnected. Attempting to reconnect...")
75
 
76
  @bot.event
77
  async def on_resume():
78
+ logging.info("Bot connection resumed.")
79
 
80
  def run_web():
81
  subprocess.run([sys.executable, "web.py"])
82
 
83
  def run_discord_bot():
84
+ # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ๋””์Šค์ฝ”๋“œ ํ† ํฐ ์ฝ๊ธฐ
85
+ discord_token = os.getenv('DISCORD_TOKEN')
86
+ if discord_token:
87
+ bot.run(discord_token)
88
+ else:
89
+ logging.error("Discord token is not set. Please check your environment variables.")
90
 
91
  if __name__ == "__main__":
92
+ # ์›น ์„œ๋ฒ„๋ฅผ ๋ณ„๋„์˜ ํ”„๋กœ์„ธ์Šค๋กœ ์‹œ์ž‘
93
+ web_process = subprocess.Popen([sys.executable, "web.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
94
+ logging.info("Web server process started.")
95
 
96
+ # Discord ๋ด‡ ์‹คํ–‰
97
  run_discord_bot()
98
 
99
+ # ์›น ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ๋Œ€๊ธฐ
100
+ web_process.wait()