2024-01-28 00:16:42 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import typing
|
|
|
|
|
import abc
|
|
|
|
|
|
|
|
|
|
from ..core import app, entities as core_entities
|
|
|
|
|
from . import entities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
preregistered_operators: list[typing.Type[CommandOperator]] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def operator_class(
|
|
|
|
|
name: str,
|
|
|
|
|
help: str,
|
2024-01-28 18:21:43 +08:00
|
|
|
|
usage: str = None,
|
|
|
|
|
alias: list[str] = [],
|
2024-01-28 00:16:42 +08:00
|
|
|
|
privilege: int=1, # 1为普通用户,2为管理员
|
|
|
|
|
parent_class: typing.Type[CommandOperator] = None
|
|
|
|
|
) -> typing.Callable[[typing.Type[CommandOperator]], typing.Type[CommandOperator]]:
|
|
|
|
|
def decorator(cls: typing.Type[CommandOperator]) -> typing.Type[CommandOperator]:
|
|
|
|
|
cls.name = name
|
|
|
|
|
cls.alias = alias
|
|
|
|
|
cls.help = help
|
2024-01-28 18:21:43 +08:00
|
|
|
|
cls.usage = usage
|
2024-01-28 00:16:42 +08:00
|
|
|
|
cls.parent_class = parent_class
|
2024-02-06 21:26:03 +08:00
|
|
|
|
cls.lowest_privilege = privilege
|
2024-01-28 00:16:42 +08:00
|
|
|
|
|
|
|
|
|
preregistered_operators.append(cls)
|
|
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandOperator(metaclass=abc.ABCMeta):
|
|
|
|
|
"""命令算子
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
ap: app.Application
|
|
|
|
|
|
|
|
|
|
name: str
|
|
|
|
|
"""名称,搜索到时若符合则使用"""
|
|
|
|
|
|
2024-02-06 23:57:21 +08:00
|
|
|
|
path: str
|
|
|
|
|
"""路径,所有父节点的name的连接,用于定义命令权限"""
|
|
|
|
|
|
2024-01-28 00:16:42 +08:00
|
|
|
|
alias: list[str]
|
|
|
|
|
"""同name"""
|
|
|
|
|
|
|
|
|
|
help: str
|
|
|
|
|
"""此节点的帮助信息"""
|
|
|
|
|
|
2024-01-28 18:21:43 +08:00
|
|
|
|
usage: str = None
|
|
|
|
|
|
2024-02-20 11:47:04 +08:00
|
|
|
|
parent_class: typing.Union[typing.Type[CommandOperator], None] = None
|
2024-01-28 00:16:42 +08:00
|
|
|
|
"""父节点类。标记以供管理器在初始化时编织父子关系。"""
|
|
|
|
|
|
|
|
|
|
lowest_privilege: int = 0
|
|
|
|
|
"""最低权限。若权限低于此值,则不予执行。"""
|
|
|
|
|
|
|
|
|
|
children: list[CommandOperator]
|
|
|
|
|
"""子节点。解析命令时,若节点有子节点,则以下一个参数去匹配子节点,
|
|
|
|
|
若有匹配中的,转移到子节点中执行,若没有匹配中的或没有子节点,执行此节点。"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, ap: app.Application):
|
|
|
|
|
self.ap = ap
|
|
|
|
|
self.children = []
|
|
|
|
|
|
|
|
|
|
async def initialize(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def execute(
|
|
|
|
|
self,
|
|
|
|
|
context: entities.ExecuteContext
|
|
|
|
|
) -> typing.AsyncGenerator[entities.CommandReturn, None]:
|
|
|
|
|
pass
|