import os import discord import requests from dotenv import load_dotenv from fastapi import FastAPI import gradio as gr import asyncio import threading import time # Load environment variables load_dotenv() # Get bot token from .env file BOT_TOKEN = os.getenv("BOT_TOKEN") # Set up FastAPI and Gradio app = FastAPI() # This will simulate the "Bot is running" message in the background @app.get("/") async def read_root(): return {"message": "Bot is running"} # Set up Discord bot client intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) # API URL for fetching responses from the external service url = "https://0zyen.vercel.app/api/chat/completions" # Function to interact with the API def get_response_from_api(user_input): payload = { "content": user_input, "model": "Evil" } response = requests.post(url, json=payload) if response.status_code == 200: api_response = response.json() return f'prompt\nmodel:evil\n{api_response.get("generated_text", "")}' return "Error communicating with API." # Async function to start the Discord bot async def run_discord_bot(): await client.start(BOT_TOKEN) # Function to keep FastAPI and Discord bot running simultaneously def start_fastapi_server(): import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860) # Event handler for when the bot is ready @client.event async def on_ready(): print(f"Bot logged in as {client.user}") # Event handler for when the bot receives a message @client.event async def on_message(message): if message.author == client.user: return # Interact with the external API response_text = get_response_from_api(message.content) # Send the response back to the Discord channel await message.channel.send(response_text) # Run both FastAPI server and Discord bot concurrently if __name__ == "__main__": # Start the FastAPI server in a separate thread threading.Thread(target=start_fastapi_server, daemon=True).start() # Run the Discord bot asyncio.run(run_discord_bot())