From 89b0972cd28866d72e7636e3766ab7bb6f158a87 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Fri, 16 Apr 2021 14:10:33 +0800 Subject: =?UTF-8?q?=F0=9F=8E=A8=E2=9A=A1=EF=B8=8F=E2=99=BB=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增:休眠操作(期间不回应任何信息) 更改:近乎所有命令格式 重构:重构帮助部分代码 --- ATRI/plugins/admin.py | 84 +++++++++++++++++++------------ ATRI/plugins/anime_search.py | 6 +-- ATRI/plugins/call_owner.py | 2 +- ATRI/plugins/code-runner.py | 110 ----------------------------------------- ATRI/plugins/code_runner.py | 110 +++++++++++++++++++++++++++++++++++++++++ ATRI/plugins/essential.py | 6 ++- ATRI/plugins/funny.py | 37 ++++++-------- ATRI/plugins/help.py | 18 +++---- ATRI/plugins/hitokoto.py | 7 ++- ATRI/plugins/nsfw.py | 2 +- ATRI/plugins/status.py | 7 ++- ATRI/plugins/utils/__init__.py | 4 +- ATRI/utils/translate.py | 2 +- 13 files changed, 209 insertions(+), 186 deletions(-) delete mode 100644 ATRI/plugins/code-runner.py create mode 100644 ATRI/plugins/code_runner.py (limited to 'ATRI') diff --git a/ATRI/plugins/admin.py b/ATRI/plugins/admin.py index 3022723..85ed805 100644 --- a/ATRI/plugins/admin.py +++ b/ATRI/plugins/admin.py @@ -4,6 +4,7 @@ from random import randint from pathlib import Path from nonebot.permission import SUPERUSER +from nonebot.adapters.cqhttp.permission import GROUP_ADMIN, GROUP_OWNER from nonebot.adapters.cqhttp import ( Bot, MessageEvent, @@ -25,15 +26,14 @@ __doc__ = """ 好友申请处理 权限组:维护者 用法: - /fr list - /fr (y/n) reqid + /friendreq list + /friendreq (y/n) reqid 补充: reqid: 申请码 """ request_friend = sv.on_command( - cmd="friendreq", - aliases={'fr'}, + cmd="/friendreq", docs=__doc__, permission=SUPERUSER ) @@ -76,15 +76,14 @@ __doc__ = """ 群聊申请处理 权限组:维护者 用法: - /gr list - /gr (y/n) reqid + /groupreq list + /groupreq (y/n) reqid 补充: reqid: 申请码 """ request_group = sv.on_command( - cmd="groupreq", - aliases={'gr'}, + cmd="/groupreq", docs=__doc__, permission=SUPERUSER ) @@ -141,8 +140,7 @@ __doc__ = """ """ broadcast = sv.on_command( - cmd="boradcast", - aliases={'bc'}, + cmd="/bc", docs=__doc__, permission=SUPERUSER ) @@ -191,7 +189,7 @@ __doc__ = """ """ track_error = sv.on_command( - cmd="track", + cmd="/track", docs=__doc__, permission=SUPERUSER ) @@ -226,15 +224,14 @@ __doc__ = """ 获取控制台信息 权限组:维护者 用法: - /gl level line + /getlog level line 补充: level: 等级(info, warning, error, debug) line: 行数(最近20行:-20) """ get_log = sv.on_command( - cmd="getlog", - aliases={'gl'}, + cmd="/getlog", docs=__doc__, permission=SUPERUSER ) @@ -281,12 +278,11 @@ __doc__ = """ 紧急停机 权限组:维护者 用法: - /st + /down """ shutdown = sv.on_command( - cmd="shutdown", - aliases={'st'}, + cmd="/down", docs=__doc__, permission=SUPERUSER ) @@ -310,21 +306,21 @@ __doc__ = """ 懒得和你废话,block了 权限组:维护者 用法: - /b u+uid,g+gid 0,1 + /block (u,g) (int) (0,1) 补充: - uid:QQ号 - gid:QQ群号 + u:QQ + g:QQ群 + int: 对应号码 0,1:对应布尔值False, True 范围为全局 示例: - /b u114514 1 + /block u 114514 1 执行对QQ号为114514的封禁 """ block = sv.on_command( - cmd="block", - aliases={'b'}, - docs="懒得和你废话,block了\n用法:/b u,g 0,1", + cmd="/block", + docs=__doc__, permission=SUPERUSER ) @@ -355,9 +351,9 @@ __doc__ = """ 权限组:维护者,群管理 用法: 对于维护者: - /s 目标指令 u+uid,g+gid,global 0,1 + /service 目标指令 u+uid,g+gid,global 0,1 对于群管理: - /s 目标指令 0,1 + /service 目标指令 0,1 补充: user:QQ号 group:QQ群号 @@ -365,16 +361,15 @@ __doc__ = """ 0,1:对应布尔值False, True 示例: 对于维护者: - /s /status u123456789 1 + /service /status u123456789 1 对于群管理: - /s /status 1 + /service /status 1 """ service_control = sv.on_command( - cmd='service', - aliases={'s'}, + cmd='/service', docs=__doc__, - permission=SUPERUSER + permission=SUPERUSER|GROUP_OWNER|GROUP_ADMIN ) @service_control.handle() @@ -415,3 +410,30 @@ async def _service_control(bot: Bot, event: GroupMessageEvent) -> None: @service_control.handle() async def _serv(bot: Bot, event: PrivateMessageEvent) -> None: await service_control.finish("此功能仅在群聊中触发") + + +__doc__ = """ +休眠bot,不处理任何信息 +权限组:维护者 +用法: + /dormant (0,1) +补充: + 0,1: 对应布尔值(False,True) +""" + +dormant = sv.on_command( + cmd='/dormant', + docs=__doc__, + permission=SUPERUSER +) + +@dormant.handle() +async def _dormant(bot: Bot, event: MessageEvent) -> None: + msg = str(event.message).strip() + if msg == "1": + sv.Dormant.control_dormant(True) + stat = "已进入休眠状态...期间咱不会回应任何人的消息哦..." + else: + sv.Dormant.control_dormant(False) + stat = "唔...回复精神力!" + await dormant.finish(stat) diff --git a/ATRI/plugins/anime_search.py b/ATRI/plugins/anime_search.py index b6686c5..e0c7c85 100644 --- a/ATRI/plugins/anime_search.py +++ b/ATRI/plugins/anime_search.py @@ -20,13 +20,13 @@ __doc__ = """ 以图搜番 权限组:所有人 用法: - /anime + 以图搜番 (pic) """ anime_search = sv.on_command( - cmd="anime", + cmd="以图搜番", docs=__doc__, - rule=is_in_service('anime') + rule=is_in_service('以图搜番') ) @anime_search.args_parser # type: ignore diff --git a/ATRI/plugins/call_owner.py b/ATRI/plugins/call_owner.py index 93cbcf8..cd79d1c 100644 --- a/ATRI/plugins/call_owner.py +++ b/ATRI/plugins/call_owner.py @@ -16,7 +16,7 @@ __doc__ = """ 给维护者留言 权限组:所有人 用法: - /来杯红茶 (msg) + 来杯红茶 (msg) """ repo = sv.on_command(cmd="来杯红茶", docs=__doc__) diff --git a/ATRI/plugins/code-runner.py b/ATRI/plugins/code-runner.py deleted file mode 100644 index 367a144..0000000 --- a/ATRI/plugins/code-runner.py +++ /dev/null @@ -1,110 +0,0 @@ -""" -Idea from: https://github.com/cczu-osa/aki -""" -import json -from nonebot.adapters.cqhttp import Bot, MessageEvent - -from ATRI.rule import is_in_service -from ATRI.service import Service as sv -from ATRI.utils.request import post_bytes -from ATRI.exceptions import RequestTimeOut - - -RUN_API_URL_FORMAT = 'https://glot.io/run/{}?version=latest' -SUPPORTED_LANGUAGES = { - 'assembly': {'ext': 'asm'}, - 'bash': {'ext': 'sh'}, - 'c': {'ext': 'c'}, - 'clojure': {'ext': 'clj'}, - 'coffeescript': {'ext': 'coffe'}, - 'cpp': {'ext': 'cpp'}, - 'csharp': {'ext': 'cs'}, - 'erlang': {'ext': 'erl'}, - 'fsharp': {'ext': 'fs'}, - 'go': {'ext': 'go'}, - 'groovy': {'ext': 'groovy'}, - 'haskell': {'ext': 'hs'}, - 'java': {'ext': 'java', 'name': 'Main'}, - 'javascript': {'ext': 'js'}, - 'julia': {'ext': 'jl'}, - 'kotlin': {'ext': 'kt'}, - 'lua': {'ext': 'lua'}, - 'perl': {'ext': 'pl'}, - 'php': {'ext': 'php'}, - 'python': {'ext': 'py'}, - 'ruby': {'ext': 'rb'}, - 'rust': {'ext': 'rs'}, - 'scala': {'ext': 'scala'}, - 'swift': {'ext': 'swift'}, - 'typescript': {'ext': 'ts'}, -} - - -__doc__ = """ -在线运行代码 -权限组:所有人 -用法: - /code (lang) (code) -示例: - /code python - print('Hello world!') -""" - -code_runner = sv.on_command( - cmd="code", - docs=__doc__, - rule=is_in_service('code') -) - -@code_runner.handle() -async def _code_runner(bot: Bot, event: MessageEvent) -> None: - msg = str(event.message).split("\n") - - if msg[0] == "list": - msg0 = "咱现在支持的语言如下:\n" - msg0 += ", ".join(map(str, SUPPORTED_LANGUAGES.keys())) - - await code_runner.finish(msg0) - elif not msg[0]: - await code_runner.finish("请键入/help以获取更多支持...") - - laug = msg[0].replace("\r", "") - if laug not in SUPPORTED_LANGUAGES: - await code_runner.finish("该语言暂不支持...") - - del msg[0] - code = "\n".join(map(str, msg)) - try: - req = await post_bytes( - RUN_API_URL_FORMAT.format(laug), - json={ - "files": [{ - "name": (SUPPORTED_LANGUAGES[laug].get("name", "main") + - f".{SUPPORTED_LANGUAGES[laug]['ext']}"), - "content": code - }], - "stdin": "", - "command": "" - } - ) - except RequestTimeOut: - raise RequestTimeOut("Failed to request!") - - payload = json.loads(req) - sent = False - for k in ['stdout', 'stderr', 'error']: - v = payload.get(k) - lines = v.splitlines() - lines, remained_lines = lines[:10], lines[10:] - out = '\n'.join(lines) - out, remained_out = out[:60 * 10], out[60 * 10:] - - if remained_lines or remained_out: - out += f"\n(太多了太多了...)" - - if out: - await bot.send(event, f"{k}:\n\n{out}") - sent = True - - if not sent: - await code_runner.finish("Running success! Nothing print.") diff --git a/ATRI/plugins/code_runner.py b/ATRI/plugins/code_runner.py new file mode 100644 index 0000000..b8f1106 --- /dev/null +++ b/ATRI/plugins/code_runner.py @@ -0,0 +1,110 @@ +""" +Idea from: https://github.com/cczu-osa/aki +""" +import json +from nonebot.adapters.cqhttp import Bot, MessageEvent + +from ATRI.rule import is_in_service +from ATRI.service import Service as sv +from ATRI.utils.request import post_bytes +from ATRI.exceptions import RequestTimeOut + + +RUN_API_URL_FORMAT = 'https://glot.io/run/{}?version=latest' +SUPPORTED_LANGUAGES = { + 'assembly': {'ext': 'asm'}, + 'bash': {'ext': 'sh'}, + 'c': {'ext': 'c'}, + 'clojure': {'ext': 'clj'}, + 'coffeescript': {'ext': 'coffe'}, + 'cpp': {'ext': 'cpp'}, + 'csharp': {'ext': 'cs'}, + 'erlang': {'ext': 'erl'}, + 'fsharp': {'ext': 'fs'}, + 'go': {'ext': 'go'}, + 'groovy': {'ext': 'groovy'}, + 'haskell': {'ext': 'hs'}, + 'java': {'ext': 'java', 'name': 'Main'}, + 'javascript': {'ext': 'js'}, + 'julia': {'ext': 'jl'}, + 'kotlin': {'ext': 'kt'}, + 'lua': {'ext': 'lua'}, + 'perl': {'ext': 'pl'}, + 'php': {'ext': 'php'}, + 'python': {'ext': 'py'}, + 'ruby': {'ext': 'rb'}, + 'rust': {'ext': 'rs'}, + 'scala': {'ext': 'scala'}, + 'swift': {'ext': 'swift'}, + 'typescript': {'ext': 'ts'}, +} + + +__doc__ = """ +在线运行代码 +权限组:所有人 +用法: + /code (lang) (code) +示例: + /code python + print('Hello world!') +""" + +code_runner = sv.on_command( + cmd="/code", + docs=__doc__, + rule=is_in_service('code') +) + +@code_runner.handle() +async def _code_runner(bot: Bot, event: MessageEvent) -> None: + msg = str(event.message).split("\n") + + if msg[0] == "list": + msg0 = "咱现在支持的语言如下:\n" + msg0 += ", ".join(map(str, SUPPORTED_LANGUAGES.keys())) + + await code_runner.finish(msg0) + elif not msg[0]: + await code_runner.finish("请键入/help以获取更多支持...") + + laug = msg[0].replace("\r", "") + if laug not in SUPPORTED_LANGUAGES: + await code_runner.finish("该语言暂不支持...") + + del msg[0] + code = "\n".join(map(str, msg)) + try: + req = await post_bytes( + RUN_API_URL_FORMAT.format(laug), + json={ + "files": [{ + "name": (SUPPORTED_LANGUAGES[laug].get("name", "main") + + f".{SUPPORTED_LANGUAGES[laug]['ext']}"), + "content": code + }], + "stdin": "", + "command": "" + } + ) + except RequestTimeOut: + raise RequestTimeOut("Failed to request!") + + payload = json.loads(req) + sent = False + for k in ['stdout', 'stderr', 'error']: + v = payload.get(k) + lines = v.splitlines() + lines, remained_lines = lines[:10], lines[10:] + out = '\n'.join(lines) + out, remained_out = out[:60 * 10], out[60 * 10:] + + if remained_lines or remained_out: + out += f"\n(太多了太多了...)" + + if out: + await bot.send(event, f"{k}:\n\n{out}") + sent = True + + if not sent: + await code_runner.finish("Running success! Nothing print.") diff --git a/ATRI/plugins/essential.py b/ATRI/plugins/essential.py index 833ce01..686d87b 100644 --- a/ATRI/plugins/essential.py +++ b/ATRI/plugins/essential.py @@ -93,11 +93,13 @@ async def _check_block(matcher: Matcher, event: MessageEvent, state: T_State) -> None: user = str(event.user_id) + msg = str(event.message) if not sv.BlockSystem.auth_user(user): raise IgnoredException(f'Block user: {user}') if not sv.Dormant.is_dormant(): - raise IgnoredException('Bot has been dormant.') + if "/dormant" not in msg: + raise IgnoredException('Bot has been dormant.') if isinstance(event, GroupMessageEvent): group = str(event.group_id) @@ -266,7 +268,7 @@ async def _gro(bot: Bot, event: GroupDecreaseNoticeEvent) -> None: message=msg ) else: - await group_member_event.finish(f"{event.user_id} 离开了我们...") + await group_member_event.finish(f"阿!有人离开了我们...") # 处理群管理事件 diff --git a/ATRI/plugins/funny.py b/ATRI/plugins/funny.py index d630092..8aea8d1 100644 --- a/ATRI/plugins/funny.py +++ b/ATRI/plugins/funny.py @@ -22,18 +22,17 @@ __doc__ = """ 来句笑话 """ -get_laugh = sv.on_message(rule=is_in_service('来句笑话')) -sv.manual_reg_service('来句笑话', __doc__) +get_laugh = sv.on_command( + cmd='来句笑话', + docs=__doc__, + rule=is_in_service('来句笑话') +) @get_laugh.handle() async def _get_laugh(bot: Bot, event: MessageEvent) -> None: user_name = event.sender.nickname - msg = str(event.message) laugh_list = [] - if msg != "来句笑话": - return - FILE = Path('.') / 'ATRI' / 'data' / 'database' / 'funny' / 'laugh.txt' with open(FILE, 'r', encoding='utf-8') as r: for line in r: @@ -43,15 +42,7 @@ async def _get_laugh(bot: Bot, event: MessageEvent) -> None: await get_laugh.finish(result.replace("%name", user_name)) -__doc__ = """ -你又行了 -权限组:所有人 -用法: - (被动触发) -""" - -me_to_you = sv.on_message(rule=is_in_service('你又行了')) -sv.manual_reg_service('你又行了', __doc__) +me_to_you = sv.on_message() @me_to_you.handle() async def _me_to_you(bot: Bot, event: MessageEvent) -> None: @@ -68,8 +59,11 @@ __doc__ = """ 抽老婆 """ -roll_wife = sv.on_message(rule=is_in_service('抽老婆')) -sv.manual_reg_service('抽老婆', __doc__) +roll_wife = sv.on_command( + cmd='抽老婆', + docs=__doc__, + rule=is_in_service('抽老婆') +) @roll_wife.handle() async def _roll_wife(bot: Bot, event: GroupMessageEvent) -> None: @@ -108,18 +102,17 @@ __doc__ = """ 伪造转发 权限组:所有人 用法: - /fm qq-name-msg... + /fakemsg qq*name*msg... 补充: qq: QQ号 name: 消息中的ID msg: 对应信息 示例: - /fm 123456789*生草人*草 114514*仙贝*臭死了 + /fakemsg 123456789*生草人*草 114514*仙贝*臭死了 """ fake_msg = sv.on_command( - cmd="fakemsg", - aliases={'fm'}, + cmd="/fakemsg", docs=__doc__, rule=is_in_service('fakemsg') ) @@ -206,4 +199,4 @@ async def _eat(bot: Bot, event: MessageEvent) -> None: get_a = re.match(r"(.*?)的智商", text)[0] result = f"> {MessageSegment.at(user)}\n" + text.replace(get_a, f'{user_n}的智商') - await eat_wat.finish(Message(result)) \ No newline at end of file + await eat_wat.finish(Message(result)) diff --git a/ATRI/plugins/help.py b/ATRI/plugins/help.py index fd51024..7d01d53 100644 --- a/ATRI/plugins/help.py +++ b/ATRI/plugins/help.py @@ -14,14 +14,13 @@ __doc__ = """ 查询命令用法 权限组:所有人 用法: - /h - /h list - /h info (cmd) + /help + /help list + /help info (cmd) """ help = sv.on_command( - cmd="help", - aliases={'h', '?', '?'}, + cmd="/help", docs=__doc__, ) @@ -31,8 +30,8 @@ async def _help(bot: Bot, event: MessageEvent) -> None: if msg[0] == "": msg = ( "呀?找不到路了?\n" - "/h list 查看可用命令列表\n" - "/h info (cmd) 查看命令具体帮助\n" + "/help list 查看可用命令列表\n" + "/help info (cmd) 查看命令具体帮助\n" "项目地址:github.com/Kyomotoi/ATRI\n" "咱只能帮你这么多了qwq" ) @@ -41,10 +40,11 @@ async def _help(bot: Bot, event: MessageEvent) -> None: files = [] for _, _, i in os.walk(SERVICE_DIR): for a in i: - files.append(a.replace('.json', '')) + f = SERVICE_DIR / a + files.append(json.loads(f.read_bytes())['command']) cmds = " | ".join(map(str, files)) msg = "咱能做很多事!比如:\n" + cmds - msg0 = msg + "\n具体用法呢,/(cmd) 就好!\n没反应可能是没权限..." + msg0 = msg + "\n没反应可能是没权限...或者为探测类型...不属于可直接触发命令..." await help.finish(msg0) elif msg[0] == "info": cmd = msg[1] diff --git a/ATRI/plugins/hitokoto.py b/ATRI/plugins/hitokoto.py index eeddf2f..83469be 100644 --- a/ATRI/plugins/hitokoto.py +++ b/ATRI/plugins/hitokoto.py @@ -25,8 +25,11 @@ __doc__ = """ @:at Bot """ -hitokoto = sv.on_message(rule=is_in_service('一言') & to_bot()) -sv.manual_reg_service('一言', __doc__) +hitokoto = sv.on_command( + cmd='一言', + docs=__doc__, + rule=is_in_service('一言') +) @hitokoto.handle() async def _hitokoto(bot: Bot, event: MessageEvent) -> None: diff --git a/ATRI/plugins/nsfw.py b/ATRI/plugins/nsfw.py index ea9e3bd..fe5bf78 100644 --- a/ATRI/plugins/nsfw.py +++ b/ATRI/plugins/nsfw.py @@ -61,7 +61,7 @@ __doc__ = """ """ nsfw_reading = sv.on_command( - cmd="nsfw", + cmd="/nsfw", docs=__doc__, rule=is_in_service('nsfw') ) diff --git a/ATRI/plugins/status.py b/ATRI/plugins/status.py index 61e96fe..86ec3df 100644 --- a/ATRI/plugins/status.py +++ b/ATRI/plugins/status.py @@ -11,10 +11,13 @@ from ATRI.config import Config __doc__ = """ 测试机器人状态 +权限组:所有人 +用法: + /ping """ ping = sv.on_command( - cmd="ping", + cmd="/ping", docs="测试机器人", rule=is_in_service('ping')) @@ -31,7 +34,7 @@ __doc__ = """ """ status = sv.on_command( - cmd="status", + cmd="/status", docs=__doc__, rule=is_in_service('status') ) diff --git a/ATRI/plugins/utils/__init__.py b/ATRI/plugins/utils/__init__.py index af6d4e0..5d1860a 100644 --- a/ATRI/plugins/utils/__init__.py +++ b/ATRI/plugins/utils/__init__.py @@ -18,7 +18,7 @@ roll一下 """ roll = sv.on_command( - cmd="roll", + cmd="/roll", docs=__doc__, rule=is_in_service('roll') ) @@ -53,7 +53,7 @@ __doc__ = """ """ encrypt = sv.on_command( - cmd="enc", + cmd="/enc", docs=__doc__, rule=is_in_service('enc') ) diff --git a/ATRI/utils/translate.py b/ATRI/utils/translate.py index d1fb83c..b183a9d 100644 --- a/ATRI/utils/translate.py +++ b/ATRI/utils/translate.py @@ -29,4 +29,4 @@ def to_simple_string(s: str): else: output_str_list.append(s[i]) - return "".join(output_str_list) \ No newline at end of file + return "".join(output_str_list) -- cgit v1.2.3