Skip to content

tanjun.clients#

Standard Tanjun client.

ClientCallbackNames module-attribute #

ClientCallbackNames = ClientCallbackNames

PrefixGetterSig module-attribute #

PrefixGetterSig = Callable[Concatenate[MessageContext, ...], Coroutine[Any, Any, Iterable[str]]]

Type hint of a callable used to get the prefix(es) for a specific guild.

This represents the callback async def (tanjun.abc.MessageContext, ...) -> collections.Iterable[str] where dependency injection is supported.

Client #

Bases: Client

Tanjun's standard tanjun.abc.Client implementation.

This implementation supports dependency injection for checks, command callbacks, prefix getters and event listeners. For more information on how this works see alluka.

When manually managing the lifetime of the client the linked rest app or bot must always be started before the Tanjun client.

Note

By default this client includes a parser error handling hook which will by overwritten if you call Client.set_hooks.

checks property #

checks

Collection of the level tanjun.abc.Context checks registered to this client.

Note

These may be taking advantage of the standard dependency injection.

hooks property #

hooks

Top level tanjun.abc.AnyHooks set for this client.

These are called during both message, menu and slash command execution.

interaction_accepts property #

interaction_accepts

The types of interactions this client is executing.

is_human_only property #

is_human_only

Whether this client is only executing for non-bot/webhook users messages.

menu_hooks property #

menu_hooks

Top level tanjun.abc.MenuHooks set for this client.

These are only called during menu command execution.

message_accepts property #

message_accepts

Type of message create events this command client accepts for execution.

message_hooks property #

message_hooks

Top level tanjun.abc.MessageHooks set for this client.

These are only called during message command execution.

prefix_getter property #

prefix_getter

Prefix getter method set for this client.

For more information on this callback's signature see PrefixGetterSig.

prefixes property #

prefixes

Collection of the standard prefixes set for this client.

slash_hooks property #

slash_hooks

Top level tanjun.abc.SlashHooks set for this client.

These are only called during slash command execution.

__init__ #

__init__(rest, *, cache=None, events=None, server=None, shards=None, voice=None, event_managed=False, injector=None, mention_prefix=False, set_global_commands=False, declare_global_commands=False, command_ids=None, message_ids=None, user_ids=None, _stack_level=0)

Initialise a Tanjun client.

Note

For a quicker way to initiate this client around a standard bot aware client, see Client.from_gateway_bot and Client.from_rest_bot.

Parameters:

  • rest (RESTClient) –

    The Hikari REST client this will use.

  • cache (Optional[Cache], default: None ) –

    The Hikari cache client this will use if applicable.

  • events (Optional[EventManager], default: None ) –

    The Hikari event manager client this will use if applicable.

    This is necessary for message command dispatch and will also be necessary for interaction command dispatch if server isn't provided.

  • server (Optional[InteractionServer], default: None ) –

    The Hikari interaction server client this will use if applicable.

    This is used for interaction command dispatch if interaction events aren't being received from the event manager.

  • shards (Optional[ShardAware], default: None ) –

    The Hikari shard aware client this will use if applicable.

  • voice (Optional[VoiceComponent], default: None ) –

    The Hikari voice component this will use if applicable.

  • event_managed (bool, default: False ) –

    Whether or not this client is managed by the event manager.

    An event managed client will be automatically started and closed based on Hikari's lifetime events.

    This can only be passed as True if events is also provided.

  • injector (Optional[Client], default: None ) –

    The alluka client this should use for dependency injection.

    If not provided then the client will initialise its own DI client.

  • mention_prefix (bool, default: False ) –

    Whether or not mention prefixes should be automatically set when this client is first started.

    It should be noted that this only applies to message commands.

  • declare_global_commands (Union[SnowflakeishSequence[PartialGuild], SnowflakeishOr[PartialGuild], bool], default: False ) –

    Whether or not to automatically set global slash commands when this client is first started.

    If one or more guild objects/IDs are passed here then the registered global commands will be set on the specified guild(s) at startup rather than globally.

    The endpoint this uses has a strict ratelimit which, as of writing, only allows for 2 requests per minute (with that ratelimit either being per-guild if targeting a specific guild otherwise globally).

  • command_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of top level command names to IDs of the existing commands to update.

    This will be used for all application commands but in cases where commands have overlapping names, message_ids and user_ids will take priority over this for their relevant command type.

    This field is complementary to declare_global_commands and, while it isn't necessarily required, this will in some situations help avoid permissions which were previously set for a command from being lost after a rename.

    This currently isn't supported when multiple guild IDs are passed for declare_global_commands.

  • message_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of message context menu command names to the IDs of existing commands to update.

  • user_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of user context menu command names to the IDs of existing commands to update.

