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 +++++++++++ examples/plugins/draftbottles/config.py | 52 +++++++++++ examples/plugins/draftbottles/database.py | 11 +++ examples/plugins/draftbottles/inspector.py | 128 ++++++++++++++++++++++++++++ examples/plugins/draftbottles/permission.py | 10 +++ examples/plugins/draftbottles/workroutes.py | 9 ++ 6 files changed, 258 insertions(+) create mode 100644 examples/plugins/draftbottles/__init__.py create mode 100644 examples/plugins/draftbottles/config.py create mode 100644 examples/plugins/draftbottles/database.py create mode 100644 examples/plugins/draftbottles/inspector.py create mode 100644 examples/plugins/draftbottles/permission.py create mode 100644 examples/plugins/draftbottles/workroutes.py (limited to 'examples/plugins/draftbottles') 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 diff --git a/examples/plugins/draftbottles/config.py b/examples/plugins/draftbottles/config.py new file mode 100644 index 0000000..f5aaa72 --- /dev/null +++ b/examples/plugins/draftbottles/config.py @@ -0,0 +1,52 @@ +from iamai import ConfigModel + + +class Config(ConfigModel): + __config_name__ = "draft_bottles" + + usage: str = """\ + 指令: + 扔漂流瓶 [文本/图片] + 捡漂流瓶 + 查看漂流瓶 [漂流瓶编号] + 点赞漂流瓶 [漂流瓶编号] + 评论漂流瓶 [漂流瓶编号] [文本] + 举报漂流瓶 [漂流瓶编号] + 删除漂流瓶 [漂流瓶编号] + 我的漂流瓶 + SUPERUSER指令: + 清空漂流瓶 + 恢复漂流瓶 [漂流瓶编号] + 删除漂流瓶评论 [漂流瓶编号] [QQ号] + 漂流瓶白名单 [QQ / 群聊 / 举报] [QQ号 / 群号] + 漂流瓶黑名单 [QQ / 群聊] [QQ号 / 群号] + 漂流瓶详情 [漂流瓶编号] + """.strip() + + command_list: dict = { + "test": "/dfb", + "throw": "扔漂流瓶", + "get": "捡漂流瓶", + "report": "举报漂流瓶", + "comment": "评论漂流瓶", + "check": "查看漂流瓶", + "remove": "删除漂流瓶", + "listb": "我的漂流瓶", + "like": "点赞漂流瓶", + "resume": "恢复漂流瓶", + "clear": "清空漂流瓶", + "delete": "删除漂流瓶评论", + "details": "漂流瓶详情", + } + + ban_list: dict = { + "groups": [], + "users": [], + } + + white_list: dict = { + "groups": [], + "users": [], + } + + max_content_length: int = 1024 diff --git a/examples/plugins/draftbottles/database.py b/examples/plugins/draftbottles/database.py new file mode 100644 index 0000000..dededac --- /dev/null +++ b/examples/plugins/draftbottles/database.py @@ -0,0 +1,11 @@ +class Database: + admin_list: list = [2753364619] + + def __init__(self) -> None: + ... + + def connect(self): + ... + + def close(self): + ... diff --git a/examples/plugins/draftbottles/inspector.py b/examples/plugins/draftbottles/inspector.py new file mode 100644 index 0000000..de757b2 --- /dev/null +++ b/examples/plugins/draftbottles/inspector.py @@ -0,0 +1,128 @@ +from iamai import Event, Depends, Bot +from .database import Database +from .permission import Permission +from .workroutes import WorkRoutes +from .config import Config +from iamai.exceptions import GetEventTimeout +from iamai.adapter.onebot11.message import CQHTTPMessageSegment as ms +import oneroll + +class Inspector: + event: Event = Depends() + bot: Bot = Depends() + database: Database = Depends() + permission: Permission = Depends() + workroutes: WorkRoutes = Depends() + + async def test(self, *args): + suffix = list(args)[0] + a = "1" + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def throw(self, *args): + suffix = list(args)[0] + config = list(args)[1] + if len(suffix) == 0: + """没有内容,则进入输入流""" + try: + content_event = await self.event.ask( + "在漂流瓶中要写下什么呢?(输入“取消”来取消扔漂流瓶操作。)", timeout=10 + ) # type: ignore + except GetEventTimeout: + return "超时。" + except Exception as e: + return f"{e!r}" + else: + if content_event.message.get_plain_text().lower() in ["取消", "cancel"]: + return ms.reply(content_event.message_id) + ms.text("已取消扔漂流瓶操作。") + """有内容,进行审核""" + content = content_event.message.get_plain_text() + self._throw(content=content, event=content_event) + else: + """有内容,进行审核""" + self._throw(content=suffix, event=self.event) + + async def get(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def report(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def comment(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def check(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def remove(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def listb(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def like(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def resume(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def clear(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def delete(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + async def details(self, *args): + suffix = list(args)[0] + try: + return f"{eval(suffix)}" + except Exception as e: + return f"{e!r}" + + @staticmethod + def _throw(content: str, **kwargs): + """扔出漂流瓶""" + event = kwargs.pop('event', None) diff --git a/examples/plugins/draftbottles/permission.py b/examples/plugins/draftbottles/permission.py new file mode 100644 index 0000000..456dc8b --- /dev/null +++ b/examples/plugins/draftbottles/permission.py @@ -0,0 +1,10 @@ +from iamai import Event, Depends +from .database import Database + + +class Permission: + event: Event = Depends() + database: Database = Depends() + + def is_admin(self): + return self.event.user_id in self.database.admin_list diff --git a/examples/plugins/draftbottles/workroutes.py b/examples/plugins/draftbottles/workroutes.py new file mode 100644 index 0000000..f539780 --- /dev/null +++ b/examples/plugins/draftbottles/workroutes.py @@ -0,0 +1,9 @@ +from iamai import Event, Depends +from .permission import Permission +from .database import Database + + +class WorkRoutes: + event: Event = Depends() + database: Database = Depends() + permission: Permission = Depends() -- cgit v1.2.3-70-g09d2