Skip to content

tanjun.hooks#

Standard implementation of Tanjun's command execution hook models.

AnyHooks = Hooks[tanjun.Context] module-attribute #

Hooks that can be used with any context.

Note

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

MenuHooks = Hooks[tanjun.MenuContext] module-attribute #

Hooks that can be used with a menu context.

Note

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

MessageHooks = Hooks[tanjun.MessageContext] module-attribute #

Hooks that can be used with a message context.

Note

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

SlashHooks = Hooks[tanjun.SlashContext] module-attribute #

Hooks that can be used with a slash context.

Note

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

Hooks #

Bases: tanjun.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__() #

Initialise a command hook object.

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!")
PARAMETER DESCRIPTION
command

The command to add the hooks to.

TYPE: tanjun.abc.ExecutableCommand

RETURNS DESCRIPTION
tanjun.abc.ExecutableCommand

The command with the hooks added.

copy() #

Copy this hook object.

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.

PARAMETER DESCRIPTION
callback

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.

TYPE: typing.Optional[tanjun.ErrorHookSig]

RETURNS DESCRIPTION
Self

The hook object to enable method chaining.

set_on_parser_error(callback) #

Set the parser error callback for this hook object.

PARAMETER DESCRIPTION
callback

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.

TYPE: typing.Optional[tanjun.HookSig]

RETURNS DESCRIPTION
Self

The hook object to enable method chaining.

set_on_success(callback) #

Set the success callback for this hook object.

PARAMETER DESCRIPTION
callback

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.

TYPE: typing.Optional[tanjun.HookSig]

RETURNS DESCRIPTION
Self

The hook object to enable method chaining.

set_post_execution(callback) #

Set the post-execution callback for this hook object.

PARAMETER DESCRIPTION
callback

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.

TYPE: typing.Optional[tanjun.HookSig]

RETURNS DESCRIPTION
Self

The hook object to enable method chaining.

set_pre_execution(callback) #

Set the pre-execution callback for this hook object.

PARAMETER DESCRIPTION
callback

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.

TYPE: typing.Optional[tanjun.HookSig]

RETURNS DESCRIPTION
Self

The hook object to enable method chaining.