Skip to content

Hook ABCs#

HookResult #

HookResult(abort: bool = False)

The result of a hook.

PARAMETER DESCRIPTION
abort

Whether to abort the execution of the command. If True, the command execution will be silently aborted. If this is undesired, you should raise an exception instead.

TYPE: bool DEFAULT: False

Hookable #

Bases: ABC, Generic[ClientT]

A trait for objects that can have hooks added to them.

hooks abstractmethod property #

hooks: MutableSequence[HookT[ClientT]]

The pre-execution hooks for this object.

post_hooks abstractmethod property #

post_hooks: MutableSequence[PostHookT[ClientT]]

The post-execution hooks for this object.

add_hook #

add_hook(hook: HookT[ClientT]) -> Self

Add a new pre-execution hook to this object.

Any function that takes a Context as its sole parameter and returns either a HookResult or None can be used as a hook.

PARAMETER DESCRIPTION
hook

The hook to add.

TYPE: HookT[ClientT]

RETURNS DESCRIPTION
Self

This object for chaining.

add_post_hook #

add_post_hook(hook: PostHookT[ClientT]) -> Self

Add a new post-execution hook to this object.

Any function that takes a Context as its sole parameter and returns None can be used as a post-hook.

PARAMETER DESCRIPTION
hook

The post-execution hook to add.

TYPE: PostHookT[ClientT]

RETURNS DESCRIPTION
Self

This object for chaining.

with_hook #

with_hook(hook: HookT[ClientT]) -> Callable[[HookableT], HookableT]

Add a new pre-execution hook to a hookable object. It will run before the command callback.

Any function that takes a Context as its sole parameter and returns either a HookResult or None can be used as a hook.

Example
@client.include
@arc.with_hook(arc.guild_only) # Add a pre-execution hook to a command
@arc.slash_command("foo", "Foo command description")
async def foo(ctx: arc.GatewayContext) -> None:
    ...

with_post_hook #

with_post_hook(hook: PostHookT[ClientT]) -> Callable[[HookableT], HookableT]

Add a new post-execution hook to a hookable object. It will run after the command callback.

Any function that takes a Context as its sole parameter and returns None can be used as a post-hook.

Post-execution hooks are not executed if a pre-execution hook aborts the execution of the command.

Warning

Post-execution hooks are called even if the command callback raises an exception. You can see if the command callback failed by checking Context.has_command_failed.

Example
@client.include
@arc.with_post_hook(arc.guild_only) # Add a post-execution hook to a command
@arc.slash_command("foo", "Foo command description")
async def foo(ctx: arc.GatewayContext) -> None:
    ...