Skip to content

Usage#

Doc parse#

as_slash_command#

tanchan.doc_parse exposes two methods which help with declaring slash commands:

import tanjun

from tanchan import doc_parse

# This command will show up as "meow" in the command menu
@doc_parse.as_slash_command()
async def meow(ctx: tanjun.abc.SlashContext) -> None:
    """Meow command's description."""

get_group = tanjun.slash_command_group("get", "Get command group")

# This command will show up as "get user" in the command menu
@get_group.with_command
@doc_parse.as_slash_command()
async def user(ctx: tanjun.abc.SlashContext) -> None:
    """Get a user."""

tanchan.doc_parse.as_slash_command acts as an extension to tanjun.as_slash_command which uses the function's name as the command's name and the first line of its docstring as the command's description.

with_annotated_args#
from typing import Annotated
from typing import Optional

import tanjun
from tanjun import annotations

from tanchan import doc_parse

# Google's doc style.
@doc_parse.with_annotated_args
@doc_parse.as_slash_command()
async def ban(
    ctx: tanjun.abc.SlashContext,
    user: annotations.User,
    reason: Optional[Annotated[annotations.Int, annotations.Length(460)]] = None,
) -> None:
    """Ban a user from this guild.

    Args:
        user: The user to ban from this guild.
        reason: The reason for the ban.
            If not provided then a generic reason will be used.
    """

# NumPy's doc style.
@doc_parse.with_annotated_args
@doc_parse.as_slash_command()
async def kick(
    ctx: tanjun.abc.SlashContext,
    member: annotations.Member,
    reason: Optional[Annotated[annotations.Int, annotations.Length(460)]] = None,
) -> None:
    """Kick a member from this guild.

    Parameters
    ----------
    member
        The guild member to kick.
    reason
        The reason for the kick.
        If not provided then a generic reason will be used.
    """

# Sphinx's "reST" doc style.
@doc_parse.with_annotated_args
@doc_parse.as_slash_command()
async def echo(
    ctx: tanjun.abc.SlashContext, content: annotations.Str, channel: Optional[annotations.Channel] = None
) -> None:
    """Make the bot echo a message.

    :param content: The message to echo.
    :param channel: The channel to echo to.
        If not provided then the current channel will be targeted.
    """

tanchan.doc_parse.with_annotated_args uses the functionality exposed in tanjun.annotations but with the added feature that slash command option descriptions are parsed from the docstring. This supports Google's doc style, NumPy's doc style, and Sphinx's "reST" doc style.

Commands#

Tan-chan provides several Tanjun commands which rely on the separate Yuyo components library. To ensure a compatible Yuyo version is present you should install Tan-chan with the tanchan[yuyo] install flag.

"tanchan.components" can be passed to Client.load_modules as a shorthand to add all of these commands and component handlers to a bot at once.

Note

Any command config should be added the Tanjun client before the commands are loaded.

Help command#

Tan-chan implements help commands which give users more information about the commands loaded in a bot.

The message command this introduces can be called as either {prefix}help to get a list of all available commands or as {prefix}help {command name} to get more information about a specific command. Commands are grouped by the name of their linked component by default so it's important to make sure you're passing legible name=s to tanjun.Component.__init__.

By default slash command functionality is turned off but this can be enabled using tanchan.components.config.HelpConfig.

These commands can be added to a bot by calling Client.load_modules with "tanchan.components.help".

Eval command#

Tan-chan implements eval commands which allow bot owners to dynamically evaluate code in the bot's runtime.

The message command this introduces can be called simply by sending {prefix}eval followed by a markdown codeblock of the code to execute.

The slash eval command isn't included by default but this can be set to be declared globally or for specific guilds using tanchan.components.config.EvalConfig and its relevant eval_guild_ids option.

These commands can be added to a bot by calling Client.load_modules with "tanchan.components.eval".