Skip to content

hikari.api.interaction_server#

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

ListenerT module-attribute #

ListenerT = typing.Union[
    typing.Callable[
        ["_InteractionT_co"],
        typing.Awaitable[
            typing.Union["_ResponseT_co", None]
        ],
    ],
    typing.Callable[
        ["_InteractionT_co"],
        typing.AsyncGenerator[
            typing.Union["_ResponseT_co", None], 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.

If the callback returns None, an HTTP no-content response will be returned. In this case you should respond to the interaction using the appropriate REST method instead.

InteractionServer #

Bases: ABC

Interface for an implementation of an interactions compatible REST server.

get_listener abstractmethod #

get_listener(
    interaction_type: type[_InteractionT_co],
) -> (
    ListenerT[_InteractionT_co, InteractionResponseBuilder]
    | None
)

Get the listener registered for an interaction.

PARAMETER DESCRIPTION
interaction_type

Type of the interaction to get the registered listener for.

TYPE: type[_InteractionT_co]

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[CommandInteraction],
    listener: ListenerT[
        CommandInteraction, _ModalOrMessageResponseBuilder
    ]
    | None,
    /,
    *,
    replace: bool = False,
) -> None
set_listener(
    interaction_type: type[ComponentInteraction],
    listener: ListenerT[
        ComponentInteraction, _ModalOrMessageResponseBuilder
    ]
    | None,
    /,
    *,
    replace: bool = False,
) -> None
set_listener(
    interaction_type: type[AutocompleteInteraction],
    listener: ListenerT[
        AutocompleteInteraction,
        InteractionAutocompleteBuilder,
    ]
    | None,
    /,
    *,
    replace: bool = False,
) -> None
set_listener(
    interaction_type: type[ModalInteraction],
    listener: ListenerT[
        ModalInteraction, _MessageResponseBuilderT
    ]
    | None,
    /,
    *,
    replace: bool = False,
) -> None
set_listener(
    interaction_type: type[_InteractionT_co],
    listener: ListenerT[
        _InteractionT_co, InteractionResponseBuilder
    ]
    | None,
    /,
    *,
    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[_InteractionT_co]

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: ListenerT[_InteractionT_co, InteractionResponseBuilder] | None

replace

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

TYPE: bool DEFAULT: False

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: str | None

Charset of the response's payload, if applicable.

content_type property #

content_type: str | None

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: MutableMapping[str, str] | None

Headers that should be added to the response if applicable.

payload property #

payload: bytes | None

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.