Files

69 lines
2.4 KiB
Python

# Auto generated by LangBot Plugin SDK.
# Please refer to https://langbot.app/docs/en/plugin/dev/tutor.html for more details.
from __future__ import annotations
import json
from typing import Any, AsyncGenerator
from langbot_plugin.api.definition.components.command.command import Command, Subcommand
from langbot_plugin.api.entities.builtin.command.context import ExecuteContext, CommandReturn
from i18n import get_text
class Cmd(Command):
async def initialize(self):
await super().initialize()
@self.subcommand(
name="",
help="Show all registered command",
usage="cmd",
)
async def _(self: Cmd, context: ExecuteContext) -> AsyncGenerator[CommandReturn, None]:
assume_command_prefix = context.full_command_text[0]
language = self.plugin.get_language()
commands = await self.plugin.list_commands()
command_list_str = ''
for command in commands:
command_manifest = command['manifest']
command_list_str += f"{assume_command_prefix}{command_manifest['metadata']['name']} - {command_manifest['metadata']['description'][language]}\n"
title = get_text(language, "cmd.title")
tip = get_text(language, "cmd.tip", command_prefix=assume_command_prefix)
output = f"{title}\n\n{command_list_str}\n{tip}"
yield CommandReturn(
text=output
)
@self.subcommand(
name="man",
help="Show the manual of a command",
usage="cmd man <command>",
)
async def man(self: Cmd, context: ExecuteContext) -> AsyncGenerator[CommandReturn, None]:
command_name = context.crt_params[0]
language = self.plugin.get_language()
commands = await self.plugin.list_commands()
command_manual = get_text(language, "cmd.man_not_found", command_name=command_name)
for command in commands:
command_manifest = command['manifest']
if command_manifest['metadata']['name'] == command_name:
description = command_manifest['metadata']['description'][language]
command_manual = get_text(language, "cmd.man_title", command_name=command_name, description=description)
break
yield CommandReturn(
text=command_manual
)