Raises:

  • ValueError

    Raises for the following reasons:

    • If event_managed is True when event_manager is None.
    • If command_ids is passed when multiple guild ids are provided for declare_global_commands.
    • If command_ids is passed when declare_global_commands is False.

add_check #

add_check(*checks)

Add a generic check to this client.

This will be applied to both message and slash command execution.

Parameters:

  • *checks (AnyCheckSig, default: () ) –

    The checks to add. These may be either synchronous or asynchronous and must take one positional argument of type tanjun.abc.Context with dependency injection being supported for its keyword arguments.

Returns:

  • Self

    The client instance to enable chained calls.

add_component #

add_component(component)

Add a component to this client.

Parameters:

  • component (Component) –

    The component to move to this client.

Returns:

  • Self

    The client instance to allow chained calls.

Raises:

  • ValueError

    If the component's name is already registered.

add_prefix #

add_prefix(prefixes)

Add a prefix used to filter message command calls.

This will be matched against the first character(s) in a message's content to determine whether the message command search stage of execution should be initiated.

Parameters:

  • prefixes (Union[Iterable[str], str]) –

    Either a single string or an iterable of strings to be used as prefixes.

Returns:

  • Self

    The client instance to enable chained calls.

close async #

close(*, deregister_listeners=True)

Close the client.

Raises:

fetch_rest_application_id async #

fetch_rest_application_id()

Fetch the ID of the application this client is linked to.

Returns:

  • Snowflake

    The application ID of the application this client is linked to.

from_gateway_bot classmethod #

from_gateway_bot(bot, /, *, event_managed=True, injector=None, mention_prefix=False, declare_global_commands=False, set_global_commands=False, command_ids=None, message_ids=None, user_ids=None)

Build a Client from a gateway bot.

Note

This defaults the client to human only mode and sets type dependency injectors for the hikari traits present in bot.

Parameters:

  • bot (ShardAware & RESTAware & EventManagerAware) –

    The bot client to build from.

    This will be used to infer the relevant Hikari clients to use.

  • event_managed (bool, default: True ) –

    Whether or not this client is managed by the event manager.

    An event managed client will be automatically started and closed based on Hikari's lifetime events.

  • injector (Optional[Client], default: None ) –

    The alluka client this should use for dependency injection.

    If not provided then the client will initialise its own DI client.

  • mention_prefix (bool, default: False ) –

    Whether or not mention prefixes should be automatically set when this client is first started.

    It should be noted that this only applies to message commands.

  • declare_global_commands (Union[SnowflakeishSequence[PartialGuild], SnowflakeishOr[PartialGuild], bool], default: False ) –

    Whether or not to automatically set global slash commands when this client is first started.

    If one or more guild objects/IDs are passed here then the registered global commands will be set on the specified guild(s) at startup rather than globally.

    The endpoint this uses has a strict ratelimit which, as of writing, only allows for 2 requests per minute (with that ratelimit either being per-guild if targeting a specific guild otherwise globally).

  • command_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of top level command names to IDs of the commands to update.

    This field is complementary to declare_global_commands and, while it isn't necessarily required, this will in some situations help avoid permissions which were previously set for a command from being lost after a rename.

    This currently isn't supported when multiple guild IDs are passed for declare_global_commands.

  • message_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of message context menu command names to the IDs of existing commands to update.

  • user_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of user context menu command names to the IDs of existing commands to update.

