Client API Reference#
GatewayBotLike
#
Client
#
Client(
app: GatewayBotLike | hikari.InteractionServerAware,
*,
ignore_unknown_interactions: bool = False,
stop_bound_on_delete: bool = True,
injector: alluka.abc.Client | None = None
)
The miru client.
It is responsible for handling component and modal interactions and dispatching them to the correct item handler.
PARAMETER | DESCRIPTION |
---|---|
app |
The currently running app instance that will be used to receive interactions. |
ignore_unknown_interactions |
Whether to ignore unknown interactions. If True, unknown interactions will be ignored and no warnings will be logged.
TYPE:
|
stop_bound_on_delete |
Whether to automatically stop bound views when the message it is bound to is deleted. This only applies to Gateway bots. When an app without EventManagerAware is used, this will be ignored.
TYPE:
|
injector |
The injector to use for dependency injection. If None, a new injector will be created. |
event_manager
property
#
event_manager: hikari.api.EventManager
The event manager instance of the underlying app.
Warning
Accessing this property will raise a RuntimeError
if the underlying app is not EventManagerAware
.
cache
property
#
The cache instance of the underlying app.
Warning
Accessing this property will raise a RuntimeError
if the underlying app is not CacheAware
.
interaction_server
property
#
interaction_server: hikari.api.InteractionServer
The interaction server instance of the underlying app.
Warning
Accessing this property will raise a RuntimeError
if the underlying app is not InteractionServerAware
.
is_rest
property
#
is_rest: bool
Whether the app is a rest client or a gateway client.
This controls the client response flow, if True, Client.handle_component_interaction
and Client.handle_modal_interaction
will return interaction response builders to be sent back to Discord, otherwise they will return None.
ignore_unknown_interactions
property
#
ignore_unknown_interactions: bool
Whether to ignore unknown interactions.
If True, unknown interactions will be ignored and no warnings will be logged.
from_arc
classmethod
#
from_arc(
client: arc.abc.Client[t.Any],
*,
ignore_unknown_interactions: bool = False,
stop_bound_on_delete: bool = True
) -> te.Self
Create a new client from an arc client, using it's dependency injector. This can be used to share dependencies between the arc client and the miru client.
PARAMETER | DESCRIPTION |
---|---|
client |
The arc client to create the miru client from. |
ignore_unknown_interactions |
Whether to ignore unknown interactions. If True, unknown interactions will be ignored and no warnings will be logged.
TYPE:
|
stop_bound_on_delete |
Whether to automatically stop bound views when the message it is bound to is deleted. This only applies to Gateway bots. When an app without EventManagerAware is used, this will be ignored.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Client
|
The created client. |
from_tanjun
classmethod
#
from_tanjun(
client: tanjun.abc.Client,
*,
ignore_unknown_interactions: bool = False,
stop_bound_on_delete: bool = True
) -> te.Self
Create a new client from a Tanjun client, using it's dependency injector. This can be used to share dependencies between the Tanjun client and the miru client.
Note
This convenience method only works if the Tanjun client was created with a bot object, not constructed manually.
PARAMETER | DESCRIPTION |
---|---|
client |
The Tanjun client to create the miru client from. |
ignore_unknown_interactions |
Whether to ignore unknown interactions. If True, unknown interactions will be ignored and no warnings will be logged.
TYPE:
|
stop_bound_on_delete |
Whether to automatically stop bound views when the message it is bound to is deleted. This only applies to Gateway bots. When an app without EventManagerAware is used, this will be ignored.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Client
|
The created client. |
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If no |
RuntimeError
|
If the located |
handle_component_interaction
async
#
handle_component_interaction(
interaction: hikari.ComponentInteraction,
) -> ResponseBuildersT | None
Handle a component interaction.
PARAMETER | DESCRIPTION |
---|---|
interaction |
The interaction to handle.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ViewResponseBuildersT | None
|
If using a REST client, the response builders to send back to discord. |
handle_modal_interaction
async
#
handle_modal_interaction(
interaction: hikari.ModalInteraction,
) -> ModalResponseBuildersT | None
Handle a modal interaction.
PARAMETER | DESCRIPTION |
---|---|
interaction |
The interaction to handle.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ModalResponseBuildersT | None
|
If using a REST client, the response builders to send back to discord. |
get_bound_view
#
get_bound_view(
message: hikari.SnowflakeishOr[hikari.PartialMessage],
) -> View | None
Get a bound view that is currently managed by this client.
PARAMETER | DESCRIPTION |
---|---|
message |
The message object or ID of the message that the view is bound to.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
View | None
|
The view if found, otherwise None. |
get_unbound_view
#
get_modal
#
set_unhandled_component_interaction_hook
#
set_unhandled_component_interaction_hook(
hook: UnhandledCompInterHookT | None = None,
) -> (
te.Self
| t.Callable[[UnhandledCompInterHookT], te.Self]
)
Decorator to set the callback to be called for unhandled component interactions.
This will be called when a component interaction is received that is not handled by any of the currently running views.
PARAMETER | DESCRIPTION |
---|---|
hook |
The function to set as the hook.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
te.Self
|
The client for chaining calls. |
set_unhandled_modal_interaction_hook
#
set_unhandled_modal_interaction_hook(
hook: UnhandledModalInterHookT | None = None,
) -> (
te.Self
| t.Callable[[UnhandledModalInterHookT], te.Self]
)
Decorator to set the callback to be called for unhandled modal interactions.
This will be called when a modal interaction is received that is not handled by any of the currently running modals.
PARAMETER | DESCRIPTION |
---|---|
hook |
The function to set as the hook.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
te.Self
|
The client for chaining calls. |
start_view
#
start_view(
view: View,
*,
bind_to: hikari.UndefinedNoneOr[
hikari.SnowflakeishOr[hikari.PartialMessage]
] = hikari.UNDEFINED
) -> None
Add a view to this client and start it.
PARAMETER | DESCRIPTION |
---|---|
view |
The view to start.
TYPE:
|
bind_to |
The message to bind the view to. If set to
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the view is not persistent and |
!!! note
|
A view can only be unbound if it is persistent, meaning that it has a timeout of None and all it's items have explicitly defined custom_ids. If a view is not persistent, it must be bound to a message. |
start_modal
#
start_modal(modal: Modal) -> None
Add a modal to this client and start it.
PARAMETER | DESCRIPTION |
---|---|
modal |
The modal to start.
TYPE:
|
get_type_dependency
#
get_type_dependency(
type_: type[T],
*,
default: T | hikari.UndefinedType = hikari.UNDEFINED
) -> T
Get a type dependency for this client.
PARAMETER | DESCRIPTION |
---|---|
type_ |
The type of the dependency.
TYPE:
|
default |
The default value to return if the dependency does not exist.
If not specified, a |
RETURNS | DESCRIPTION |
---|---|
T
|
The instance of the dependency, or the default value if it does not exist. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If the dependency does not exist and no default was specified. |
set_type_dependency
#
set_type_dependency(
type_: type[T], instance: T
) -> te.Self
Set a type dependency for this client. This can then be injected into miru callbacks.
PARAMETER | DESCRIPTION |
---|---|
type_ |
The type of the dependency.
TYPE:
|
instance |
The instance of the dependency.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
te.Self
|
The client for chaining calls. |
Example
class Counter:
def __init__(self, value: int = 0) -> None:
self.value = value
client.set_type_dependency(Counter, Counter(0))
class SomeView(miru.View):
@miru.button(label="Counter!", style=hikari.ButtonStyle.PRIMARY)
@client.inject_dependencies
async def counter_button(
self,
ctx: miru.ViewContext,
button: miru.Button,
counter: Counter = miru.inject(),
) -> None:
counter.value += 1
await ctx.respond(f"Counter is {counter.value}")
See Also
-
Client.get_type_dependency
A method to get dependencies for the client. -
Client.inject_dependencies
A decorator to inject dependencies into arbitrary functions.
inject_dependencies
#
inject_dependencies(
func: t.Callable[P, T] | None = None
) -> (
t.Callable[P, T]
| t.Callable[
[t.Callable[P, T]], t.Callable[P, T]
]
)
Decorator to inject dependencies into the decorated function.
This should be the first (the one at the bottom) decorator of the given function.
Example
class Counter:
def __init__(self, value: int = 0) -> None:
self.value = value
client.set_type_dependency(Counter, Counter(0))
class SomeView(miru.View):
@miru.button(label="Counter!", style=hikari.ButtonStyle.PRIMARY)
@client.inject_dependencies
async def counter_button(
self,
ctx: miru.ViewContext,
button: miru.Button,
counter: Counter = miru.inject(),
) -> None:
counter.value += 1
await ctx.respond(f"Counter is {counter.value}")
See Also
Client.set_type_dependency
A method to set dependencies for the client.