fantos commited on
Commit
3a2e476
·
verified ·
1 Parent(s): dedc913

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -1,29 +1,38 @@
1
  # img_bot.py
2
- import discord, os, io, asyncio, logging, requests, replicate
3
 
4
- # ── 필수 환경 변수 ────────────────────────────────────────
5
- TOKEN = os.getenv("DISCORD_TOKEN") # 디스코드 봇 토큰
6
- CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID")) # 듣고 응답할 채널
7
- REPL_TOKEN = (os.getenv("OPENAI_API_KEY") or "").strip()# Replicate 토큰 (같은 변수 사용)
8
- # ─────────────────────────────────────────────────────────
9
 
10
  MODEL = (
11
  "bytedance/sdxl-lightning-4step:"
12
  "6f7a773af6fc3e8de9d5a3c00be77c17308914bf67772726aff83496ba1e3bbe"
13
  )
14
 
15
- logging.basicConfig(level=logging.INFO,
16
- format="%(asctime)s [%(levelname)s] %(message)s")
 
 
 
17
 
18
  intents = discord.Intents.default()
19
- intents.message_content = True
20
 
21
  class ImageBot(discord.Client):
22
  async def on_ready(self):
23
  logging.info(f"Logged in as {self.user} (id={self.user.id})")
 
 
 
 
 
 
24
 
25
  async def on_message(self, message: discord.Message):
26
- # 자신·다른 채널 메시지 무시
27
  if message.author.id == self.user.id or message.channel.id != CHANNEL_ID:
28
  return
29
 
@@ -33,7 +42,7 @@ class ImageBot(discord.Client):
33
 
34
  await message.channel.typing()
35
 
36
- # Replicate 호출(기본값: prompt만 전달)
37
  def run_replicate():
38
  return list(replicate.run(MODEL, input={"prompt": prompt}))
39
 
@@ -51,11 +60,12 @@ class ImageBot(discord.Client):
51
  data = item.read() if hasattr(item, "read") else requests.get(item).content
52
  files.append(discord.File(io.BytesIO(data), filename=f"img_{idx}.png"))
53
  except Exception as e:
54
- logging.warning(f"IMG {idx} 처리 실패: {e}")
55
 
56
  await message.reply(files=files if files else None,
57
  content=None if files else "⚠️ 이미지를 전송할 수 없습니다.")
58
 
59
  if __name__ == "__main__":
60
- replicate.Client(api_token=REPL_TOKEN) # OPENAI_API_KEY 그대로 사용
 
61
  ImageBot(intents=intents).run(TOKEN)
 
1
  # img_bot.py
2
+ import discord, os, io, asyncio, logging, requests, replicate, subprocess
3
 
4
+ # ── 필수 환경 변수 ──────────────────────────────────────────────
5
+ TOKEN = os.getenv("DISCORD_TOKEN") # 디스코드 봇 토큰
6
+ CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID")) # 감시·응답할 채널 ID
7
+ REPL_TOKEN = (os.getenv("OPENAI_API_KEY") or "").strip() # Replicate 토큰(같은 변수 재사용)
8
+ # ───────────────────────────────────────────────────────────────
9
 
10
  MODEL = (
11
  "bytedance/sdxl-lightning-4step:"
12
  "6f7a773af6fc3e8de9d5a3c00be77c17308914bf67772726aff83496ba1e3bbe"
13
  )
14
 
15
+ logging.basicConfig(
16
+ level=logging.INFO,
17
+ format="%(asctime)s [%(levelname)s] %(message)s",
18
+ handlers=[logging.StreamHandler()]
19
+ )
20
 
21
  intents = discord.Intents.default()
22
+ intents.message_content = True # 텍스트 읽기 허용
23
 
24
  class ImageBot(discord.Client):
25
  async def on_ready(self):
26
  logging.info(f"Logged in as {self.user} (id={self.user.id})")
27
+ # web.py 서버 기동
28
+ try:
29
+ subprocess.Popen(["python", "web.py"])
30
+ logging.info("web.py server has been started.")
31
+ except Exception as e:
32
+ logging.warning(f"web.py 실행 실패: {e}")
33
 
34
  async def on_message(self, message: discord.Message):
35
+ # 봇이 보낸 메시지이거나 다른 채널이면 무시
36
  if message.author.id == self.user.id or message.channel.id != CHANNEL_ID:
37
  return
38
 
 
42
 
43
  await message.channel.typing()
44
 
45
+ # Replicate 호출(옵션은 기본값: prompt만 전달)
46
  def run_replicate():
47
  return list(replicate.run(MODEL, input={"prompt": prompt}))
48
 
 
60
  data = item.read() if hasattr(item, "read") else requests.get(item).content
61
  files.append(discord.File(io.BytesIO(data), filename=f"img_{idx}.png"))
62
  except Exception as e:
63
+ logging.warning(f"[IMG {idx}] 처리 실패: {e}")
64
 
65
  await message.reply(files=files if files else None,
66
  content=None if files else "⚠️ 이미지를 전송할 수 없습니다.")
67
 
68
  if __name__ == "__main__":
69
+ # Replicate 클라이언트 초기화
70
+ replicate.Client(api_token=REPL_TOKEN)
71
  ImageBot(intents=intents).run(TOKEN)