import os import discord from discord.ext import commands import aiohttp import asyncio from fastapi import FastAPI import uvicorn import threading # --- CONFIGURATION --- # Pulls your token safely from the Hugging Face Settings tab DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN") HF_API_URL = "https://pugazh24-cyberwolf-sentinel-engine.hf.space/generate" # --- THE DISGUISE (Dummy Web Server) -- app = FastAPI() @app.get("/") def health_check(): return {"status": "CyberWolf Sentinel Discord Bot is Online!"} def run_dummy_server(): uvicorn.run(app, host="0.0.0.0", port=7860) # --- DISCORD BOT CODE --- intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_ready(): print("--------------------------------------------------") print(f"SUCCESS: {bot.user.name} is online in the Cloud!") print("--------------------------------------------------") def split_response(text, max_length=1900): return [text[i:i + max_length] for i in range(0, len(text), max_length)] async def query_sentinel_engine(payload: dict) -> dict: async with aiohttp.ClientSession() as session: try: async with session.post(HF_API_URL, json=payload, timeout=180) as response: if response.status == 200: return await response.json() elif response.status == 422: return {"error": "HTTP 422: Backend rejected payload schema."} else: return {"error": f"HTTP Error {response.status}"} except asyncio.TimeoutError: return {"error": "Inference timed out."} except Exception as e: return {"error": f"Network exception: {str(e)}"} @bot.command(name="ask") async def ask(ctx, *, prompt: str): async with ctx.typing(): payload = {"prompt": prompt, "max_tokens": 128, "temperature": 0.7} result = await query_sentinel_engine(payload) if isinstance(result, dict) and "error" in result: await ctx.send(f"❌ **Engine Failure:** `{result['error']}`") return if isinstance(result, dict) and "generated_text" in result: response_text = result["generated_text"].strip() else: response_text = f"⚠️ Server returned an unparseable response: {result}" for chunk in split_response(response_text): await ctx.send(chunk) # --- MAIN EXECUTION --- if __name__ == "__main__": # 1. Start the disguise server in the background threading.Thread(target=run_dummy_server, daemon=True).start() # 2. Boot up the actual Discord Bot if not DISCORD_BOT_TOKEN: print("CRITICAL ERROR: DISCORD_BOT_TOKEN is missing from Space Secrets!") else: bot.run(DISCORD_BOT_TOKEN)