Skip to content

hikari.api.interaction_server#

Provides an interface for Interaction REST server API implementations to follow.

ListenerT module-attribute #

ListenerT = Union[
    Callable[["_InteractionT_co"], Awaitable["_ResponseT_co"]],
    Callable[["_InteractionT_co"], AsyncGenerator["_ResponseT_co", None]],
]

Type hint of a Interaction server's listener callback.

This should be an async callback which takes in one positional argument which subclasses hikari.interactions.base_interactions.PartialInteraction and may return an instance of the relevant hikari.api.special_endpoints.InteractionResponseBuilder subclass for the provided interaction type which will instruct the server on how to respond.

InteractionServer #

Bases: ABC

Interface for an implementation of an interactions compatible REST server.

get_listener abstractmethod #

get_listener(
    interaction_type: Type[_InteractionT_co],
) -> Optional[ListenerT[_InteractionT_co, InteractionResponseBuilder]]

Get the listener registered for an interaction.

PARAMETER DESCRIPTION
interaction_type

Type of the interaction to get the registered listener for.

TYPE: Type[PartialInteraction]

RETURNS DESCRIPTION
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.

on_interaction abstractmethod async #

on_interaction(body: bytes, signature: bytes, timestamp: bytes) -> Response

Handle an interaction received from Discord as a REST server.

PARAMETER DESCRIPTION
body

The interaction payload.

TYPE: bytes

signature

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

TYPE: bytes

timestamp

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

TYPE: bytes

RETURNS DESCRIPTION
Response

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

set_listener abstractmethod #

set_listener(
    interaction_type: Type[_InteractionT_co],
    listener: Optional[ListenerT[_InteractionT_co, InteractionResponseBuilder]],
    /,
    *,
    replace: bool = False,
) -> None

Set the listener callback for this interaction server.

PARAMETER DESCRIPTION
interaction_type

The type of interaction this listener should be registered for.

TYPE: Type[PartialInteraction]

listener

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.

TYPE: Optional[ListenerT[PartialInteraction, InteractionResponseBuilder]]

PARAMETER DESCRIPTION
replace

Whether this call should replace the previously set listener or not.

TYPE: bool

RAISES DESCRIPTION
TypeError

If replace is False when a listener is already set.

Response #

Bases: Protocol

Protocol of the data returned by hikari.api.interaction_server.InteractionServer.on_interaction.

This is used to instruct lower-level REST server logic on how it should respond.

charset property #

charset: Optional[str]

Charset of the response's payload, if applicable.

content_type property #

content_type: Optional[str]

Content type of the response's payload, if applicable.

files property #

Up to 10 files that should be included alongside a JSON response.

headers property #

Headers that should be added to the response if applicable.

payload property #

payload: Optional[bytes]

Payload to provide in the response.

status_code property #

status_code: int

Status code that should be used to respond.

For more information see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status.