Context API Reference#

class miru.context.Context(interaction: InteractionT)[source]#

Bases: ABC, Generic[InteractionT]

An abstract base class for context objects that proxying a Discord interaction.

async defer(response_type: ResponseType, *, flags: int | MessageFlag | UndefinedType = hikari.UNDEFINED) None[source]#
async defer(*, flags: int | MessageFlag | UndefinedType = hikari.UNDEFINED) None

Short-hand method to defer an interaction response. Raises RuntimeError if the interaction was already responded to.

Parameters:
  • response_type (hikari.ResponseType, optional) – The response-type of this defer action. Defaults to DEFERRED_MESSAGE_UPDATE.

  • flags (Union[int, hikari.MessageFlag, None], optional) – Message flags that should be included with this defer request, by default None

Raises:
  • RuntimeError – The interaction was already responded to.

  • ValueError – response_type was not a deferred response type.

async edit_response(content: hikari.UndefinedNoneOr[t.Any] = UNDEFINED, *, flags: t.Union[int, hikari.MessageFlag, hikari.UndefinedType] = UNDEFINED, tts: hikari.UndefinedOr[bool] = UNDEFINED, component: hikari.UndefinedNoneOr[hikari.api.ComponentBuilder] = UNDEFINED, components: hikari.UndefinedNoneOr[t.Sequence[hikari.api.ComponentBuilder]] = UNDEFINED, attachment: hikari.UndefinedNoneOr[hikari.Resourceish] = UNDEFINED, attachments: hikari.UndefinedNoneOr[t.Sequence[hikari.Resourceish]] = UNDEFINED, embed: hikari.UndefinedNoneOr[hikari.Embed] = UNDEFINED, embeds: hikari.UndefinedNoneOr[t.Sequence[hikari.Embed]] = UNDEFINED, mentions_everyone: hikari.UndefinedOr[bool] = UNDEFINED, user_mentions: hikari.UndefinedOr[t.Union[hikari.SnowflakeishSequence[hikari.PartialUser], bool]] = UNDEFINED, role_mentions: hikari.UndefinedOr[t.Union[hikari.SnowflakeishSequence[hikari.PartialRole], bool]] = UNDEFINED) InteractionResponse[source]#

A short-hand method to edit the initial response belonging to this interaction. If no initial response was issued yet, this will create one of type MESSAGE_UPDATE. In the case of modals, this will be the component’s message that triggered the modal.

Parameters:
  • content (hikari.UndefinedOr[Any], optional) – The content of the message. Anything passed here will be cast to str.

  • tts (hikari.UndefinedOr[bool], optional) – If the message should be tts or not.

  • attachment (hikari.UndefinedOr[hikari.Resourceish], optional) – An attachment to add to this message.

  • attachments (hikari.UndefinedOr[t.Sequence[hikari.Resourceish]], optional) – A sequence of attachments to add to this message.

  • component (hikari.UndefinedOr[hikari.api.special_endpoints.ComponentBuilder], optional) – A component to add to this message.

  • components (hikari.UndefinedOr[t.Sequence[hikari.api.special_endpoints.ComponentBuilder]], optional) – A sequence of components to add to this message.

  • embed (hikari.UndefinedOr[hikari.Embed], optional) – An embed to add to this message.

  • embeds (hikari.UndefinedOr[Sequence[hikari.Embed]], optional) – A sequence of embeds to add to this message.

  • mentions_everyone (hikari.UndefinedOr[bool], optional) – If True, mentioning @everyone will be allowed.

  • user_mentions (hikari.UndefinedOr[Union[hikari.SnowflakeishSequence[hikari.PartialUser], bool]], optional) – The set of allowed user mentions in this message. Set to True to allow all.

  • role_mentions (hikari.UndefinedOr[Union[hikari.SnowflakeishSequence[hikari.PartialRole], bool]], optional) – The set of allowed role mentions in this message. Set to True to allow all.

  • flags (Union[hikari.UndefinedType, int, hikari.MessageFlag], optional) – Message flags that should be included with this message.

Returns:

A proxy object representing the response to the interaction.

Return type:

InteractionResponse

get_channel() TextableGuildChannel | None[source]#

Gets the channel this context represents, None if in a DM. Requires application cache.

get_guild() GatewayGuild | None[source]#

Gets the guild this context represents, if any. Requires application cache.

async get_last_response() InteractionResponse[source]#

Get the last response issued to the interaction this context is proxying.

Returns:

The response object.

Return type:

InteractionResponse

Raises:

RuntimeError – The interaction was not yet responded to.

async respond(content: hikari.UndefinedOr[t.Any] = UNDEFINED, *, flags: t.Union[int, hikari.MessageFlag, hikari.UndefinedType] = UNDEFINED, tts: hikari.UndefinedOr[bool] = UNDEFINED, component: hikari.UndefinedOr[hikari.api.ComponentBuilder] = UNDEFINED, components: hikari.UndefinedOr[t.Sequence[hikari.api.ComponentBuilder]] = UNDEFINED, attachment: hikari.UndefinedOr[hikari.Resourceish] = UNDEFINED, attachments: hikari.UndefinedOr[t.Sequence[hikari.Resourceish]] = UNDEFINED, embed: hikari.UndefinedOr[hikari.Embed] = UNDEFINED, embeds: hikari.UndefinedOr[t.Sequence[hikari.Embed]] = UNDEFINED, mentions_everyone: hikari.UndefinedOr[bool] = UNDEFINED, user_mentions: hikari.UndefinedOr[t.Union[hikari.SnowflakeishSequence[hikari.PartialUser], bool]] = UNDEFINED, role_mentions: hikari.UndefinedOr[t.Union[hikari.SnowflakeishSequence[hikari.PartialRole], bool]] = UNDEFINED, delete_after: hikari.UndefinedOr[t.Union[float, int, datetime.timedelta]] = UNDEFINED) InteractionResponse[source]#

Short-hand method to create a new message response via the interaction this context represents.

Parameters:
  • content (hikari.UndefinedOr[Any], optional) – The content of the message. Anything passed here will be cast to str.

  • tts (hikari.UndefinedOr[bool], optional) – If the message should be tts or not.

  • attachment (hikari.UndefinedOr[hikari.Resourceish], optional) – An attachment to add to this message.

  • attachments (hikari.UndefinedOr[t.Sequence[hikari.Resourceish]], optional) – A sequence of attachments to add to this message.

  • component (hikari.UndefinedOr[hikari.api.special_endpoints.ComponentBuilder], optional) – A component to add to this message.

  • components (hikari.UndefinedOr[t.Sequence[hikari.api.special_endpoints.ComponentBuilder]], optional) – A sequence of components to add to this message.

  • embed (hikari.UndefinedOr[hikari.Embed], optional) – An embed to add to this message.

  • embeds (hikari.UndefinedOr[Sequence[hikari.Embed]], optional) – A sequence of embeds to add to this message.

  • mentions_everyone (hikari.UndefinedOr[bool], optional) – If True, mentioning @everyone will be allowed.

  • user_mentions (hikari.UndefinedOr[Union[hikari.SnowflakeishSequence[hikari.PartialUser], bool]], optional) – The set of allowed user mentions in this message. Set to True to allow all.

  • role_mentions (hikari.UndefinedOr[Union[hikari.SnowflakeishSequence[hikari.PartialRole], bool]], optional) – The set of allowed role mentions in this message. Set to True to allow all.

  • flags (Union[hikari.UndefinedType, int, hikari.MessageFlag], optional) – Message flags that should be included with this message.

  • delete_after (hikari.undefinedOr[Union[float, int, datetime.timedelta]], optional) – Delete the response after the specified delay.

Returns:

A proxy object representing the response to the interaction.

Return type:

InteractionResponse

property app: MiruAware#

The application that loaded miru.

property app_permissions: Permissions | None#

The permissions of the user who triggered the interaction. Will be None in DMs.

property author: User#

Alias for Context.user.

property bot: MiruAware#

The application that loaded miru.

property channel_id: Snowflake#

The ID of the channel the context represents.

property custom_id: str#

The developer provided unique identifier for the interaction this context is proxying.

property guild_id: t.Optional[Snowflake]#

The ID of the guild the context represents. Will be None in DMs.

property guild_locale: str | Locale | None#

The guild locale of this context, if in a guild. This will default to en-US if not a community guild.

property interaction: InteractionT#

The underlying interaction object.

Warning

This should not be used directly in most cases, and is only exposed for advanced use cases.

If you use the interaction to create a response in a view, you should disable the autodefer feature in your View.

property is_valid: bool#

Returns if the underlying interaction expired or not. This is not 100% accurate due to API latency, but should be good enough for most use cases.

property locale: str | Locale#

The locale of this context.

property member: InteractionMember | None#

The member who triggered this interaction. Will be None in DMs.

property responses: Sequence[InteractionResponse]#

A list of all responses issued to the interaction this context is proxying.

property user: User#

The user who triggered this interaction.

class miru.context.InteractionResponse(context: Context[InteractionT], message: Message | None = None)[source]#

Bases: object

Represents a response to an interaction, allows for standardized handling of responses. This class is not meant to be directly instantiated, and is instead returned by miru.context.Context.

async delete() None[source]#

Delete the response issued to the interaction this object represents.

delete_after(delay: int | float | timedelta) None[source]#

Delete the response after the specified delay.

Parameters:

delay (Union[int, float, datetime.timedelta]) – The delay after which the response should be deleted.

async edit(content: hikari.UndefinedOr[t.Any] = UNDEFINED, *, component: hikari.UndefinedOr[hikari.api.ComponentBuilder] = UNDEFINED, components: hikari.UndefinedOr[t.Sequence[hikari.api.ComponentBuilder]] = UNDEFINED, attachment: hikari.UndefinedOr[hikari.Resourceish] = UNDEFINED, attachments: hikari.UndefinedOr[t.Sequence[hikari.Resourceish]] = UNDEFINED, embed: hikari.UndefinedOr[hikari.Embed] = UNDEFINED, embeds: hikari.UndefinedOr[t.Sequence[hikari.Embed]] = UNDEFINED, mentions_everyone: hikari.UndefinedOr[bool] = UNDEFINED, user_mentions: hikari.UndefinedOr[t.Union[hikari.SnowflakeishSequence[hikari.PartialUser], bool]] = UNDEFINED, role_mentions: hikari.UndefinedOr[t.Union[hikari.SnowflakeishSequence[hikari.PartialRole], bool]] = UNDEFINED) InteractionResponse[source]#

