mirror of
https://github.com/KimigaiiWuyi/GenshinUID.git
synced 2025-05-12 06:55:58 +08:00
✨ 新增node
字段消息发送方法
This commit is contained in:
parent
0ffaa950c2
commit
f0ef919fb2
@ -1,13 +1,14 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from typing import Union, Optional
|
from typing import List, Union, Optional
|
||||||
|
|
||||||
import websockets.client
|
import websockets.client
|
||||||
from nonebot import get_bot
|
from nonebot import get_bot
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
|
from nonebot.adapters import Bot
|
||||||
from msgspec import json as msgjson
|
from msgspec import json as msgjson
|
||||||
from websockets.exceptions import ConnectionClosedError
|
from websockets.exceptions import ConnectionClosedError
|
||||||
|
|
||||||
from .models import MessageSend, MessageReceive
|
from .models import Message, MessageSend, MessageReceive
|
||||||
|
|
||||||
BOT_ID = 'NoneBot2'
|
BOT_ID = 'NoneBot2'
|
||||||
|
|
||||||
@ -34,6 +35,7 @@ class GsClient:
|
|||||||
# 解析消息
|
# 解析消息
|
||||||
content = ''
|
content = ''
|
||||||
image: Optional[bytes] = None
|
image: Optional[bytes] = None
|
||||||
|
node = []
|
||||||
if msg.content:
|
if msg.content:
|
||||||
for _c in msg.content:
|
for _c in msg.content:
|
||||||
if _c.data:
|
if _c.data:
|
||||||
@ -44,54 +46,35 @@ class GsClient:
|
|||||||
elif _c.type and _c.type.startswith('log'):
|
elif _c.type and _c.type.startswith('log'):
|
||||||
_type = _c.type.split('_')[-1].lower()
|
_type = _c.type.split('_')[-1].lower()
|
||||||
getattr(logger, _type)(_c.data)
|
getattr(logger, _type)(_c.data)
|
||||||
|
elif _c.type == 'node':
|
||||||
|
node = _c.data
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# 根据bot_id字段发送消息
|
# 根据bot_id字段发送消息
|
||||||
|
# OneBot v11 & v12
|
||||||
if msg.bot_id == 'onebot':
|
if msg.bot_id == 'onebot':
|
||||||
result_image = (
|
await onebot_send(
|
||||||
f'[CQ:image,file=base64://{image}]' if image else ''
|
bot,
|
||||||
|
content,
|
||||||
|
image,
|
||||||
|
node,
|
||||||
|
msg.target_id,
|
||||||
|
msg.target_type,
|
||||||
)
|
)
|
||||||
result_msg = content + result_image
|
# ntchat
|
||||||
if msg.target_type == 'group':
|
|
||||||
await bot.call_api(
|
|
||||||
'send_group_msg',
|
|
||||||
group_id=msg.target_id,
|
|
||||||
message=result_msg,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
await bot.call_api(
|
|
||||||
'send_private_msg',
|
|
||||||
user_id=msg.target_id,
|
|
||||||
message=result_msg,
|
|
||||||
)
|
|
||||||
elif msg.bot_id == 'ntchat':
|
elif msg.bot_id == 'ntchat':
|
||||||
if content:
|
await ntchat_send(bot, content, image, node, msg.target_id)
|
||||||
await bot.call_api(
|
# 频道
|
||||||
'send_text',
|
|
||||||
to_wxid=msg.target_id,
|
|
||||||
content=content,
|
|
||||||
)
|
|
||||||
if image:
|
|
||||||
await bot.call_api(
|
|
||||||
'send_image',
|
|
||||||
to_wxid=msg.target_id,
|
|
||||||
content=content,
|
|
||||||
)
|
|
||||||
elif msg.bot_id == 'qqguild':
|
elif msg.bot_id == 'qqguild':
|
||||||
result = {}
|
await guild_send(
|
||||||
if image:
|
bot,
|
||||||
result['file_image'] = image
|
content,
|
||||||
if content:
|
image,
|
||||||
result['content'] = content
|
node,
|
||||||
if msg.target_type == 'group':
|
msg.target_id,
|
||||||
await bot.call_api(
|
msg.target_type,
|
||||||
'post_messages',
|
)
|
||||||
channel_id=int(msg.target_id)
|
|
||||||
if msg.target_id
|
|
||||||
else 0,
|
|
||||||
**result,
|
|
||||||
)
|
|
||||||
except ConnectionClosedError:
|
except ConnectionClosedError:
|
||||||
logger.warning(f'与[gsuid-core]断开连接! Bot_ID: {BOT_ID}')
|
logger.warning(f'与[gsuid-core]断开连接! Bot_ID: {BOT_ID}')
|
||||||
|
|
||||||
@ -113,3 +96,133 @@ class GsClient:
|
|||||||
)
|
)
|
||||||
for task in pending:
|
for task in pending:
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
|
|
||||||
|
def to_json(msg: str, name: str, uin: int):
|
||||||
|
return {
|
||||||
|
'type': 'node',
|
||||||
|
'data': {'name': name, 'uin': uin, 'content': msg},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def onebot_send(
|
||||||
|
bot: Bot,
|
||||||
|
content: Optional[str],
|
||||||
|
image: Optional[bytes],
|
||||||
|
node: Optional[List[Message]],
|
||||||
|
target_id: Optional[str],
|
||||||
|
target_type: Optional[str],
|
||||||
|
):
|
||||||
|
async def _send(content: Optional[str], image: Optional[bytes]):
|
||||||
|
result_image = f'[CQ:image,file=base64://{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,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await bot.call_api(
|
||||||
|
'send_private_msg',
|
||||||
|
user_id=target_id,
|
||||||
|
message=result_msg,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _send_node(messages):
|
||||||
|
if target_type == 'group':
|
||||||
|
await bot.call_api(
|
||||||
|
'send_group_forward_msg',
|
||||||
|
group_id=target_id,
|
||||||
|
messages=messages,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await bot.call_api(
|
||||||
|
'send_private_forward_msg',
|
||||||
|
user_id=target_id,
|
||||||
|
messages=messages,
|
||||||
|
)
|
||||||
|
|
||||||
|
if node:
|
||||||
|
messages = (
|
||||||
|
[
|
||||||
|
to_json(
|
||||||
|
f'[CQ:image,file=base64://{_msg.data}]'
|
||||||
|
if _msg.type == 'image'
|
||||||
|
else _msg.data,
|
||||||
|
'小助手',
|
||||||
|
2854196310,
|
||||||
|
)
|
||||||
|
for _msg in node
|
||||||
|
if _msg.data
|
||||||
|
],
|
||||||
|
)
|
||||||
|
await _send_node(messages)
|
||||||
|
else:
|
||||||
|
await _send(content, image)
|
||||||
|
|
||||||
|
|
||||||
|
async def guild_send(
|
||||||
|
bot: Bot,
|
||||||
|
content: Optional[str],
|
||||||
|
image: Optional[bytes],
|
||||||
|
node: Optional[List[Message]],
|
||||||
|
target_id: Optional[str],
|
||||||
|
target_type: Optional[str],
|
||||||
|
):
|
||||||
|
async def _send(content: Optional[str], image: Optional[bytes]):
|
||||||
|
result = {}
|
||||||
|
if image:
|
||||||
|
result['file_image'] = image
|
||||||
|
if content:
|
||||||
|
result['content'] = content
|
||||||
|
if target_type == 'group':
|
||||||
|
await bot.call_api(
|
||||||
|
'post_messages',
|
||||||
|
channel_id=int(target_id) if target_id else 0,
|
||||||
|
**result,
|
||||||
|
)
|
||||||
|
|
||||||
|
if node:
|
||||||
|
for _msg in node:
|
||||||
|
if _msg.type == 'image':
|
||||||
|
image = _msg.data
|
||||||
|
content = None
|
||||||
|
else:
|
||||||
|
image = None
|
||||||
|
content = _msg.data
|
||||||
|
await _send(content, image)
|
||||||
|
else:
|
||||||
|
await _send(content, image)
|
||||||
|
|
||||||
|
|
||||||
|
async def ntchat_send(
|
||||||
|
bot: Bot,
|
||||||
|
content: Optional[str],
|
||||||
|
image: Optional[bytes],
|
||||||
|
node: Optional[List[Message]],
|
||||||
|
target_id: Optional[str],
|
||||||
|
):
|
||||||
|
async def _send(content: Optional[str], image: Optional[bytes]):
|
||||||
|
if content:
|
||||||
|
await bot.call_api(
|
||||||
|
'send_text',
|
||||||
|
to_wxid=target_id,
|
||||||
|
content=content,
|
||||||
|
)
|
||||||
|
if image:
|
||||||
|
await bot.call_api(
|
||||||
|
'send_image',
|
||||||
|
to_wxid=target_id,
|
||||||
|
content=content,
|
||||||
|
)
|
||||||
|
|
||||||
|
if node:
|
||||||
|
for _msg in node:
|
||||||
|
if _msg.type == 'image':
|
||||||
|
await _send(None, _msg.data)
|
||||||
|
else:
|
||||||
|
await _send(_msg.data, None)
|
||||||
|
else:
|
||||||
|
await _send(content, image)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user