Spaces:
Runtime error
Runtime error
Update main.py
Browse files12345678910111213149 unmodified lines242526272829305 unmodified lines36373839404142434445464748495051525354555657585960616263646566676869707172737475767778import osimport asyncioimport discordfrom discord.ext import commandsfrom google import genaifrom google.genai import typesfrom aiohttp import web # Importamos la librería para el servidor fantasmaDISCORD_TOKEN = os.getenv("DISCORD_TOKEN")GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")client = genai.Client(api_key=GEMINI_API_KEY)PERSONALIDAD = (9 unmodified linesintents = discord.Intents.default()intents.message_content = Truebot = commands.Bot(command_prefix="!", intents=intents)@bot.eventasync def on_ready():5 unmodified linesreturnif bot.user in message.mentions:texto_usuario = message.content.replace(f'<@!{bot.user.id}>', '').replace(f'<@{bot.user.id}>', '').strip()if not texto_usuario:await message.reply("¿Qué onda, bld? ¿Para qué me nombrás si no me vas a decir nada? Jajaja")returnasync with message.channel.typing():try:response = client.models.generate_content(model="gemini-1.5-flash",contents=texto_usuario,config=config)await message.reply(response.text)except Exception as e:print(f"Error real en la API: {e}")await message.reply("Uhh bld, se me cruzaron los cables en serio, aguantame un toque.")await bot.process_commands(message)# --- SERVIDOR WEB FANTASMA PARA ENGAÑAR A HUGGING FACE ---async def handle_web(request):return web.Response(text="Bot de Discord funcionando piola.")async def init_web_server():app = web.Application()app.router.add_get('/', handle_web)runner = web.AppRunner(app)await runner.setup()# Le decimos a HF que estamos activos en su puerto sagrado 7860site = web.TCPSite(runner, '0.0.0.0', 7860)await site.start()print("Servidor fantasma levantado en el puerto 7860")async def main():# Arrancamos la web fantasma y el bot al mismo tiempoawait init_web_server()await bot.start(DISCORD_TOKEN)if __name__ == "__main__":123456789101112131415161718199 unmodified lines29303132333435363738395 unmodified lines45464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159import osimport asyncioimport socketimport aiohttpimport discordfrom discord.ext import commandsfrom google import genaifrom google.genai import typesfrom aiohttp import webDISCORD_TOKEN = os.getenv("DISCORD_TOKEN")GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")print("DISCORD_TOKEN cargado:", DISCORD_TOKEN is not None)print("GEMINI_API_KEY cargada:", GEMINI_API_KEY is not None)client = genai.Client(api_key=GEMINI_API_KEY)PERSONALIDAD = (9 unmodified linesintents = discord.Intents.default()intents.message_content = Truebot = commands.Bot(command_prefix="!",intents=intents)@bot.eventasync def on_ready():5 unmodified linesreturnif bot.user in message.mentions:texto_usuario = (message.content.replace(f"<@!{bot.user.id}>", "").replace(f"<@{bot.user.id}>", "").strip())if not texto_usuario:await message.reply("¿Qué onda, bld? ¿Para qué me nombrás si no me vas a decir nada? Jajaja")returnasync with message.channel.typing():try:print("Mandando consulta a Gemini...")response = client.models.generate_content(model="gemini-1.5-flash",contents=texto_usuario,config=config)print("Gemini respondió correctamente")await message.reply(response.text)except Exception as e:print("ERROR GEMINI:", repr(e))await message.reply("Uhh bld, se me cruzaron los cables en serio, aguantame un toque.")await bot.process_commands(message)# ------------------------------# SERVIDOR WEB PARA HUGGING FACE# ------------------------------async def handle_web(request):return web.Response(text="Bot funcionando.")async def init_web_server():app = web.Application()app.router.add_get("/", handle_web)runner = web.AppRunner(app)await runner.setup()site = web.TCPSite(runner,"0.0.0.0",7860)await site.start()print("Servidor web levantado en puerto 7860")# ------------------------------# DIAGNOSTICO DE RED# ------------------------------async def test_dns():print("Probando DNS...")try:ip = socket.gethostbyname("discord.com")print("discord.com resuelve a:", ip)except Exception as e:print("ERROR DNS:", repr(e))async def test_discord_https():print("Probando HTTPS hacia Discord...")try:timeout = aiohttp.ClientTimeout(total=15)async with aiohttp.ClientSession(timeout=timeout) as session:async with session.get("https://discord.com/api/v10") as response:print("Discord responde HTTP:",response.status)except Exception as e:print("ERROR CONECTANDO A DISCORD:",repr(e))# ------------------------------# MAIN# ------------------------------async def main():await init_web_server()await test_dns()await test_discord_https()print("Intentando iniciar sesión en Discord...")await bot.start(DISCORD_TOKEN)if __name__ == "__main__":
main.py+95-14