Skip to content

yuyo.asgi#

ASGI/3 adapter for Hikari's interaction server.

AsgiAdapter #

Asgi/3 adapter for Hikari's interaction server interface.

For this to work, hikari has to be installed with the optional "server" feature (e.g python -m pip install hikari[server]).

server property #

server

The interaction server this adapter is bound to.

__call__ async #

__call__(scope, receive, send)

Call the adapter.

Note

This method is called by the ASGI server.

Parameters:

  • scope (Scope) –

    The scope of the request.

  • receive (ASGIReceiveCallable) –

    The receive function to use.

  • send (ASGISendCallable) –

    The send function to use.

Raises:

__init__ #

__init__(server, /, *, executor=None, max_body_size=1024 ** 2)

Initialise the adapter.

Parameters:

  • server (InteractionServer) –

    The interaction server to use.

  • executor (Optional[Executor], default: None ) –

    If non-None, then this executor is used instead of the concurrent.futures.ThreadPoolExecutor attached to the asyncio.AbstractEventLoop that the bot will run on. This executor is used primarily for file-IO.

    While mainly supporting the concurrent.futures.ThreadPoolExecutor implementation in the standard lib, Hikari's file handling systems should also work with concurrent.futures.ProcessPoolExecutor, which relies on all objects used in IPC to be pickleable. Many third-party libraries will not support this fully though, so your mileage may vary on using ProcessPoolExecutor implementations with this parameter.

  • max_body_size (int, default: 1024 ** 2 ) –

    The maximum body size this should allow received request bodies to be in bytes before failing the request with a 413 - Content Too Large.

add_shutdown_callback #

add_shutdown_callback(callback)

Add a callback to be called when the ASGI server shuts down.

Warning

These callbacks will block the ASGI server from shutting down until they complete and any raised errors will lead to a failed shutdown.

Parameters:

Returns:

  • Self

    The adapter to enable chained calls.

add_startup_callback #

add_startup_callback(callback)

Add a callback to be called when the ASGI server starts up.

Warning

These callbacks will block the ASGI server from starting until they complete and any raised errors will lead to a failed startup.

Parameters:

Returns:

  • Self

    The adapter to enable chained calls.

remove_shutdown_callback #

remove_shutdown_callback(callback)

Remove a shutdown callback.

Parameters:

Returns:

  • Self

    The adapter to enable chained calls.

Raises:

  • ValueError

    If the callback was not registered.

remove_startup_callback #

remove_startup_callback(callback)

Remove a startup callback.

Parameters:

Returns:

  • Self

    The adapter to enable chained calls.

Raises:

  • ValueError

    If the callback was not registered.

AsgiBot #

Bases: RESTBotAware

Bot implementation which acts as an ASGI adapter.

This bot doesn't initiate a server internally but instead relies on being called as an ASGI app.

For this to work, hikari has to be installed with the optional "server" feature (e.g python -m pip install hikari[server]).

__call__ async #

__call__(scope, receive, send)

Call the bot with an ASGI event.

Note

This method is called by the ASGI server and allows the bot to function like AsgiAdapter.

Parameters:

  • scope (Scope) –

    The scope of the request.

  • receive (ASGIReceiveCallable) –

    The receive function to use.

  • send (ASGISendCallable) –

    The send function to use.

Raises:

__init__ #

__init__(token, token_type=None, public_key=None, *, asgi_managed=True, executor=None, http_settings=None, max_body_size=1024 ** 2, max_rate_limit=300.0, max_retries=3, proxy_settings=None, rest_url=None)

Initialise a new ASGI bot.

