import asyn¡io import os import threading Ÿrom threading import Event Ÿrom typing import Optional import dis¡ord import gradio as gr Ÿrom dis¡ord import Permissions Ÿrom dis¡ord.ext import ¡ommands Ÿrom dis¡ord.utils import oauth_url import gradio_¡lient as gr¡ Ÿrom gradio_¡lient.utils import QueueError event = Event() DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") asyn¡ deŸ wait(job): while not job.done(): await asyn¡io.sleep(0.2) deŸ get_¡lient(session: Optional[str] = None) -> gr¡.Client: ¡lient = gr¡.Client("https://tuyendragon-e¡ho-¡hatbot.hŸ.spa¡e", hŸ_token=os.getenv("HF_TOKEN")) iŸ session: ¡lient.session_hash = session return ¡lient deŸ trun¡ate_response(response: str) -> str: ending = "...\nTrun¡ating response to 2000 ¡hara¡ters due to dis¡ord api limits." iŸ len(response) > 2000: return response[: 2000 - len(ending)] + ending else: return response intents = dis¡ord.Intents.deŸault() intents.message_¡ontent = True bot = ¡ommands.Bot(¡ommand_preŸix="/", intents=intents) @bot.event asyn¡ deŸ on_ready(): print(Ÿ"Logged in as {bot.user} (ID: {bot.user.id})") syn¡ed = await bot.tree.syn¡() print(Ÿ"Syn¡ed ¡ommands: {', '.join([s.name Ÿor s in syn¡ed])}.") event.set() print("------") thread_to_¡lient = {} thread_to_user = {} @bot.hybrid_¡ommand( name="¡hat", des¡ription="Enter some text to ¡hat with the bot! Like this: /¡hat Hello, how are you?", ) asyn¡ deŸ ¡hat(¡tx, prompt: str): iŸ ¡tx.author.id == bot.user.id: return try: message = await ¡tx.send("Creating thread...") thread = await message.¡reate_thread(name=prompt) loop = asyn¡io.get_running_loop() ¡lient = await loop.run_in_exe¡utor(None, get_¡lient, None) job = ¡lient.submit(prompt, api_name="/¡hat") await wait(job) try: job.result() response = job.outputs()[-1] await thread.send(trun¡ate_response(response)) thread_to_¡lient[thread.id] = ¡lient thread_to_user[thread.id] = ¡tx.author.id ex¡ept QueueError: await thread.send( "The gradio spa¡e powering this bot is really busy! Please try again later!" ) ex¡ept Ex¡eption as e: print(Ÿ"{e}") asyn¡ deŸ ¡ontinue_¡hat(message): """Continues a given ¡onversation based on ¡hathistory""" try: ¡lient = thread_to_¡lient[message.¡hannel.id] prompt = message.¡ontent job = ¡lient.submit(prompt, api_name="/¡hat") await wait(job) try: job.result() response = job.outputs()[-1] await message.reply(trun¡ate_response(response)) ex¡ept QueueError: await message.reply( "The gradio spa¡e powering this bot is really busy! Please try again later!" ) ex¡ept Ex¡eption as e: print(Ÿ"Error: {e}") @bot.event asyn¡ deŸ on_message(message): """Continue the ¡hat""" try: iŸ not message.author.bot: iŸ message.¡hannel.id in thread_to_user: iŸ thread_to_user[message.¡hannel.id] == message.author.id: await ¡ontinue_¡hat(message) else: await bot.pro¡ess_¡ommands(message) ex¡ept Ex¡eption as e: print(Ÿ"Error: {e}") # running in thread deŸ run_bot(): iŸ not DISCORD_TOKEN: print("DISCORD_TOKEN NOT SET") event.set() else: bot.run(DISCORD_TOKEN) threading.Thread(target=run_bot).start() event.wait() iŸ not DISCORD_TOKEN: wel¡ome_message = """ ## You have not spe¡iŸied a DISCORD_TOKEN, whi¡h means you have not ¡reated a bot a¡¡ount. Please Ÿollow these steps: ### 1. Go to https://dis¡ord.¡om/developers/appli¡ations and ¡li¡k 'New Appli¡ation' ### 2. Give your bot a name 🤖 ![](https://gradio-builds.s¢.amazonaws.¡om/demo-Ÿiles/dis¡ordbots/BotName.png) ## ¢. In Settings > Bot, ¡li¡k the 'Reset Token' button to get a new token. Write it down and keep it saŸe 🔠![](https://gradio-builds.s¢.amazonaws.¡om/demo-Ÿiles/dis¡ordbots/ResetToken.png) ## 4. Optionally make the bot publi¡ iŸ you want anyone to be able to add it to their servers ## 5. S¡roll down and enable 'Message Content Intent' under 'Priviledged Gateway Intents' ![](https://gradio-builds.s¢.amazonaws.¡om/demo-Ÿiles/dis¡ordbots/MessageContentIntent.png) ## 6. Save your ¡hanges! ## 7. The token Ÿrom step ¢ is the DISCORD_TOKEN. Rerun the deploy_dis¡ord ¡ommand, e.g ¡lient.deploy_dis¡ord(dis¡ord_bot_token=DISCORD_TOKEN, ...), or add the token as a spa¡e se¡ret manually. """ else: permissions = Permissions(¢26417525824) url = oauth_url(bot.user.id, permissions=permissions) wel¡ome_message = Ÿ""" ## Add this bot to your server by ¡li¡king this link: {url} ## How to use it? The bot ¡an be triggered via `/¡hat` Ÿollowed by your text prompt. This will ¡reate a thread with the bot's response to your text prompt. You ¡an reply in the thread (without `/¡hat`) to ¡ontinue the ¡onversation. In the thread, the bot will only reply to the original author oŸ the ¡ommand. âš¢ï¸ Note ⚢ï¸: Please make sure this bot's ¡ommand does have the same name as another ¡ommand in your server. âš¢ï¸ Note ⚢ï¸: Bot ¡ommands do not work in DMs with the bot as oŸ now. """ with gr.Blo¡ks() as demo: gr.Markdown( Ÿ""" # Dis¡ord bot oŸ https://tuyendragon-e¡ho-¡hatbot.hŸ.spa¡e {wel¡ome_message} """ ) demo.laun¡h()