from_rest_bot classmethod #

from_rest_bot(bot, /, *, bot_managed=False, declare_global_commands=False, injector=None, set_global_commands=False, command_ids=None, message_ids=None, user_ids=None)

Build a Client from a hikari.RESTBotAware instance.

Note

This sets type dependency injectors for the hikari traits present in bot (including hikari.RESTBotAware).

Parameters:

  • bot (RESTBotAware) –

    The bot client to build from.

  • declare_global_commands (Union[SnowflakeishSequence[PartialGuild], SnowflakeishOr[PartialGuild], bool], default: False ) –

    Whether or not to automatically set global slash commands when this client is first started.

    If one or more guild objects/IDs are passed here then the registered global commands will be set on the specified guild(s) at startup rather than globally.

    The endpoint this uses has a strict ratelimit which, as of writing, only allows for 2 requests per minute (with that ratelimit either being per-guild if targeting a specific guild otherwise globally).

  • bot_managed (bool, default: False ) –

    Whether the client should be managed by the REST bot.

    A REST bot managed client will be automatically started and closed based on the REST bot's startup and shutdown callbacks.

  • injector (Optional[Client], default: None ) –

    The alluka client this should use for dependency injection.

    If not provided then the client will initialise its own DI client.

  • command_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of top level command names to IDs of the existing commands to update.

    This will be used for all application commands but in cases where commands have overlapping names, message_ids and user_ids will take priority over this for their relevant command type.

    This field is complementary to declare_global_commands and, while it isn't necessarily required, this will in some situations help avoid permissions which were previously set for a command from being lost after a rename.

    This currently isn't supported when multiple guild IDs are passed for declare_global_commands.

  • message_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of message context menu command names to the IDs of existing commands to update.

  • user_ids (Optional[Mapping[str, SnowflakeishOr[PartialCommand]]], default: None ) –

    If provided, a mapping of user context menu command names to the IDs of existing commands to update.

on_autocomplete_interaction_request async #

on_autocomplete_interaction_request(interaction)

Execute a command autocomplete based on received REST requests.

Parameters:

  • interaction (AutocompleteInteraction) –

    The interaction to execute autocomplete based on.

Returns:

on_command_interaction_request async #

on_command_interaction_request(interaction)

Execute an app command based on received REST requests.

Parameters:

  • interaction (CommandInteraction) –

    The interaction to execute a command based on.

Returns:

on_gateway_autocomplete_create async #

on_gateway_autocomplete_create(interaction)

Execute command autocomplete based on a received gateway interaction create.

Parameters:

  • interaction (AutocompleteInteraction) –

    The interaction to execute a command based on.

on_gateway_command_create async #

on_gateway_command_create(interaction)

Execute an app command based on a received gateway interaction create.

Parameters:

  • interaction (CommandInteraction) –

    The interaction to execute a command based on.

on_interaction_create_event async #

on_interaction_create_event(event)

Handle a gateway interaction create event.

This will execute both application command and autocomplete interactions.

Parameters:

  • event (InteractionCreateEvent) –

    The event to execute commands based on.

on_message_create_event async #

on_message_create_event(event)

Execute a message command based on a gateway event.

Parameters:

  • event (MessageCreateEvent) –

    The event to handle.

open async #

open(*, register_listeners=True)

Start the client.

If mention_prefix was passed to Client.__init__ or Client.from_gateway_bot then this function may make a fetch request to Discord if it cannot get the current user from the cache.

Raises:

remove_check #

remove_check(check)

Remove a check from the client.

Parameters:

