hikari.impl.interaction_server#

Standard implementation of a REST based interactions server.

Module Contents#

class hikari.impl.interaction_server.InteractionServer(*, dumps=data_binding.default_json_dumps, entity_factory, executor=None, loads=data_binding.default_json_loads, rest_client, public_key=None)[source]#

Bases: hikari.api.interaction_server.InteractionServer

Standard implementation of hikari.api.interaction_server.InteractionServer.

Parameters:
entity_factoryhikari.api.entity_factory.EntityFactory

The entity factory instance this server should use.

Other Parameters:
dumpshikari.internal.data_binding.JSONEncoder

The JSON encoder this server should use. Defaults to hikari.internal.data_binding.default_json_dumps.

loadshikari.internal.data_binding.JSONDecoder

The JSON decoder this server should use. Defaults to hikari.internal.data_binding.default_json_loads.

public_keybytes

The public key this server should use for verifying request payloads from Discord. If left as None then the client will try to work this out using rest_client.

rest_clienthikari.api.rest.RESTClient

The client this should use for making REST requests.

property is_alive: bool[source]#

Whether this interaction server is active.

async aiohttp_hook(request)[source]#

Handle an AIOHTTP interaction request.

This method handles aiohttp specific detail before calling InteractionServer.on_interaction with the data extracted from the request if it can and handles building an aiohttp response.

Parameters:
requestaiohttp.web.Request

The received request.

Returns:
aiohttp.web.Response

The aiohttp response.

async close()[source]#

Gracefully close the server and any open connections.

get_listener(interaction_type, /)[source]#
get_listener(interaction_type: Type[hikari.interactions.component_interactions.ComponentInteraction], /) hikari.api.interaction_server.ListenerT[hikari.interactions.component_interactions.ComponentInteraction, _ModalOrMessageResponseBuilderT] | None
get_listener(interaction_type: Type[hikari.interactions.command_interactions.AutocompleteInteraction], /) hikari.api.interaction_server.ListenerT[hikari.interactions.command_interactions.AutocompleteInteraction, hikari.api.special_endpoints.InteractionAutocompleteBuilder] | None
get_listener(interaction_type: Type[hikari.interactions.modal_interactions.ModalInteraction], /) hikari.api.interaction_server.ListenerT[hikari.interactions.modal_interactions.ModalInteraction, _MessageResponseBuilderT] | None
get_listener(interaction_type: Type[_InteractionT_co], /) hikari.api.interaction_server.ListenerT[_InteractionT_co, hikari.api.special_endpoints.InteractionResponseBuilder] | None

Get the listener registered for an interaction.

Parameters:
interaction_typetyping.Type[hikari.interactions.base_interactions.PartialInteraction]

Type of the interaction to get the registered listener for.

Returns:
typing.Optional[ListenersT[hikari.interactions.base_interactions.PartialInteraction, hikari.api.special_endpoints.InteractionResponseBuilder]

The callback registered for the provided interaction type if found, else None.

async join()[source]#

Wait for the process to halt before continuing.

async on_interaction(body, signature, timestamp)[source]#

Handle an interaction received from Discord as a REST server.

Note

If this server instance is alive then this will be called internally by the server but if the instance isn’t alive then this may still be called externally to trigger interaction dispatch.

Parameters:
bodybytes

The interaction payload.

signaturebytes

Value of the "X-Signature-Ed25519" header used to verify the body.

timestampbytes

Value of the "X-Signature-Timestamp" header used to verify the body.

Returns:
hikari.api.interaction_server.Response

Instructions on how the REST server calling this should respond to the interaction request.

set_listener(interaction_type, listener, /, *, replace=False)[source]#
set_listener(interaction_type: Type[hikari.interactions.component_interactions.ComponentInteraction], listener: hikari.api.interaction_server.ListenerT[hikari.interactions.component_interactions.ComponentInteraction, _ModalOrMessageResponseBuilderT] | None, /, *, replace: bool = False) None
set_listener(interaction_type: Type[hikari.interactions.command_interactions.AutocompleteInteraction], listener: hikari.api.interaction_server.ListenerT[hikari.interactions.command_interactions.AutocompleteInteraction, hikari.api.special_endpoints.InteractionAutocompleteBuilder] | None, /, *, replace: bool = False) None
set_listener(interaction_type: Type[hikari.interactions.modal_interactions.ModalInteraction], listener: hikari.api.interaction_server.ListenerT[hikari.interactions.modal_interactions.ModalInteraction, _MessageResponseBuilderT] | None, /, *, replace: bool = False) None

Set the listener callback for this interaction server.

Parameters:
interaction_typetyping.Type[hikari.interactions.base_interactions.PartialInteraction]

The type of interaction this listener should be registered for.

listenertyping.Optional[ListenerT[hikari.interactions.base_interactions.PartialInteraction, hikari.api.special_endpoints.InteractionResponseBuilder]]

The asynchronous listener callback to set or None to unset the previous listener.

An asynchronous listener can be either a normal coroutine or an async generator which should yield exactly once. This allows sending an initial response to the request, while still later executing further logic.

Other Parameters:
replacebool

Whether this call should replace the previously set listener or not. This call will raise a ValueError if set to False when a listener is already set.

Raises:
TypeError

If replace is False when a listener is already set.

async start(backlog=128, host=None, port=None, path=None, reuse_address=None, reuse_port=None, socket=None, shutdown_timeout=60.0, ssl_context=None)[source]#

Start the bot and wait for the internal server to startup then return.

Note

For more information on the other parameters such as defaults see AIOHTTP’s documentation.

Other Parameters:
backlogint

The number of unaccepted connections that the system will allow before refusing new connections.

hosttyping.Optional[typing.Union[str, aiohttp.web.HostSequence]]

TCP/IP host or a sequence of hosts for the HTTP server.

porttyping.Optional[int]

TCP/IP port for the HTTP server.

pathtyping.Optional[str]

File system path for HTTP server unix domain socket.

reuse_addresstyping.Optional[bool]

Tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire.

reuse_porttyping.Optional[bool]

Tells the kernel to allow this endpoint to be bound to the same port as other existing endpoints are also bound to.

sockettyping.Optional[socket.socket]

A pre-existing socket object to accept connections on.

shutdown_timeoutfloat

A delay to wait for graceful server shutdown before forcefully disconnecting all open client sockets. This defaults to 60 seconds.

ssl_contexttyping.Optional[ssl.SSLContext]

SSL context for HTTPS servers.