支持nonebot-adapter-red

This commit is contained in:
KimigaiiWuyi 2023-09-22 01:47:22 +08:00
parent 7e0824d701
commit 5fab1ad607
3 changed files with 93 additions and 1 deletions

View File

@ -266,6 +266,27 @@ async def get_all_message(bot: Bot, ev: Event):
else: else:
logger.debug('[gsuid] 不支持该 Feishu 事件...') logger.debug('[gsuid] 不支持该 Feishu 事件...')
return return
# RedProtocol
elif bot.adapter.get_name() == 'RedProtocol':
from nonebot.adapters.red.event import (
GroupMessageEvent,
PrivateMessageEvent,
)
sp_bot_id = 'onebot:red'
if isinstance(ev, GroupMessageEvent) or isinstance(
ev, PrivateMessageEvent
):
user_id = ev.get_user_id()
msg_id = ev.msgId
if isinstance(ev, GroupMessageEvent):
user_type = 'group'
group_id = str(ev.peerUid)
else:
user_type = 'direct'
else:
logger.debug('[gsuid] 不支持该 RedProtocol 事件...')
return
# ntchat # ntchat
elif bot.adapter.get_name() == 'ntchat': elif bot.adapter.get_name() == 'ntchat':
from nonebot.adapters.ntchat.event import ( from nonebot.adapters.ntchat.event import (
@ -450,6 +471,8 @@ def convert_message(_msg: Any, message: List[Message], index: int):
if file_id in _msg.data.values(): if file_id in _msg.data.values():
message.append(Message('image', _msg.data['file_id'])) message.append(Message('image', _msg.data['file_id']))
logger.debug('[OB12图片]', _msg.data['file_id']) logger.debug('[OB12图片]', _msg.data['file_id'])
elif 'path' in _msg.data:
message.append(Message('image', _msg.data['path']))
else: else:
message.append(Message('image', _msg.data['url'])) message.append(Message('image', _msg.data['url']))
elif _msg.type == 'at': elif _msg.type == 'at':

View File

@ -38,6 +38,8 @@ else:
def _get_bot(bot_id: str) -> Bot: def _get_bot(bot_id: str) -> Bot:
if 'v12' in bot_id: if 'v12' in bot_id:
bot_id = 'onebotv12' bot_id = 'onebotv12'
elif 'red' in bot_id:
bot_id = 'RedProtocol'
# bots: Dict[str, str] 以适配器名称为键、bot_self_id为值的字典 # bots: Dict[str, str] 以适配器名称为键、bot_self_id为值的字典
_refresh_bots() _refresh_bots()
if bot_id not in bots: if bot_id not in bots:
@ -167,6 +169,18 @@ class GsClient:
msg.target_id, msg.target_id,
msg.target_type, msg.target_type,
) )
# RedProtocol
elif msg.bot_id == 'onebot:red':
await onebot_red_send(
bot,
content,
image,
node,
file,
at_list,
msg.target_id,
msg.target_type,
)
# ntchat # ntchat
elif msg.bot_id == 'ntchat': elif msg.bot_id == 'ntchat':
await ntchat_send( await ntchat_send(
@ -370,6 +384,61 @@ async def onebot_send(
await _send(content, image) await _send(content, image)
async def onebot_red_send(
bot: Bot,
content: Optional[str],
image: Optional[str],
node: Optional[List[Dict]],
file: Optional[str],
at_list: Optional[List[str]],
target_id: Optional[str],
target_type: Optional[str],
):
from nonebot.adapters.red.bot import Bot
from nonebot.adapters.red.message import Message, MessageSegment
assert isinstance(bot, Bot)
chat_type = 'group' if target_type == 'group' else 'friend'
async def _send(content: Optional[str], image: Optional[str]):
result_msg: Message = Message()
if image:
img_bytes = base64.b64decode(image.replace('base64://', ''))
result_msg.append(MessageSegment.image(img_bytes))
if content:
result_msg.append(MessageSegment.text(content))
if at_list and target_type == 'group':
for at in at_list:
result_msg += MessageSegment.at(at)
if file:
file_name, file_content = file.split('|')
path = Path(__file__).resolve().parent / file_name
store_file(path, file_content)
result_msg += MessageSegment.file(path)
if target_id:
await bot.send_message(chat_type, target_id, result_msg)
if file:
del_file(path) # type: ignore
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 guild_send( async def guild_send(
bot: Bot, bot: Bot,
content: Optional[str], content: Optional[str],

View File

@ -23,7 +23,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry] [tool.poetry]
name = "nonebot-plugin-genshinuid" name = "nonebot-plugin-genshinuid"
version = "4.1.0" version = "4.2.0"
description = "支持OneBot(QQ)、OneBotV12、QQ频道、微信、KOOK开黑啦、Telegram电报、FeiShu飞书的全功能NoneBot2原神插件" description = "支持OneBot(QQ)、OneBotV12、QQ频道、微信、KOOK开黑啦、Telegram电报、FeiShu飞书的全功能NoneBot2原神插件"
authors = ["KimigaiiWuyi <444835641@qq.com>"] authors = ["KimigaiiWuyi <444835641@qq.com>"]
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"