From e6e9453a024dff943ea52b4fb588f2cf7fec509b Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Mon, 26 Feb 2024 13:21:50 +0800 Subject: refactor(command): 命令词法解析器 (#82) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(command): 重构命令路由 * feat(lua): 包装异步方法`self.event.reply` => `msg:echo` * feat(lua): 包装异步输入流方法`self.event.ask` => `msg:ask` * 'Refactored by Sourcery' (#83) Co-authored-by: Sourcery AI <> * feat(Token|Lexer): 添加`Token`与`Lexer`类 * refactor(Lexer): 词法分析器添加`advance`方法 * chore: lint code * refactor: sync gensokyo adapter * feat: Cli parser (#85) * feat(cli): 添加`Cli`类,解析命令行参数 * fix: 修复错误的`dest`与`action` * feat(cli): 实现`install_package` 与 `build_template` * feat(cli): 实现`-c|--config`指令配置镜像常量等 * feat(cli): 使用高效率的异步网络库`aiohttp` * fix(cli): `TYPE_CHECKING` with partially module `typing` * refactor!: examples, tests, src... rewrite in rust --------- Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- examples/plugins/draftbottles/__init__.py | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/plugins/draftbottles/__init__.py (limited to 'examples/plugins/draftbottles/__init__.py') diff --git a/examples/plugins/draftbottles/__init__.py b/examples/plugins/draftbottles/__init__.py new file mode 100644 index 0000000..1a71f1d --- /dev/null +++ b/examples/plugins/draftbottles/__init__.py @@ -0,0 +1,48 @@ +from typing import Union +from iamai import Plugin, Event, Depends +from iamai.log import logger +from .config import Config +from iamai.event import MessageEvent +from .database import Database +from .permission import Permission +from .workroutes import WorkRoutes +from .inspector import Inspector + + +class Bottles(Plugin, config=Config): + database: Database = Depends() + permission: Permission = Depends() + workroutes: WorkRoutes = Depends() + inspector: Inspector = Depends() + + def __init__(self): + self.text = None + self.prefix = None + self.suffix = None + + async def handle(self) -> None: + self.namespace = next( + ( + key + for key, value in self.config.command_list.items() + if value == self.prefix + ), + "", + ) + if method := getattr(self.inspector, self.namespace, None): + result = await method(self.suffix, self.config) + if result: + await self.event.reply(result) + + async def rule(self) -> bool: + if not isinstance(self.event, MessageEvent): + return False + if not self.permission.is_admin(): + return False + self.text = self.event.get_plain_text() + for prefix in list(self.config.command_list.values()): + if self.text.startswith(prefix): + self.prefix = prefix + self.suffix = self.text[len(self.prefix) + 1 :] + return True + return False -- cgit v1.2.3-70-g09d2