Raises:

  • ValueError

    If the check was not previously added.

remove_prefix #

remove_prefix(prefix)

Remove a message content prefix from the client.

Parameters:

  • prefix (str) –

    The prefix to remove.

Raises:

  • ValueError

    If the prefix is not registered with the client.

Returns:

  • Self

    The client instance to enable chained calls.

set_auto_defer_after #

set_auto_defer_after(time)

Set when this client should automatically defer execution of commands.

Warning

If time is set to None then automatic deferrals will be disabled. This may lead to unexpected behaviour.

Parameters:

  • time (Optional[float]) –

    The time in seconds to defer interaction command responses after.

set_autocomplete_ctx_maker #

set_autocomplete_ctx_maker(maker=context.AutocompleteContext)

Set the autocomplete context maker to use when creating contexts.

Warning

The caller must return an instance of tanjun.AutocompleteContext rather than just any implementation of the AutocompleteContext abc due to this client relying on implementation detail of tanjun.AutocompleteContext.

Parameters:

Returns:

  • Self

    This client to enable method chaining.

set_case_sensitive #

set_case_sensitive(state)

Set whether this client defaults to being case sensitive for message commands.

Parameters:

  • state (bool) –

    Whether this client's message commands should be matched case-sensitively.

    This may be overridden by component specific configuration.

set_default_app_command_permissions #

set_default_app_command_permissions(permissions)

Set the default member permissions needed for this client's commands.

Warning

This may be overridden by guild staff and does not apply to admins.

Parameters:

Returns:

  • Self

    This client to enable method chaining.

set_dms_enabled_for_app_cmds #

set_dms_enabled_for_app_cmds(state)

Set whether this clients's commands should be enabled in DMs.

Parameters:

Returns:

  • Self

    This client to enable method chaining.

set_ephemeral_default #

set_ephemeral_default(state)

Set whether slash contexts spawned by this client should default to ephemeral responses.

This defaults to False if not explicitly set.

Parameters:

  • state (bool) –

    Whether slash command contexts executed in this client should should default to ephemeral.

    This will be overridden by any response calls which specify flags.

Returns:

  • Self

    This client to enable method chaining.

set_global_commands async #

set_global_commands(*, application=None, guild=hikari.UNDEFINED, force=False)

Alias of Client.declare_global_commands.

deprecated

Since v2.1.1a1; use Client.declare_global_commands instead.

set_hikari_trait_injectors #

set_hikari_trait_injectors(bot)

Set type based dependency injection based on the hikari traits found in bot.

This is a short hand for calling Client.set_type_dependency for all the hikari trait types bot is valid for with bot.

Parameters:

  • bot (RESTAware) –

    The hikari client to set dependency injectors for.

set_hooks #

set_hooks(hooks)

Set the general command execution hooks for this client.

The callbacks within this hook will be added to every slash and message command execution started by this client.

Parameters:

  • hooks (Optional[AnyHooks]) –

    The general command execution hooks to set for this client.

    Passing None will remove all hooks.

Returns:

  • Self

    The client instance to enable chained calls.

set_human_only #

set_human_only(value=True)

Set whether or not message commands execution should be limited to "human" users.

Note

This doesn't apply to interaction commands as these can only be triggered by a "human" (normal user account).

Parameters:

  • value (bool, default: True ) –

    Whether or not message commands execution should be limited to "human" users.

    Passing True here will prevent message commands from being executed based on webhook and bot messages.

set_interaction_accepts #

set_interaction_accepts(accepts)

Set the kind of interactions this client should execute.

Parameters:

Raises:

set_interaction_not_found #

set_interaction_not_found(message)

Set the response message for when an interaction command is not found.

Warning

Setting this to None may lead to unexpected behaviour (especially when the client is still set to auto-defer interactions) and should only be done if you know what you're doing.

Parameters:

  • message (Optional[str]) –

    The message to respond with when an interaction command isn't found.