Parameters:

  • token (Union[str, TokenStrategy]) –

    The bot or bearer token. If no token is to be used, this can be undefined.

  • token_type (Union[TokenType, str, None], default: None ) –

    The type of token in use. This should only be passed when str is passed for token, can be "Bot" or "Bearer" and will be defaulted to "Bot" in this situation.

    This should be left as None when either hikari.api.TokenStrategy or None is passed for token.

  • asgi_managed (bool, default: True ) –

    Whether this bot's internal components should be automatically started and stopped based on the Asgi lifespan events.

  • executor (Optional[Executor], default: None ) –

    If non-None, then this executor is used instead of the concurrent.futures.ThreadPoolExecutor attached to the asyncio.AbstractEventLoop that the bot will run on. This executor is used primarily for file-IO.

    While mainly supporting the concurrent.futures.ThreadPoolExecutor implementation in the standard lib, Hikari's file handling systems should also work with concurrent.futures.ProcessPoolExecutor, which relies on all objects used in IPC to be pickleable. Many third-party libraries will not support this fully though, so your mileage may vary on using ProcessPoolExecutor implementations with this parameter.

  • http_settings (Optional[HTTPSettings], default: None ) –

    Optional custom HTTP configuration settings to use. Allows you to customise functionality such as whether SSL-verification is enabled, what timeouts aiohttp should expect to use for requests, and behavior regarding HTTP-redirects.

  • max_body_size (int, default: 1024 ** 2 ) –

    The maximum body size this should allow received request bodies to be in bytes before failing the request with a 413 - Content Too Large.

  • max_rate_limit (float, default: 300.0 ) –

    The max number of seconds to backoff for when rate limited. Anything greater than this will instead raise an error.

    This defaults to five minutes to stop potentially indefinitely waiting on an endpoint, which is almost never what you want to do if giving a response to a user.

    You can set this to float("inf") to disable this check entirely. Note that this only applies to the REST API component that communicates with Discord, and will not affect sharding or third party HTTP endpoints that may be in use.

  • max_retries (int, default: 3 ) –

    Maximum number of times a request will be retried if

    it fails with a 5xx status. Defaults to 3 if set to None.

  • proxy_settings (Optional[ProxySettings], default: None ) –

    Custom proxy settings to use with network-layer logic in your application to get through an HTTP-proxy.

  • public_key (Union[bytes, str, None], default: None ) –

    The public key to use to verify received interaction requests.

    This may be a hex encoded str or the raw bytes. If left as None then the client will try to work this value out based on token.

  • rest_url (Optional[str], default: None ) –

    Defaults to the Discord REST API URL if None. Can be overridden if you are attempting to point to an unofficial endpoint, or if you are attempting to mock/stub the Discord API for any reason.

    Generally you do not want to change this.

Raises:

  • ValueError
    • If token_type is provided when a token strategy is passed for token.
    • if token_type is left as None when a string is passed for token.

add_shutdown_callback #

add_shutdown_callback(callback)

Add a callback to be called when the bot shuts down.

Warning

These callbacks will block the bot from shutting down until they complete and any raised errors will lead to a failed shutdown.

Parameters:

add_startup_callback #

add_startup_callback(callback)

Add a callback to be called when the bot starts up.

Warning

These callbacks will block the bot from starting until they complete and any raised errors will lead to a failed startup.

Parameters:

close async #

close()

Close the bot's REST client.

Warning

Unless asgi_managed=False is passed to AsgiBot.__init__, the bot will be automatically closed based on the ASGI lifespan events and any other calls to this function will raise a RuntimeError.

Raises:

  • RuntimeError

    If the client isn't alive. If the client is ASGI managed.

remove_shutdown_callback #

remove_shutdown_callback(callback)

Remove a shutdown callback.

Parameters:

Raises:

  • ValueError

    If the callback was not registered.

remove_startup_callback #

remove_startup_callback(callback)

Remove a startup callback.

Parameters:

Raises:

  • ValueError

    If the callback was not registered.

run #

run()

Start the bot's REST client and wait until the bot's closed.

Warning

Unless asgi_managed=False is passed to AsgiBot.__init__, the bot will be automatically started and closed based on the ASGI lifespan events and any other calls to this function will raise a RuntimeError.

Raises:

  • RuntimeError

    If the client is already alive. If the client is ASGI managed.

start async #

start()

Start the bot's REST client.

Warning

Unless asgi_managed=False is passed to AsgiBot.__init__, the bot will be automatically started based on the ASGI lifespan events and any other calls to this function will raise a RuntimeError.

Raises:

  • RuntimeError

    If the client is already alive. If the client is ASGI managed.