all-db / app.py
standarovskid's picture
Update app.py
7fdf557 verified
import asyncio
import uvloop
import os
import logging
import threading
from threading import Event
from typing import Optional
import discord
import gradio as gr
from discord import Permissions
import discord
from discord.ext import commands
from discord.utils import oauth_url
from gradio_client import Client
from gradio_client.utils import QueueError
import aiohttp
from aiohttp.resolver import AsyncResolver
import utils
event = Event()
logging.basicConfig(level=logging.INFO)
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
HF_TOKEN=os.getenv("HF_TOKEN")
def get_client(session: Optional[str] = None) -> Client:
client = Client("https://standarovskid-bot.hf.space/", token=HF_TOKEN)
if session:
client.session_hash = session
return client
async def main():
# Setup DNS resolver to avoid potential issues
resolver = AsyncResolver(nameservers=["1.1.1.1", "8.8.8.8"])
connector = aiohttp.TCPConnector(resolver=resolver)
# Setup Discord bot
bot = commands.Bot(intents=intents, connector=connector, command_prefix="!")
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
synced = await bot.tree.sync()
print(f"Synced commands: {', '.join([s.name for s in synced])}.")
event.set()
print("------")
@bot.command()
async def test(ctx, *, arg):
thread = await ctx.create_thread(name=prompt, auto_archive_duration=60)
loop = asyncio.get_running_loop()
client = await loop.run_in_executor(None, get_client, None)
job = client.submit(prompt, api_name="/slow_echo")
wait([job])
await ctx.send(arg)
@bot.event
async def on_message(message):
try:
if not message.author.bot:
await bot.process_commands(message)
except Exception as e:
print(f"Error: {e}")
# running in thread
def run_bot():
if not DISCORD_TOKEN:
print("DISCORD_TOKEN NOT SET")
event.set()
else:
bot.run(DISCORD_TOKEN)
utils.keepalive_run()
await bot.start(os.getenv('DISCORD_TOKEN'))
if __name__ == "__main__":
asyncio.run(main())