set_menu_ctx_maker #

set_menu_ctx_maker(maker=context.MenuContext)

Set the autocomplete context maker to use when creating contexts.

Warning

The caller must return an instance of tanjun.MenuContext rather than just any implementation of the MenuContext abc due to this client relying on implementation detail of tanjun.MenuContext.

Parameters:

Returns:

  • Self

    This client to enable method chaining.

set_menu_hooks #

set_menu_hooks(hooks)

Set the menu command execution hooks for this client.

The callbacks within this hook will be added to every menu command execution started by this client.

Parameters:

  • hooks (Optional[MenuHooks]) –

    The menu context specific command execution hooks to set for this client.

    Passing None will remove the hooks.

Returns:

  • Self

    The client instance to enable chained calls.

set_menu_not_found #

set_menu_not_found(message)

Set the response message for when a menu command is not found.

Warning

Setting this to None may lead to unexpected behaviour (especially when the client is still set to auto-defer interactions) and should only be done if you know what you're doing.

Parameters:

  • message (Optional[str]) –

    The message to respond with when a menu command isn't found.

set_message_accepts #

set_message_accepts(accepts)

Set the kind of messages commands should be executed based on.

Parameters:

  • accepts (MessageAcceptsEnum) –

    The type of messages commands should be executed based on.

Raises:

set_message_ctx_maker #

set_message_ctx_maker(maker=context.MessageContext)

Set the message context maker to use when creating context for a message.

Warning

The caller must return an instance of tanjun.MessageContext rather than just any implementation of the MessageContext abc due to this client relying on implementation detail of tanjun.MessageContext.

Parameters:

Returns:

  • Self

    This client to enable method chaining.

set_message_hooks #

set_message_hooks(hooks)

Set the message command execution hooks for this client.

The callbacks within this hook will be added to every message command execution started by this client.

Parameters:

  • hooks (Optional[MessageHooks]) –

    The message context specific command execution hooks to set for this client.

    Passing None will remove all hooks.

Returns:

  • Self

    The client instance to enable chained calls.

set_prefix_getter #

set_prefix_getter(getter)

Set the callback used to retrieve message prefixes set for the relevant guild.

Parameters:

  • getter (Optional[PrefixGetterSig]) –

    The callback which'll be used to retrieve prefixes for the guild a message context is from. If None is passed here then the callback will be unset.

    This should be an async callback which one argument of type tanjun.abc.MessageContext and returns an iterable of string prefixes. Dependency injection is supported for this callback's keyword arguments.

Returns:

  • Self

    The client instance to enable chained calls.

set_slash_ctx_maker #

set_slash_ctx_maker(maker=context.SlashContext)

Set the slash context maker to use when creating context for a slash command.

Warning

The caller must return an instance of tanjun.SlashContext rather than just any implementation of the SlashContext abc due to this client relying on implementation detail of tanjun.SlashContext.

Parameters:

Returns:

  • Self

    This client to enable method chaining.

set_slash_hooks #

set_slash_hooks(hooks)

Set the slash command execution hooks for this client.

The callbacks within this hook will be added to every slash command execution started by this client.

Parameters:

  • hooks (Optional[SlashHooks]) –

    The slash context specific command execution hooks to set for this client.

    Passing None will remove the hooks.

Returns:

  • Self

    The client instance to enable chained calls.

set_slash_not_found #

set_slash_not_found(message)

Set the response message for when a slash command is not found.

Warning

Setting this to None may lead to unexpected behaviour (especially when the client is still set to auto-defer interactions) and should only be done if you know what you're doing.

Parameters:

  • message (Optional[str]) –

    The message to respond with when a slash command isn't found.

with_check #

with_check(check)

Add a check to this client through a decorator call.

Parameters:

  • check (CheckSig) –

    The check to add. This may be either synchronous or asynchronous and must take one positional argument of type tanjun.abc.Context with dependency injection being supported for its keyword arguments.

