Skip to content

Quart support! Use asyncio with commands

Compare
Choose a tag to compare
@breqdev breqdev released this 27 May 04:48
· 361 commits to main since this release
1c73244

This release introduces the ability to use Quart instead of Flask (#18, #23). This allows developers to write commands that use asyncio, which is a more familiar experience for longtime discord.py users.

from quart import Quart

import quart.flask_patch
from flask_discord_interactions import DiscordInteractions

app = Quart(__name__)
discord = DiscordInteractions(app)

discord.update_slash_commands()

@discord.command()
async def ping(ctx):
    "Respond with a friendly 'pong'!"
    return "Pong!"

# Non-async functions still work
@discord.command()
def pong(ctx):
    return "Ping!"

Additionally, this adds an AsyncContext object for handling followup messages:

@discord.command()
async def wait(ctx, seconds: int):
    async def do_followup():
        await asyncio.sleep(seconds)
        await ctx.edit("Done!")
        await ctx.close()

    asyncio.create_task(do_followup())
    return Response(deferred=True)

# Normal followups use the normal Context
@discord.command()
def wait_sync(ctx, seconds: int):
    def do_followup():
        time.sleep(seconds)
        ctx.edit("Done!")

    threading.Thread(target=do_followup).start()
    return Response(deferred=True)

This update also allows embeds to be included in ephemeral responses, as per #19.