补上大部分适配的msg_id并添加at_list支持

This commit is contained in:
Wuyi无疑 2023-03-29 22:28:03 +08:00
parent 41fa893dfc
commit 10230bd1ae
2 changed files with 73 additions and 13 deletions

View File

@ -67,6 +67,7 @@ async def send_char_adv(bot: Bot, ev: Event):
sp_user_type = 'direct' sp_user_type = 'direct'
group_id = None group_id = None
user_id = str(raw_data['from_'].id) user_id = str(raw_data['from_'].id)
msg_id = str(raw_data['message_id'])
# kaiheila # kaiheila
elif 'channel_type' in raw_data: elif 'channel_type' in raw_data:
# 如果发送者是个Bot不响应 # 如果发送者是个Bot不响应
@ -85,6 +86,7 @@ async def send_char_adv(bot: Bot, ev: Event):
# ntchat # ntchat
elif not messages and 'message' in raw_data: elif not messages and 'message' in raw_data:
messages = raw_data['message'] messages = raw_data['message']
msg_id = str(raw_data['data']['msgid'])
# onebot # onebot
elif 'sender' in raw_data: elif 'sender' in raw_data:
if ( if (
@ -106,6 +108,7 @@ async def send_char_adv(bot: Bot, ev: Event):
sp_user_type = 'direct' sp_user_type = 'direct'
group_id = None group_id = None
user_id = raw_data['event'].sender.sender_id.union_id user_id = raw_data['event'].sender.sender_id.union_id
msg_id = str(raw_data['event'].message.message_id)
# ntchat # ntchat
if 'data' in raw_data: if 'data' in raw_data:
if 'chatroom' in raw_data['data']['to_wxid']: if 'chatroom' in raw_data['data']['to_wxid']:

View File

@ -9,13 +9,24 @@ import websockets.client
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 nonebot import get_bot, get_bots, get_driver
from websockets.exceptions import ConnectionClosedError from websockets.exceptions import ConnectionClosedError
from .models import MessageSend, MessageReceive from .models import MessageSend, MessageReceive
BOT_ID = 'NoneBot2' BOT_ID = 'NoneBot2'
bots: Dict[str, str] = {} bots: Dict[str, str] = {}
driver = get_driver()
if hasattr(driver.config, 'gsuid_core_host'):
HOST = driver.config.gsuid_core_host
else:
HOST = 'localhost'
if hasattr(driver.config, 'gsuid_core_port'):
PORT = driver.config.gsuid_core_port
else:
PORT = '8765'
def _get_bot(bot_id: str) -> Bot: def _get_bot(bot_id: str) -> Bot:
@ -33,14 +44,14 @@ def _get_bot(bot_id: str) -> Bot:
class GsClient: class GsClient:
@classmethod @classmethod
async def async_connect( async def async_connect(cls, IP: str = HOST, PORT: Union[str, int] = PORT):
cls, IP: str = 'localhost', PORT: Union[str, int] = '8765'
):
self = GsClient() self = GsClient()
cls.is_alive = True cls.is_alive = True
cls.ws_url = f'ws://{IP}:{PORT}/ws/{BOT_ID}' cls.ws_url = f'ws://{IP}:{PORT}/ws/{BOT_ID}'
logger.info(f'Bot_ID: {BOT_ID}连接至[gsuid-core]: {self.ws_url}...') logger.info(f'Bot_ID: {BOT_ID}连接至[gsuid-core]: {self.ws_url}...')
cls.ws = await websockets.client.connect(cls.ws_url, max_size=2**26) cls.ws = await websockets.client.connect(
cls.ws_url, max_size=2**26, open_timeout=60, ping_timeout=60
)
logger.success(f'与[gsuid-core]成功连接! Bot_ID: {BOT_ID}') logger.success(f'与[gsuid-core]成功连接! Bot_ID: {BOT_ID}')
cls.msg_list = asyncio.queues.Queue() cls.msg_list = asyncio.queues.Queue()
return self return self
@ -63,6 +74,16 @@ class GsClient:
f'{msg.bot_id} - {msg.target_type} - {msg.target_id}' f'{msg.bot_id} - {msg.target_type} - {msg.target_id}'
) )
bot_list = [] bot_list = []
# 解析消息
if msg.bot_id == 'NoneBot2':
if msg.content:
_data = msg.content[0]
if _data.type and _data.type.startswith('log'):
_type = _data.type.split('_')[-1].lower()
getattr(logger, _type)(_data.data)
continue
if msg.bot_self_id in _bots: if msg.bot_self_id in _bots:
bot_list.append(_bots[msg.bot_self_id]) bot_list.append(_bots[msg.bot_self_id])
elif not msg.bot_self_id: elif not msg.bot_self_id:
@ -70,14 +91,11 @@ class GsClient:
else: else:
continue continue
# 解析消息
if msg.bot_id == 'NoneBot2':
continue
content = '' content = ''
image: Optional[str] = None image: Optional[str] = None
node = [] node = []
file = '' file = ''
at_list = []
if msg.content: if msg.content:
for _c in msg.content: for _c in msg.content:
if _c.data: if _c.data:
@ -85,13 +103,12 @@ class GsClient:
content += _c.data content += _c.data
elif _c.type == 'image': elif _c.type == 'image':
image = _c.data image = _c.data
elif _c.type and _c.type.startswith('log'):
_type = _c.type.split('_')[-1].lower()
getattr(logger, _type)(_c.data)
elif _c.type == 'node': elif _c.type == 'node':
node = _c.data node = _c.data
elif _c.type == 'file': elif _c.type == 'file':
file = _c.data file = _c.data
elif _c.type == 'at':
at_list.append(_c.data)
else: else:
pass pass
@ -105,13 +122,21 @@ class GsClient:
image, image,
node, node,
file, file,
at_list,
msg.target_id, msg.target_id,
msg.target_type, msg.target_type,
) )
# ntchat # ntchat
elif msg.bot_id == 'ntchat': elif msg.bot_id == 'ntchat':
await ntchat_send( await ntchat_send(
bot, content, image, file, node, msg.target_id bot,
content,
image,
file,
node,
at_list,
msg.target_id,
msg.target_type,
) )
# 频道 # 频道
elif msg.bot_id == 'qqguild': elif msg.bot_id == 'qqguild':
@ -120,6 +145,7 @@ class GsClient:
content, content,
image, image,
node, node,
at_list,
msg.target_id, msg.target_id,
msg.target_type, msg.target_type,
msg.msg_id, msg.msg_id,
@ -150,6 +176,7 @@ class GsClient:
image, image,
file, file,
node, node,
at_list,
msg.target_id, msg.target_id,
msg.target_type, msg.target_type,
) )
@ -205,6 +232,7 @@ async def onebot_send(
image: Optional[str], image: Optional[str],
node: Optional[List[Dict]], node: Optional[List[Dict]],
file: Optional[str], file: Optional[str],
at_list: Optional[List[str]],
target_id: Optional[str], target_id: Optional[str],
target_type: Optional[str], target_type: Optional[str],
): ):
@ -212,6 +240,9 @@ async def onebot_send(
result_image = f'[CQ:image,file={image}]' if image else '' result_image = f'[CQ:image,file={image}]' if image else ''
content = content if content else '' content = content if content else ''
result_msg = content + result_image result_msg = content + result_image
if at_list and target_type == 'group':
for at in at_list:
result_msg += f'[CQ:at,qq={at}]'
if file: if file:
file_name, file_content = file.split('|') file_name, file_content = file.split('|')
@ -282,6 +313,7 @@ async def guild_send(
content: Optional[str], content: Optional[str],
image: Optional[str], image: Optional[str],
node: Optional[List[Dict]], node: Optional[List[Dict]],
at_list: Optional[List[str]],
target_id: Optional[str], target_id: Optional[str],
target_type: Optional[str], target_type: Optional[str],
msg_id: Optional[str], msg_id: Optional[str],
@ -293,6 +325,9 @@ async def guild_send(
result['file_image'] = img_bytes result['file_image'] = img_bytes
if content: if content:
result['content'] = content result['content'] = content
if at_list and target_type == 'group':
for at in at_list:
result['content'] += f'<@{at}>'
if target_type == 'group': if target_type == 'group':
await bot.call_api( await bot.call_api(
'post_messages', 'post_messages',
@ -325,14 +360,20 @@ async def ntchat_send(
image: Optional[str], image: Optional[str],
file: Optional[str], file: Optional[str],
node: Optional[List[Dict]], node: Optional[List[Dict]],
at_list: Optional[List[str]],
target_id: Optional[str], target_id: Optional[str],
target_type: Optional[str],
): ):
async def _send(content: Optional[str], image: Optional[str]): async def _send(content: Optional[str], image: Optional[str]):
if content: if content:
if at_list and target_type == 'group':
for _ in at_list:
content += '{$@}'
await bot.call_api( await bot.call_api(
'send_text', 'send_text',
to_wxid=target_id, to_wxid=target_id,
content=content, content=content,
at_list=at_list,
) )
if image: if image:
await bot.call_api( await bot.call_api(
@ -454,6 +495,7 @@ async def feishu_send(
image: Optional[str], image: Optional[str],
file: Optional[str], file: Optional[str],
node: Optional[List[Dict]], node: Optional[List[Dict]],
at_list: Optional[List[str]],
target_id: Optional[str], target_id: Optional[str],
target_type: Optional[str], target_type: Optional[str],
): ):
@ -473,6 +515,21 @@ async def feishu_send(
del_file(path) del_file(path)
_type = 'file' _type = 'file'
elif content: elif content:
if at_list and target_type == 'group':
for at in at_list:
try:
name_data = await bot.call_api(
'contact/v3/users',
method='GET',
query={'user_id': at},
body={'user_id_type', 'union_id'},
)
name = name_data['user']['name']
except Exception as e:
logger.warning(f'获取用户名称失败...{e}')
name = at[:3]
content += f'<at user_id="{at}">{name}</at>'
msg = {'text': content} msg = {'text': content}
_type = 'text' _type = 'text'
elif image: elif image: