View API Reference#

class miru.view.View(*, timeout: t.Optional[t.Union[float, int, datetime.timedelta]] = 120.0, autodefer: bool = True)[source]#

Represents a set of Discord UI components attached to a message.

Parameters:
  • timeout (Optional[Union[float, int, datetime.timedelta]], optional) – The duration after which the view times out, in seconds, by default 120.0

  • autodefer (bool, optional) – If enabled, interactions will be automatically deferred if not responded to within 2 seconds, by default True

Raises:
  • ValueError – Raised if a view has more than 25 components attached.

  • RuntimeError – Raised if miru.install() was never called before instantiation.

add_item(item: ViewItem) te.Self[source]#

Adds a new item to the view.

Parameters:

item (ViewItem) – The item to be added.

Raises:
  • ValueError – A view already has 25 components attached.

  • TypeError – Parameter item is not an instance of ViewItem.

  • RuntimeError – The item is already attached to this view.

  • RuntimeError – The item is already attached to another view.

Returns:

The view the item was added to.

Return type:

View

classmethod from_message(message: hikari.Message, *, timeout: t.Optional[float] = 120, autodefer: bool = True) te.Self[source]#

Create a new view from the components included in the passed message. Returns an empty view if the message has no components attached.

Parameters:
  • message (hikari.Message) – The message to read components from

  • timeout (Optional[float], optional) – The timeout of the created view, by default 120

  • autodefer (bool, optional) – If enabled, interactions will be automatically deferred if not responded to within 2 seconds, by default True

Returns:

  • View – The view that represents the components attached to this message.

  • .. warning:: – This function constructs a completely new view based on the information available in the message object. Any custom behaviour (such as callbacks) will not be re-created, if you want to access an already running view that is bound to a message, use miru.view.get_view instead.

get_context(interaction: ~hikari.interactions.component_interactions.ComponentInteraction, *, cls: ~typing.Type[~miru.context.view.ViewContext] = <class 'miru.context.view.ViewContext'>) ViewContext[source]#

Get the context for this view. Override this function to provide a custom context object.

Parameters:
  • interaction (hikari.ComponentInteraction) – The interaction to construct the context from.

  • cls (Type[ViewContext], optional) – The class to use for the context, by default ViewContext.

Returns:

The context for this interaction.

Return type:

ViewContext

async on_error(error: Exception, item: ViewItem | None = None, context: ViewContextT | None = None) None[source]#

Called when an error occurs in a callback function or the built-in timeout function. Override for custom error-handling logic.

Parameters:
  • error (Exception) – The exception encountered.

  • item (Optional[MessageItem[ViewT]], optional) – The item this exception originates from, if any.

  • context (Optional[Context], optional) – The context associated with this exception, if any.

async start(message: PartialMessage | Snowflake | int | Awaitable[PartialMessage | Snowflake | int] | None = None) None[source]#

Start up the view and begin listening for interactions.

Parameters:

message (Union[hikari.Message, Awaitable[hikari.Message]]) – If provided, the view will be bound to this message, and if the message is edited with a new view, this view will be stopped. Unbound views do not support message editing with additional views.

Raises:
  • TypeError – Parameter ‘message’ cannot be resolved to an instance of ‘hikari.Message’.

  • BootstrapFailureError – miru.install() was not called before starting a view.

stop() None[source]#

Stop listening for interactions.

async view_check(context: ViewContextT) bool[source]#

Called before any callback in the view is called. Must evaluate to a truthy value to pass. Override for custom check logic.

Parameters:

context (ViewContextT) – The context for this check.

Returns:

A boolean indicating if the check passed or not.

Return type:

bool

async wait_for_input(timeout: float | None = None) None[source]#

Wait for any input to be received. This will also unblock if the view was stopped, thus it is recommended to check for the presence of a value after this function returns.

Parameters:

timeout (Optional[float], optional) – The amount of time to wait for input, in seconds, by default None

property autodefer: bool#

A boolean indicating if the received interaction should automatically be deferred if not responded to or not.

property is_bound: bool#

Determines if the view is bound to a message or not. If this is False, message edits will not be supported.

property is_persistent: bool#

Determines if this view is persistent or not.

property message: Message | None#

The message this view is bound to. Will be None if the view is not bound, or if a message_id was passed to view.start().

property message_id: Snowflake | None#

The message ID this view is bound to. Will be None if the view is an unbound persistent view.

miru.view.get_view(message: PartialMessage | Snowflake | int) View | None[source]#

Get a currently running view that is attached to the provided message.

Parameters:

message (hikari.SnowflakeishOr[hikari.PartialMessage]) – The message the view is attached to.

Returns:

The view bound to this message, if any.

Return type:

Optional[View]

Raises:

BootstrapFailureError – miru.install() was not called before this operation.