import asyncio import os import threading from threading import Event from typing import Optional import discord import gradio as gr import gradio_client as grc from discord import Permissions from discord.ext import commands from discord.utils import oauth_url from gradio_client.utils import QueueError event = Event() DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") HF_TOKEN = os.getenv("HF_TOKEN") async def wait(job): while not job.done(): await asyncio.sleep(0.2) def get_client(session: Optional[str] = None) -> grc.Client: client = grc.Client("https://wop-xxx-opengpt.hf.space/", hf_token=HF_TOKEN) if session: client.session_hash = session return client def truncate_response(response: str) -> str: ending = "...\nTruncating response to 2000 characters due to discord api limits." if len(response) > 2000: return response[: 2000 - len(ending)] + ending else: return response intents = discord.Intents.all() bot = commands.Bot(command_prefix="$", intents=intents) @bot.event async def on_ready(): print(f"Logged in as {bot.user} (ID: {bot.user.id})") event.set() print("------") @bot.event async def on_member_join(mb): channelv0 = discord.utils.get(mb.guild.channels, name = "đź‘‹wellcome-goodbye") await channelv0.send(f"Welcome to server, <@{mb.id}>! ") # running in thread def run_bot(): if not DISCORD_TOKEN: print("DISCORD_TOKEN NOT SET") event.set() else: bot.run(DISCORD_TOKEN) threading.Thread(target=run_bot).start() event.wait() with gr.Blocks() as demo: gr.Markdown( f""" # Discord bot is online! """ ) demo.launch()