Spaces:
Runtime error
Runtime error
File size: 4,568 Bytes
aa86378 42009ec 2f2343e aa86378 2a074f6 2f2343e 42009ec aa86378 2f2343e 2a074f6 aa86378 2a074f6 eeb1b57 aa86378 2f2343e aa86378 2f2343e aa86378 2f2343e aa86378 2f2343e 2a074f6 2f2343e aa86378 2f2343e aa86378 2f2343e aa86378 2f2343e 7b0b8b3 2f2343e 7b0b8b3 2f2343e 7b0b8b3 2f2343e 7b0b8b3 2f2343e 0f0ccfa 2f2343e 7b0b8b3 2f2343e 7b0b8b3 2f2343e 0f0ccfa 2f2343e 7b0b8b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 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
)
@bot.event
async def on_ready():
print(f"¡Bot conectado y tirando facha como {bot.user}!")
@bot.event
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()) |