Spaces:
Runtime error
Runtime error
Download main.py from ferrrnannnn/SenleionBotAI: direct link, hf CLI and curl.
- Browser
- Download file 4.57 kB
-
https://huggingface.co/spaces/ferrrnannnn/SenleionBotAI/resolve/main/main.py
- Command line
-
hf download hf://spaces/ferrrnannnn/SenleionBotAI/main.py
-
curl -L -o main.py https://huggingface.co/spaces/ferrrnannnn/SenleionBotAI/resolve/main/main.py
4.57 kB
| import os | |
| import asyncio | |
| import socket | |
| import aiohttp | |
| import discord | |
| from discord.ext import commands | |
| from google import genai | |
| from google.genai import types | |
| from aiohttp import web | |
| DISCORD_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 = ( | |
| "Sos un miembro más del servidor de Discord. Hablás como un pibe de Argentina, " | |
| "usás jerga de barrio de forma natural (bld, de una, facha, qué onda, corta la bocha, mal, olvídate) " | |
| "y respondés de forma coherente, corta, divertida y sin dar cringe ni parecer un robot corporativo. " | |
| "Sos un par, un amigo más del grupo." | |
| ) | |
| config = types.GenerateContentConfig( | |
| system_instruction=PERSONALIDAD | |
| ) | |
| intents = discord.Intents.default() | |
| intents.message_content = True | |
| bot = commands.Bot( | |
| command_prefix="!", | |
| intents=intents | |
| ) | |
| async def on_ready(): | |
| print(f"¡Bot conectado y tirando facha como {bot.user}!") | |
| async def on_message(message): | |
| if message.author == bot.user: | |
| return | |
| if 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" | |
| ) | |
| return | |
| async 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) | |
| ) | |
| async def test_google_https(): | |
| print("Probando HTTPS hacia Google...") | |
| try: | |
| timeout = aiohttp.ClientTimeout(total=15) | |
| async with aiohttp.ClientSession(timeout=timeout) as session: | |
| async with session.get( | |
| "https://www.google.com" | |
| ) as response: | |
| print( | |
| "Google responde HTTP:", | |
| response.status | |
| ) | |
| except Exception as e: | |
| print( | |
| "ERROR CONECTANDO A GOOGLE:", | |
| repr(e) | |
| ) | |
| # ------------------------------ | |
| # MAIN | |
| # ------------------------------ | |
| async def main(): | |
| await init_web_server() | |
| await test_dns() | |
| await test_google_https() | |
| await test_discord_https() | |
| print("Intentando iniciar sesión en Discord...") | |
| await bot.start(DISCORD_TOKEN) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) |