Skip to content

tanjun.hooks#

Standard implementation of Tanjun's command execution hook models.

AnyHooks module-attribute #

AnyHooks = Hooks[tanjun.Context]

Hooks that can be used with any context.

Note

This is shorthand for Hooks[tanjun.abc.Context].

MenuHooks module-attribute #

MenuHooks = Hooks[tanjun.MenuContext]

Hooks that can be used with a menu context.

Note

This is shorthand for Hooks[tanjun.abc.MenuContext].

MessageHooks module-attribute #

MessageHooks = Hooks[tanjun.MessageContext]

Hooks that can be used with a message context.

Note

This is shorthand for Hooks[tanjun.abc.MessageContext].

SlashHooks module-attribute #

SlashHooks = Hooks[tanjun.SlashContext]

Hooks that can be used with a slash context.

Note

This is shorthand for Hooks[tanjun.abc.SlashContext].

Hooks #

Bases: Hooks[_ContextT_contra]

Standard implementation of tanjun.abc.Hooks used for command execution.

This will take either tanjun.abc.Context, tanjun.abc.MessageContext or tanjun.abc.SlashContext dependent on what its bound by (generic wise).

Note

This implementation adds a concept of parser errors which won't be dispatched to general "error" hooks and do not share the error suppression semantics as they favour to always suppress the error if a registered handler is found.

__init__ #

__init__()

Initialise a command hook object.

add_to_command #

add_to_command(command)

Add this hook object to a command.

Note

This will likely override any previously added hooks.

Examples:

This method may be used as a command decorator:

@standard_hooks.add_to_command
@as_message_command("command")
async def command_command(ctx: tanjun.abc.Context) -> None:
    await ctx.respond("You've called a command!")

Parameters:

  • command (ExecutableCommand) –

    The command to add the hooks to.

Returns:

  • ExecutableCommand

    The command with the hooks added.

copy #

copy()

Copy this hook object.

set_on_error #

set_on_error(callback)

Set the error callback for this hook object.

Note

This will not be called for tanjun.ParserErrors as these are generally speaking expected. To handle those see Hooks.set_on_parser_error.

Parameters:

  • callback (Optional[ErrorHookSig[_ContextT_contra]]) –

    The callback to set for this hook. This will remove any previously set callbacks.

    This callback should take two positional arguments (of type tanjun.abc.Context and Exception) and may be either synchronous or asynchronous.

    Returning True indicates that the error should be suppressed, False that it should be re-raised and None that no decision has been made. This will be accounted for along with the decisions other error hooks make by majority rule.

Returns:

  • Self

    The hook object to enable method chaining.

set_on_parser_error #

set_on_parser_error(callback)

Set the parser error callback for this hook object.

Parameters:

  • callback (Optional[ParserHookSig[_ContextT_contra]]) –

    The callback to set for this hook. This will remove any previously set callbacks.

    This callback should take two positional arguments (of type tanjun.abc.Context and tanjun.ParserError), return None and may be either synchronous or asynchronous.

    It's worth noting that, unlike general error handlers, this will always suppress the error.

Returns:

  • Self

    The hook object to enable method chaining.

set_on_success #

set_on_success(callback)

Set the success callback for this hook object.

Parameters:

  • callback (Optional[HookSig[_ContextT_contra]]) –

    The callback to set for this hook. This will remove any previously set callbacks.

    This callback should take one positional argument (of type tanjun.abc.Context), return None and may be either synchronous or asynchronous.

Returns:

  • Self

    The hook object to enable method chaining.

set_post_execution #

set_post_execution(callback)

Set the post-execution callback for this hook object.

Parameters:

  • callback (Optional[HookSig[_ContextT_contra]]) –

    The callback to set for this hook. This will remove any previously set callbacks.

    This callback should take one positional argument (of type tanjun.abc.Context), return None and may be either synchronous or asynchronous.

Returns:

  • Self

    The hook object to enable method chaining.

set_pre_execution #

set_pre_execution(callback)

Set the pre-execution callback for this hook object.

Parameters:

  • callback (Optional[HookSig[_ContextT_contra]]) –

    The callback to set for this hook. This will remove any previously set callbacks.

    This callback should take one positional argument (of type tanjun.abc.Context), return None and may be either synchronous or asynchronous.

Returns:

  • Self

    The hook object to enable method chaining.