Spaces:
Sleeping
Sleeping
| import discord | |
| import asyncio | |
| from dotenv import load_dotenv | |
| import sys | |
| import os | |
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) | |
| from controllers.chatbot_controller import process_user_input | |
| load_dotenv() | |
| DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN") | |
| intents = discord.Intents.default() | |
| intents.message_content = True | |
| client = discord.Client(intents=intents) | |
| COMMAND_PREFIX = "!" | |
| MAX_DISCORD_LENGTH = 800 | |
| async def on_ready(): | |
| print(f'β Bot conectado como {client.user}') | |
| async def on_message(message): | |
| if message.author == client.user: | |
| return | |
| if message.content.startswith(COMMAND_PREFIX): | |
| pergunta = message.content[len(COMMAND_PREFIX):].strip() | |
| await message.channel.send("π€ A pensar...") | |
| try: | |
| resposta = await asyncio.to_thread(process_user_input, pergunta) | |
| if isinstance(resposta, dict): | |
| texto = resposta.get("resposta", "β Erro: resposta mal formatada.") | |
| else: | |
| texto = resposta | |
| if len(texto) > MAX_DISCORD_LENGTH: | |
| texto = texto[:MAX_DISCORD_LENGTH - 3] + "..." | |
| await message.channel.send(f"π Resposta (Chatbot IA):\n{texto}") | |
| except Exception as e: | |
| await message.channel.send("β Erro ao processar a pergunta.") | |
| print("Erro:", e) | |
| if __name__ == "__main__": | |
| client.run(DISCORD_BOT_TOKEN) | |