Returns:

  • CheckSig

    The added check.

with_prefix_getter #

with_prefix_getter(getter)

Set the prefix getter callback for this client through decorator call.

Examples:

client = tanjun.Client.from_rest_bot(bot)

@client.with_prefix_getter
async def prefix_getter(ctx: tanjun.abc.MessageContext) -> collections.abc.Iterable[str]:
    raise NotImplementedError

Parameters:

  • getter (PrefixGetterSig) –

    The callback which'll be to retrieve prefixes for the guild a message event is from.

    This should be an async callback which one argument of type tanjun.abc.MessageContext and returns an iterable of string prefixes. Dependency injection is supported for this callback's keyword arguments.

Returns:

InteractionAcceptsEnum #

Bases: IntFlag

The possible configurations for which interaction this client should execute.

ALL class-attribute instance-attribute #

ALL = AUTOCOMPLETE | COMMANDS

Execute all the interaction types Tanjun supports.

AUTOCOMPLETE class-attribute instance-attribute #

AUTOCOMPLETE = auto()

Execute autocomplete interactions.

COMMANDS class-attribute instance-attribute #

COMMANDS = auto()

Execute command interactions.

This includes slash command and context menu calls.

NONE class-attribute instance-attribute #

NONE = 0

Set the client to execute no interactions.

MessageAcceptsEnum #

Bases: str, Enum

The possible configurations for which events Client should execute commands based on.

ALL class-attribute instance-attribute #

ALL = 'ALL'

Set the client to execute commands based on both DM and guild message create events.

DM_ONLY class-attribute instance-attribute #

DM_ONLY = 'DM_ONLY'

Set the client to execute commands based only DM message create events.

GUILD_ONLY class-attribute instance-attribute #

GUILD_ONLY = 'GUILD_ONLY'

Set the client to execute commands based only guild message create events.

NONE class-attribute instance-attribute #

NONE = 'NONE'

Set the client to not execute commands based on message create events.

get_event_type #

get_event_type()

Get the base event type this mode listens to.

Returns:

  • type[MessageCreateEvent] | None

    The type object of the MessageCreateEvent class this mode will register a listener for.

    This will be None if this mode disables listening to message create events.

as_loader #

as_loader(callback=None, /, *, standard_impl=True)

Mark a callback as being used to load Tanjun components from a module.

Note

This is only necessary if you wish to use Client.load_modules.

Parameters:

  • callback (Union[Callable[[Client], None], Callable[[Client], None], None], default: None ) –

    The callback used to load Tanjun components from a module.

    This should take one argument of type Client (or tanjun.abc.Client if standard_impl is False), return nothing and will be expected to initiate and add utilities such as components to the provided client.

  • standard_impl (bool, default: True ) –

    Whether this loader should only allow instances of Client as opposed to tanjun.abc.Client.

Returns:

  • collections.abc.Callable[[tanjun.abc.Client], None]]

    The decorated load callback.

as_unloader #

as_unloader(callback=None, /, *, standard_impl=True)

Mark a callback as being used to unload a module's utilities from a client.

Note

This is the inverse of as_loader and is only necessary if you wish to use the Client.unload_modules or Client.reload_modules.

Parameters:

  • callback (Union[Callable[[Client], None], Callable[[Client], None], None], default: None ) –

    The callback used to unload Tanjun components from a module.

    This should take one argument of type Client (or tanjun.abc.Client if standard_impl is False), return nothing and will be expected to remove utilities such as components from the provided client.

  • standard_impl (bool, default: True ) –

    Whether this unloader should only allow instances of Client as opposed to tanjun.abc.Client.

Returns:

  • collections.abc.Callable[[tanjun.abc.Client], None]]

    The decorated unload callback.

on_parser_error async #

on_parser_error(ctx, error)

Handle message parser errors.

This is the default message parser error hook included by Client.