A short-hand method to edit the message belonging to this response.

Parameters:
  • content (undefined.UndefinedOr[t.Any], optional) – The content of the message. Anything passed here will be cast to str.

  • attachment (undefined.UndefinedOr[hikari.Resourceish], optional) – An attachment to add to this message.

  • attachments (undefined.UndefinedOr[t.Sequence[hikari.Resourceish]], optional) – A sequence of attachments to add to this message.

  • component (undefined.UndefinedOr[hikari.api.special_endpoints.ComponentBuilder], optional) – A component to add to this message.

  • components (undefined.UndefinedOr[t.Sequence[hikari.api.special_endpoints.ComponentBuilder]], optional) – A sequence of components to add to this message.

  • embed (undefined.UndefinedOr[hikari.Embed], optional) – An embed to add to this message.

  • embeds (undefined.UndefinedOr[t.Sequence[hikari.Embed]], optional) – A sequence of embeds to add to this message.

  • mentions_everyone (undefined.UndefinedOr[bool], optional) – If True, mentioning @everyone will be allowed.

  • user_mentions (undefined.UndefinedOr[t.Union[hikari.SnowflakeishSequence[hikari.PartialUser], bool]], optional) – The set of allowed user mentions in this message. Set to True to allow all.

  • role_mentions (undefined.UndefinedOr[t.Union[hikari.SnowflakeishSequence[hikari.PartialRole], bool]], optional) – The set of allowed role mentions in this message. Set to True to allow all.

Returns:

A proxy object representing the response to the interaction.

Return type:

InteractionResponse

async retrieve_message() Message[source]#

Get or fetch the message created by this response. Initial responses need to be fetched, while followups will be provided directly.

Note

The object itself can also be awaited directly, which in turn calls this method, producing the same results.

Returns:

The message created by this response.

Return type:

hikari.Message

class miru.context.ModalContext(modal: Modal, interaction: hikari.ModalInteraction, values: t.Mapping[ModalItem, str])[source]#

Bases: RawModalContext

A context object proxying a ModalInteraction received by a miru modal.

get_value_by(predicate: t.Callable[[ModalItem], bool], default: hikari.UndefinedOr[T] = UNDEFINED) T | str[source]#

Get the value for the first modal item that matches the given predicate.

Parameters:
  • predicate (Callable[[ModalItem], bool]) – A predicate to match the item.

  • default (hikari.UndefinedOr[T], optional) – A default value to return if no item was matched, by default hikari.UNDEFINED

Returns:

The value of the item that matched the predicate or the default value.

Return type:

T | str

Raises:

KeyError – The item was not found and no default was provided.

get_value_by_id(custom_id: str, default: T | UndefinedType = UNDEFINED) T | str[source]#

Get the value for a modal item with the given custom ID.

Parameters:
  • custom_id (str) – The custom_id of the component.

  • default (hikari.UndefinedOr[T], optional) – A default value to return if the item was not found, by default hikari.UNDEFINED

Returns:

The value of the item with the given custom ID or the default value.

Return type:

T | str

Raises:

KeyError – The item was not found and no default was provided.

get_value_by_predicate(predicate: t.Callable[[ModalItem], bool], default: hikari.UndefinedOr[T] = UNDEFINED) T | str[source]#

Get the value for the first modal item that matches the given predicate.

Deprecated#

Will be removed in 3.5.0. Use ModalContext.get_value_by instead.

param predicate:

A predicate to match the item.

type predicate:

Callable[[ModalItem], bool]

param default:

A default value to return if no item was matched, by default hikari.UNDEFINED

type default:

hikari.UndefinedOr[T], optional

returns:

The value of the item that matched the predicate or the default value.

rtype:

T | str

raises KeyError:

The item was not found and no default was provided.

property modal: Modal#

The modal this context originates from.

property values: t.Mapping[ModalItem, str]#

The values received as input for this modal.

class miru.context.RawComponentContext(interaction: InteractionT)[source]#

Bases: Context[ComponentInteraction]

Raw context proxying component interactions received directly over the gateway.

async respond_with_modal(modal: Modal) None[source]#

Respond to this interaction with a modal.

property message: Message#

The message object for the view this context is proxying.

class miru.context.RawModalContext(interaction: InteractionT)[source]#

Bases: Context[ModalInteraction]

Raw context object proxying a ModalInteraction received directly over the gateway.

class miru.context.ViewContext(view: View, interaction: hikari.ComponentInteraction)[source]#

Bases: RawComponentContext

A context object proxying a ComponentInteraction for a view item.

property view: View#

The view this context originates from.