max1949 commited on
Commit
92e22fd
·
verified ·
1 Parent(s): da7caff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -14
app.py CHANGED
@@ -555,32 +555,62 @@ async def auto_backup():
555
 
556
  @bot.event
557
  async def on_ready():
558
- if not auto_backup.is_running(): auto_backup.start()
559
- await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="HuggingFace Node"))
 
 
 
 
 
 
560
  logger.info(f"✅ [Online] BK-GTA V62.9-Titan Live. Session: {SESSION_ID}")
561
 
 
 
 
 
 
 
 
 
 
562
  def run_bot():
563
- if not DISCORD_TOKEN:
 
 
 
 
 
564
  logger.critical("❌ [FATAL] DISCORD_TOKEN Missing.")
565
  return
566
 
567
- # 自动重连容器
568
- while True:
569
- try:
570
- bot.run(DISCORD_TOKEN)
571
- except Exception as e:
572
- logger.error(f"⚠️ [Socket Error] {e}. Restarting in 10s...")
573
- time.sleep(10)
 
 
 
 
 
 
 
574
 
575
  if __name__ == "__main__":
576
  # 1. 启动 Discord 后台线程
577
  threading.Thread(target=run_bot, daemon=True).start()
578
 
579
- # 2. 启动 Gradio 前台哨兵 (强制监听 7860 端口)
 
 
 
580
  iface = gr.Interface(
581
- fn=get_status,
582
- inputs=None,
583
- outputs="json",
584
  title="BK-GTA Sentinel AI",
585
  description="Physical architecture monitor. Logic Integrity: Secure."
586
  )
 
555
 
556
  @bot.event
557
  async def on_ready():
558
+ if not auto_backup.is_running():
559
+ auto_backup.start()
560
+ await bot.change_presence(
561
+ activity=discord.Activity(
562
+ type=discord.ActivityType.watching,
563
+ name="HuggingFace Node"
564
+ )
565
+ )
566
  logger.info(f"✅ [Online] BK-GTA V62.9-Titan Live. Session: {SESSION_ID}")
567
 
568
+ @bot.event
569
+ async def on_disconnect():
570
+ logger.warning("⚠️ [Gateway] Disconnected. discord.py will auto-reconnect...")
571
+
572
+ @bot.event
573
+ async def on_resumed():
574
+ logger.info("✅ [Gateway] Session resumed successfully.")
575
+
576
+
577
  def run_bot():
578
+ """
579
+ [修复] 不再使用 while True 循环。
580
+ discord.py 内置自动重连机制 (reconnect=True 是默认值),
581
+ 无需手动重启。bot.run() 只能调用一次。
582
+ """
583
+ if not DISCORD_TOKEN:
584
  logger.critical("❌ [FATAL] DISCORD_TOKEN Missing.")
585
  return
586
 
587
+ try:
588
+ bot.run(DISCORD_TOKEN, reconnect=True)
589
+ except discord.LoginFailure:
590
+ logger.critical("❌ [FATAL] Invalid DISCORD_TOKEN! Check your token.")
591
+ except discord.PrivilegedIntentsRequired:
592
+ logger.critical(
593
+ "❌ [FATAL] Privileged Intents not enabled!\n"
594
+ "Go to https://discord.com/developers/applications → Your Bot → Bot tab\n"
595
+ "→ Enable 'SERVER MEMBERS INTENT' and 'MESSAGE CONTENT INTENT'"
596
+ )
597
+ except Exception as e:
598
+ logger.error(f"⚠️ [Fatal Error] Bot terminated: {e}")
599
+ traceback.print_exc()
600
+
601
 
602
  if __name__ == "__main__":
603
  # 1. 启动 Discord 后台线程
604
  threading.Thread(target=run_bot, daemon=True).start()
605
 
606
+ # 2. 给 bot 一点启动时间,避免 get_status 立即报错
607
+ time.sleep(3)
608
+
609
+ # 3. 启动 Gradio 前台哨兵
610
  iface = gr.Interface(
611
+ fn=get_status,
612
+ inputs=None,
613
+ outputs="json",
614
  title="BK-GTA Sentinel AI",
615
  description="Physical architecture monitor. Logic Integrity: Secure."
616
  )