hikari 的命令处理程序 tanjun 的签名解析器。
项目描述
谷地
终于可以在没有那些该死的装饰链的情况下定义你的命令了!
例子
@component.with_slash_command
@tanchi.as_slash_command(default_to_ephemeral=True)
async def command(
ctx: tanjun.abc.SlashContext,
integer: int = 0,
flag: bool = False,
channel: typing.Optional[hikari.GuildTextChannel] = None,
):
"""Small tanchi command
Parameters
----------
integer : int
Any integer value.
flag : bool
Whether this flag should be enabled.
channel : hikari.GuildTextChannel
The channel to target.
"""
文档?
普通类型
str斜杠命令 ( , int, float, )中支持的所有内置类型bool都不需要特别注意。
option: str
选择
可以使用typing.Literal或enum.Enum
option: typing.Literal["Foo", "Bar", "Baz"]
class MyEnum(enum.IntEnum):
foo = 1
bar = 2
baz = 3
option: MyEnum
范围
整数和浮点选项支持最小和最大边界。这些可以用 设置tanchi.Range。从边界辨别选项的类型。
int_option: tanchi.Range[1, 10]
float_option: tanchi.Range[0.0, 1.0]
频道
频道类型可以在typing.Union. 如果您希望所有频道类型使用hikari.GuildChannel
option: typing.Union[hikari.GuildTextChannel, hikari.GuildNewsChannel]
转换器
如果内置 tanjun 转换器可用,则类型会被隐式转换。
option: hikari.Emoji
要提供您自己的转换器,您可以使用tanchi.Converted.
option: tanchi.Converted[int, round]
自动完成
除了使用装饰器,自动完成器可以直接在注释中提供tanchi.Autocompleted。
option: tanchi.Autocompleted[autocomplete_callback]
由于转换器和自动补全经常一起使用,您可以直接提供转换器。
option: tanchi.Autocompleted[autocomplete_callback, converter_callback]
MyPy 兼容性
因为 mypy 不尊重__class_getitem__您很可能不得不在typing.Annotated某些情况下使用。
option: typing.Annotated[int, tanchi.Range(1, 10)]
option: typing.Annotated[int, tanchi.Converted(range)]
option: typing.Annotated[str, tanchi.Autocompleted(autocomplete_callback)]
文档字符串
Tanchi 从文档字符串中解析描述。
所有支持格式的示例:
休息
"""Command description on a single line
Parameters
----------
foo : OptionType
Description for the option named "foo"
bar:
Description for the option named "bar"
"""
谷歌
"""Command description on a single line
Args:
foo (OptionType): Description for the option named "foo"
bar: Description for the option named "bar"
"""
自动完成示例
context.set_choices您可以选择将选项作为序列或映射返回,而不是使用。
@tanchi.as_slash_command()
async def command(context: tanjun.abc.SlashContext, option: str) -> None:
...
@tanchi.with_autocomplete(command, "option")
def autocomplete_names(context: tanjun.abc.AutocompleteContext, option: str):
return [word for word in WORDS if option.lower() in word.lower()]
内部也支持返回选项Autocompleted