Hardik5456's picture
Create bot.py
d76e213 verified
raw
history blame
1.14 kB
import discord
import requests
import os
import asyncio
# Load Discord bot token from Hugging Face Secrets
TOKEN = os.getenv("DISCORD_BOT_TOKEN") # You will set this in Hugging Face Secrets
# Hugging Face Space API URL (DeepScaleR runs here)
HF_API_URL = "http://0.0.0.0:7860/run/predict"
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'Logged in as {client.user}')
@client.event
async def on_message(message):
if message.author == client.user:
return # Avoid replying to itself
user_input = message.content.strip()
if user_input:
try:
response = requests.post(HF_API_URL, json={"data": [user_input]})
response_json = response.json()
ai_response = response_json.get("data", ["Sorry, something went wrong."])[0]
await message.channel.send(ai_response)
except Exception as e:
await message.channel.send("Error communicating with AI.")
# Run the bot
async def start_bot():
await client.start(TOKEN)
asyncio.run(start_bot())