From d8ac0460144b0d28140e54f688d1a94b7a240793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wuyi=E6=97=A0=E7=96=91?= <444835641@qq.com> Date: Sun, 26 Mar 2023 19:19:47 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=E4=BC=98=E5=8C=96=E5=A4=9A?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 16 +++++- GenshinUID/client.py | 111 ++++++++++++++++++++++++------------------ 2 files changed, 78 insertions(+), 49 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 0b337cbe..4ebaf7b0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,9 +11,21 @@ "nonebug", "weapontype" ], + "editor.formatOnSave": true, "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter", + "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true } - } -} \ No newline at end of file + }, + "isort.args": [ + "--profile", + "black" + ], + "python.formatting.provider": "black", + "python.linting.flake8Enabled": true, + "python.linting.flake8CategorySeverity.W": "Warning", + "python.linting.flake8CategorySeverity.F": "Warning", + "python.linting.flake8CategorySeverity.E": "Warning", +} diff --git a/GenshinUID/client.py b/GenshinUID/client.py index 0b775ed0..ef3f3fbc 100644 --- a/GenshinUID/client.py +++ b/GenshinUID/client.py @@ -5,10 +5,10 @@ from pathlib import Path from typing import Any, Dict, List, Union, Optional import websockets.client -from nonebot import get_bots from nonebot.log import logger from nonebot.adapters import Bot from msgspec import json as msgjson +from nonebot import get_bot, get_bots from websockets.exceptions import ConnectionClosedError from .models import MessageSend, MessageReceive @@ -17,6 +17,19 @@ BOT_ID = 'NoneBot2' bots: Dict[str, str] = {} +def _get_bot(bot_id: str) -> Bot: + if 'onebot' in bot_id: + bot_id = 'onebot' + if bot_id not in bots: + for _bot_id in bots.keys(): + if bot_id in _bot_id: + bot_id = _bot_id + break + bot_real_id = bots[bot_id] + bot = get_bot(bot_real_id) + return bot + + class GsClient: @classmethod async def async_connect( @@ -48,8 +61,11 @@ class GsClient: f'【接收】[gsuid-core]: ' f'{msg.bot_id} - {msg.target_type} - {msg.target_id}' ) + bot_list = [] if msg.bot_self_id in _bots: - bot = _bots[msg.bot_self_id] + bot_list.append(_bots[msg.bot_self_id]) + elif not msg.bot_self_id: + bot_list.append(_get_bot(msg.bot_id)) else: continue @@ -80,51 +96,52 @@ class GsClient: # 根据bot_id字段发送消息 # OneBot v11 & v12 - if msg.bot_id == 'onebot': - await onebot_send( - bot, - content, - image, - node, - file, - msg.target_id, - msg.target_type, - ) - # ntchat - elif msg.bot_id == 'ntchat': - await ntchat_send( - bot, content, image, file, node, msg.target_id - ) - # 频道 - elif msg.bot_id == 'qqguild': - await guild_send( - bot, - content, - image, - node, - msg.target_id, - msg.target_type, - msg.msg_id, - ) - elif msg.bot_id == 'telegram': - await telegram_send( - bot, - content, - image, - file, - node, - msg.target_id, - ) - elif msg.bot_id == 'kaiheila': - await kaiheila_send( - bot, - content, - image, - file, - node, - msg.target_id, - msg.target_type, - ) + for bot in bot_list: + if msg.bot_id == 'onebot': + await onebot_send( + bot, + content, + image, + node, + file, + msg.target_id, + msg.target_type, + ) + # ntchat + elif msg.bot_id == 'ntchat': + await ntchat_send( + bot, content, image, file, node, msg.target_id + ) + # 频道 + elif msg.bot_id == 'qqguild': + await guild_send( + bot, + content, + image, + node, + msg.target_id, + msg.target_type, + msg.msg_id, + ) + elif msg.bot_id == 'telegram': + await telegram_send( + bot, + content, + image, + file, + node, + msg.target_id, + ) + elif msg.bot_id == 'kaiheila': + await kaiheila_send( + bot, + content, + image, + file, + node, + msg.target_id, + msg.target_type, + ) except Exception as e: logger.error(e) except RuntimeError: