diff --git a/GenshinUID/__init__.py b/GenshinUID/__init__.py index 0625cb41..07055ff2 100644 --- a/GenshinUID/__init__.py +++ b/GenshinUID/__init__.py @@ -8,11 +8,12 @@ from nonebot.internal.adapter import Event from nonebot import get_driver, on_message, on_fullmatch from .client import GsClient -from .auto_install import install +from .auto_install import start, install from .models import Message, MessageReceive get_message = on_message(priority=999) install_core = on_fullmatch('gs一键安装', permission=SUPERUSER, block=True) +start_core = on_fullmatch('启动core', permission=SUPERUSER, block=True) connect_core = on_fullmatch( ('连接core', '链接core'), permission=SUPERUSER, block=True ) @@ -135,11 +136,16 @@ async def send_install_msg(matcher: Matcher): @connect_core.handle() -async def send_start_msg(matcher: Matcher): +async def send_connect_msg(matcher: Matcher): await connect() await matcher.send('链接成功!') +@start_core.handle() +async def send_start_msg(matcher: Matcher): + await matcher.send(await start()) + + @driver.on_bot_connect async def start_client(): if gsclient is None: diff --git a/GenshinUID/client.py b/GenshinUID/client.py index f24d6d84..9ce37717 100644 --- a/GenshinUID/client.py +++ b/GenshinUID/client.py @@ -1,5 +1,7 @@ +import os import base64 import asyncio +from pathlib import Path from typing import Any, Dict, List, Union, Optional import websockets.client @@ -65,6 +67,7 @@ class GsClient: content = '' image: Optional[str] = None node = [] + file = '' if msg.content: for _c in msg.content: if _c.data: @@ -77,6 +80,8 @@ class GsClient: getattr(logger, _type)(_c.data) elif _c.type == 'node': node = _c.data + elif _c.type == 'file': + file = _c.data else: pass @@ -88,13 +93,14 @@ class GsClient: content, image, node, + file, msg.target_id, msg.target_type, ) # ntchat elif msg.bot_id == 'ntchat': await ntchat_send( - bot, content, image, node, msg.target_id + bot, content, image, file, node, msg.target_id ) # 频道 elif msg.bot_id == 'qqguild': @@ -112,6 +118,7 @@ class GsClient: bot, content, image, + file, node, msg.target_id, ) @@ -120,6 +127,7 @@ class GsClient: bot, content, image, + file, node, msg.target_id, msg.target_type, @@ -157,11 +165,23 @@ def to_json(msg: str, name: str, uin: int): } +def store_file(path: Path, file: str): + file_content = base64.b64decode(file).decode() + with open(path, 'w') as f: + f.write(file_content) + + +def del_file(path: Path): + if path.exists(): + os.remove(path) + + async def onebot_send( bot: Bot, content: Optional[str], image: Optional[str], node: Optional[List[Dict]], + file: Optional[str], target_id: Optional[str], target_type: Optional[str], ): @@ -169,18 +189,39 @@ async def onebot_send( result_image = f'[CQ:image,file={image}]' if image else '' content = content if content else '' result_msg = content + result_image - if target_type == 'group': - await bot.call_api( - 'send_group_msg', - group_id=target_id, - message=result_msg, - ) + + if file: + file_name, file_content = file.split('|') + path = Path(__file__).resolve().parent / file_name + store_file(path, file_content) + if target_type == 'group': + await bot.call_api( + 'upload_group_file', + file=str(path.absolute()), + name=file_name, + group_id=target_id, + ) + else: + await bot.call_api( + 'upload_private_file', + file=str(path.absolute()), + name=file_name, + user_id=target_id, + ) + del_file(path) else: - await bot.call_api( - 'send_private_msg', - user_id=target_id, - message=result_msg, - ) + if target_type == 'group': + await bot.call_api( + 'send_group_msg', + group_id=target_id, + message=result_msg, + ) + else: + await bot.call_api( + 'send_private_msg', + user_id=target_id, + message=result_msg, + ) async def _send_node(messages): if target_type == 'group': @@ -259,6 +300,7 @@ async def ntchat_send( bot: Bot, content: Optional[str], image: Optional[str], + file: Optional[str], node: Optional[List[Dict]], target_id: Optional[str], ): @@ -275,6 +317,16 @@ async def ntchat_send( to_wxid=target_id, file_path=image, ) + if file: + file_name, file_content = file.split('|') + path = Path(__file__).resolve().parent / file_name + store_file(path, file_content) + await bot.call_api( + 'send_file', + to_wxid=target_id, + file_path=str(path.absolute()), + ) + del_file(path) if node: for _msg in node: @@ -290,6 +342,7 @@ async def kaiheila_send( bot: Bot, content: Optional[str], image: Optional[str], + file: Optional[str], node: Optional[List[Dict]], target_id: Optional[str], target_type: Optional[str], @@ -302,6 +355,14 @@ async def kaiheila_send( url = await bot.upload_file(img_bytes, 'GSUID-TEMP') # type:ignore result['type'] = 2 result['content'] = url + elif file: + file_name, file_content = file.split('|') + path = Path(__file__).resolve().parent / file_name + store_file(path, file_content) + with open(path, 'rb') as f: + doc = f.read() + url = await bot.upload_file(doc, file_name) # type:ignore + result['content'] = url else: result['content'] = content @@ -327,6 +388,7 @@ async def telegram_send( bot: Bot, content: Optional[str], image: Optional[str], + file: Optional[str], node: Optional[List[Dict]], target_id: Optional[str], ): @@ -337,10 +399,20 @@ async def telegram_send( result['photo'] = img_bytes if content: result['text'] = content + if file: + file_name, file_content = file.split('|') + path = Path(__file__).resolve().parent / file_name + store_file(path, file_content) + with open(path, 'rb') as f: + doc = f.read() + result['document'] = doc + if content: await bot.call_api('send_message', chat_id=target_id, **result) if image: await bot.call_api('send_photo', chat_id=target_id, **result) + if file: + await bot.call_api('send_document', chat_id=target_id, **result) if node: for _msg in node: diff --git a/poetry.lock b/poetry.lock index 70327d20..f1e40547 100644 --- a/poetry.lock +++ b/poetry.lock @@ -89,19 +89,19 @@ files = [ [[package]] name = "filelock" -version = "3.9.0" +version = "3.10.0" description = "A platform independent file lock." category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "filelock-3.9.0-py3-none-any.whl", hash = "sha256:f58d535af89bb9ad5cd4df046f741f8553a418c01a7856bf0d173bbc9f6bd16d"}, - {file = "filelock-3.9.0.tar.gz", hash = "sha256:7b319f24340b51f55a2bf7a12ac0755a9b03e718311dac567a0f4f7fabd2f5de"}, + {file = "filelock-3.10.0-py3-none-any.whl", hash = "sha256:e90b34656470756edf8b19656785c5fea73afa1953f3e1b0d645cef11cab3182"}, + {file = "filelock-3.10.0.tar.gz", hash = "sha256:3199fd0d3faea8b911be52b663dfccceb84c95949dd13179aa21436d1a79c4ce"}, ] [package.extras] -docs = ["furo (>=2022.12.7)", "sphinx (>=5.3)", "sphinx-autodoc-typehints (>=1.19.5)"] -testing = ["covdefaults (>=2.2.2)", "coverage (>=7.0.1)", "pytest (>=7.2)", "pytest-cov (>=4)", "pytest-timeout (>=2.1)"] +docs = ["furo (>=2022.12.7)", "sphinx (>=6.1.3)", "sphinx-autodoc-typehints (>=1.22,!=1.23.4)"] +testing = ["covdefaults (>=2.3)", "coverage (>=7.2.1)", "pytest (>=7.2.2)", "pytest-cov (>=4)", "pytest-timeout (>=2.1)"] [[package]] name = "flake8" @@ -152,14 +152,14 @@ gitdb = ">=4.0.1,<5" [[package]] name = "identify" -version = "2.5.20" +version = "2.5.21" description = "File identification library for Python" category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "identify-2.5.20-py2.py3-none-any.whl", hash = "sha256:5dfef8a745ca4f2c95f27e9db74cb4c8b6d9916383988e8791f3595868f78a33"}, - {file = "identify-2.5.20.tar.gz", hash = "sha256:c8b288552bc5f05a08aff09af2f58e6976bf8ac87beb38498a0e3d98ba64eb18"}, + {file = "identify-2.5.21-py2.py3-none-any.whl", hash = "sha256:69edcaffa8e91ae0f77d397af60f148b6b45a8044b2cc6d99cafa5b04793ff00"}, + {file = "identify-2.5.21.tar.gz", hash = "sha256:7671a05ef9cfaf8ff63b15d45a91a1147a03aaccb2976d4e9bd047cbbc508471"}, ] [package.extras] @@ -875,14 +875,14 @@ typing-extensions = ">=3.7.4" [[package]] name = "virtualenv" -version = "20.20.0" +version = "20.21.0" description = "Virtual Python Environment builder" category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "virtualenv-20.20.0-py3-none-any.whl", hash = "sha256:3c22fa5a7c7aa106ced59934d2c20a2ecb7f49b4130b8bf444178a16b880fa45"}, - {file = "virtualenv-20.20.0.tar.gz", hash = "sha256:a8a4b8ca1e28f864b7514a253f98c1d62b64e31e77325ba279248c65fb4fcef4"}, + {file = "virtualenv-20.21.0-py3-none-any.whl", hash = "sha256:31712f8f2a17bd06234fa97fdf19609e789dd4e3e4bf108c3da71d710651adbc"}, + {file = "virtualenv-20.21.0.tar.gz", hash = "sha256:f50e3e60f990a0757c9b68333c9fdaa72d7188caa417f96af9e52407831a3b68"}, ] [package.dependencies]