File size: 2,122 Bytes
aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b aac6f11 1bc6d0b |
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 |
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()) |