🎨 优化多连接适配

This commit is contained in:
Wuyi无疑 2023-03-26 19:19:47 +08:00
parent e1f057a09a
commit d8ac046014
2 changed files with 78 additions and 49 deletions

14
.vscode/settings.json vendored
View File

@ -11,9 +11,21 @@
"nonebug", "nonebug",
"weapontype" "weapontype"
], ],
"editor.formatOnSave": true,
"[python]": { "[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": { "editor.codeActionsOnSave": {
"source.organizeImports": true "source.organizeImports": true
} }
} },
"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",
} }

View File

@ -5,10 +5,10 @@ from pathlib import Path
from typing import Any, Dict, List, Union, Optional from typing import Any, Dict, List, Union, Optional
import websockets.client import websockets.client
from nonebot import get_bots
from nonebot.log import logger from nonebot.log import logger
from nonebot.adapters import Bot from nonebot.adapters import Bot
from msgspec import json as msgjson from msgspec import json as msgjson
from nonebot import get_bot, get_bots
from websockets.exceptions import ConnectionClosedError from websockets.exceptions import ConnectionClosedError
from .models import MessageSend, MessageReceive from .models import MessageSend, MessageReceive
@ -17,6 +17,19 @@ BOT_ID = 'NoneBot2'
bots: Dict[str, str] = {} 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: class GsClient:
@classmethod @classmethod
async def async_connect( async def async_connect(
@ -48,8 +61,11 @@ class GsClient:
f'【接收】[gsuid-core]: ' f'【接收】[gsuid-core]: '
f'{msg.bot_id} - {msg.target_type} - {msg.target_id}' f'{msg.bot_id} - {msg.target_type} - {msg.target_id}'
) )
bot_list = []
if msg.bot_self_id in _bots: 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: else:
continue continue
@ -80,51 +96,52 @@ class GsClient:
# 根据bot_id字段发送消息 # 根据bot_id字段发送消息
# OneBot v11 & v12 # OneBot v11 & v12
if msg.bot_id == 'onebot': for bot in bot_list:
await onebot_send( if msg.bot_id == 'onebot':
bot, await onebot_send(
content, bot,
image, content,
node, image,
file, node,
msg.target_id, file,
msg.target_type, msg.target_id,
) msg.target_type,
# ntchat )
elif msg.bot_id == 'ntchat': # ntchat
await ntchat_send( elif msg.bot_id == 'ntchat':
bot, content, image, file, node, msg.target_id await ntchat_send(
) bot, content, image, file, node, msg.target_id
# 频道 )
elif msg.bot_id == 'qqguild': # 频道
await guild_send( elif msg.bot_id == 'qqguild':
bot, await guild_send(
content, bot,
image, content,
node, image,
msg.target_id, node,
msg.target_type, msg.target_id,
msg.msg_id, msg.target_type,
) msg.msg_id,
elif msg.bot_id == 'telegram': )
await telegram_send( elif msg.bot_id == 'telegram':
bot, await telegram_send(
content, bot,
image, content,
file, image,
node, file,
msg.target_id, node,
) msg.target_id,
elif msg.bot_id == 'kaiheila': )
await kaiheila_send( elif msg.bot_id == 'kaiheila':
bot, await kaiheila_send(
content, bot,
image, content,
file, image,
node, file,
msg.target_id, node,
msg.target_type, msg.target_id,
) msg.target_type,
)
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
except RuntimeError: except RuntimeError: