Skip to content

Asgi#

The AsgiBot is a RESTBot implementation which acts as an ASGI application to handle the interaction requests received from Discord for slash commands, context menus, message components and modals as part of an ASGI server.

rest_bot = yuyo.asgi.AsgiBot(os.environ["TOKEN"])
tanjun.Client.from_rest_bot(rest_bot)

# ... more setup

If the above example was saved in the file "bot.py" (within the current working directory) then the following may be run in the command line to run this bot using Uvicorn:

uvicorn bot:rest_bot

But since ASGI is a generic standard, you can also run this bot in other tooling such as Hypercorn, Daphne, or using FastAPI's sub-applications to mount this bot on a specific route within an existing FastAPI server:

bot = yuyo.asgi.AsgiBot(os.environ["TOKEN"], asgi_managed=False)

app = fastapi.FastAPI(on_startup=[bot.start], on_shutdown=[bot.close])

app.mount("/bot", bot)  # type: ignore

The extra steps this example goes through to let FastAPI startup and shutdown the AsgiBot are only necessary when mounting as a FastAPI sub-application.

Serverless#

bot = yuyo.AsgiBot(os.environ["TOKEN"].strip(), "Bot")

# ... Setup bot

entry_point = agraffe.Agraffe.entry_point(bot)  # type: ignore

Thanks to the flexibility of the ASGI standard, this can also be used with serverless frameworks people have implemented ASGI adapters for. The above example uses agraffe to create an entry point for AWS Lambdas, Azure Functions, or Google Cloud Functions.