2023-11-13 21:59:23 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
import typing
|
|
|
|
|
|
2023-04-21 16:36:59 +08:00
|
|
|
|
import mirai
|
|
|
|
|
import mirai.models.bus
|
2023-07-31 21:04:59 +08:00
|
|
|
|
from mirai.bot import MiraiRunner
|
2023-04-21 16:36:59 +08:00
|
|
|
|
|
2023-11-13 21:59:23 +08:00
|
|
|
|
from .. import adapter as adapter_model
|
2024-02-08 13:12:33 +08:00
|
|
|
|
from ...core import app
|
2023-04-21 16:36:59 +08:00
|
|
|
|
|
2023-04-21 17:15:32 +08:00
|
|
|
|
|
2024-02-08 13:12:33 +08:00
|
|
|
|
@adapter_model.adapter_class("yiri-mirai")
|
2023-11-13 21:59:23 +08:00
|
|
|
|
class YiriMiraiAdapter(adapter_model.MessageSourceAdapter):
|
2023-04-21 16:36:59 +08:00
|
|
|
|
"""YiriMirai适配器"""
|
|
|
|
|
bot: mirai.Mirai
|
|
|
|
|
|
2024-02-08 13:12:33 +08:00
|
|
|
|
def __init__(self, config: dict, ap: app.Application):
|
2023-04-21 16:36:59 +08:00
|
|
|
|
"""初始化YiriMirai的对象"""
|
2024-02-08 13:12:33 +08:00
|
|
|
|
self.ap = ap
|
|
|
|
|
self.config = config
|
2023-04-21 16:36:59 +08:00
|
|
|
|
if 'adapter' not in config or \
|
|
|
|
|
config['adapter'] == 'WebSocketAdapter':
|
|
|
|
|
self.bot = mirai.Mirai(
|
|
|
|
|
qq=config['qq'],
|
|
|
|
|
adapter=mirai.WebSocketAdapter(
|
|
|
|
|
host=config['host'],
|
|
|
|
|
port=config['port'],
|
|
|
|
|
verify_key=config['verifyKey']
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
elif config['adapter'] == 'HTTPAdapter':
|
|
|
|
|
self.bot = mirai.Mirai(
|
|
|
|
|
qq=config['qq'],
|
|
|
|
|
adapter=mirai.HTTPAdapter(
|
|
|
|
|
host=config['host'],
|
|
|
|
|
port=config['port'],
|
|
|
|
|
verify_key=config['verifyKey']
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise Exception('Unknown adapter for YiriMirai: ' + config['adapter'])
|
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def send_message(
|
2023-04-21 16:36:59 +08:00
|
|
|
|
self,
|
|
|
|
|
target_type: str,
|
|
|
|
|
target_id: str,
|
|
|
|
|
message: mirai.MessageChain
|
|
|
|
|
):
|
|
|
|
|
"""发送消息
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
target_type (str): 目标类型,`person`或`group`
|
|
|
|
|
target_id (str): 目标ID
|
|
|
|
|
message (mirai.MessageChain): YiriMirai库的消息链
|
|
|
|
|
"""
|
|
|
|
|
task = None
|
|
|
|
|
if target_type == 'person':
|
|
|
|
|
task = self.bot.send_friend_message(int(target_id), message)
|
|
|
|
|
elif target_type == 'group':
|
|
|
|
|
task = self.bot.send_group_message(int(target_id), message)
|
|
|
|
|
else:
|
|
|
|
|
raise Exception('Unknown target type: ' + target_type)
|
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await task
|
2023-04-21 16:36:59 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def reply_message(
|
2023-04-21 16:36:59 +08:00
|
|
|
|
self,
|
|
|
|
|
message_source: mirai.MessageEvent,
|
2023-04-21 17:51:58 +08:00
|
|
|
|
message: mirai.MessageChain,
|
|
|
|
|
quote_origin: bool = False
|
2023-04-21 16:36:59 +08:00
|
|
|
|
):
|
|
|
|
|
"""回复消息
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
message_source (mirai.MessageEvent): YiriMirai消息源事件
|
|
|
|
|
message (mirai.MessageChain): YiriMirai库的消息链
|
2023-04-23 15:58:37 +08:00
|
|
|
|
quote_origin (bool, optional): 是否引用原消息. Defaults to False.
|
2023-04-21 16:36:59 +08:00
|
|
|
|
"""
|
2024-01-23 22:28:30 +08:00
|
|
|
|
await self.bot.send(message_source, message, quote_origin)
|
2023-04-21 17:51:58 +08:00
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def is_muted(self, group_id: int) -> bool:
|
|
|
|
|
result = await self.bot.member_info(target=group_id, member_id=self.bot.qq).get()
|
2023-04-21 17:51:58 +08:00
|
|
|
|
if result.mute_time_remaining > 0:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
2023-04-21 16:36:59 +08:00
|
|
|
|
|
|
|
|
|
def register_listener(
|
|
|
|
|
self,
|
|
|
|
|
event_type: typing.Type[mirai.Event],
|
|
|
|
|
callback: typing.Callable[[mirai.Event], None]
|
|
|
|
|
):
|
|
|
|
|
"""注册事件监听器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
event_type (typing.Type[mirai.Event]): YiriMirai事件类型
|
|
|
|
|
callback (typing.Callable[[mirai.Event], None]): 回调函数,接收一个参数,为YiriMirai事件
|
|
|
|
|
"""
|
|
|
|
|
self.bot.on(event_type)(callback)
|
|
|
|
|
|
|
|
|
|
def unregister_listener(
|
|
|
|
|
self,
|
|
|
|
|
event_type: typing.Type[mirai.Event],
|
|
|
|
|
callback: typing.Callable[[mirai.Event], None]
|
|
|
|
|
):
|
|
|
|
|
"""注销事件监听器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
event_type (typing.Type[mirai.Event]): YiriMirai事件类型
|
|
|
|
|
callback (typing.Callable[[mirai.Event], None]): 回调函数,接收一个参数,为YiriMirai事件
|
|
|
|
|
"""
|
|
|
|
|
assert isinstance(self.bot, mirai.Mirai)
|
|
|
|
|
bus = self.bot.bus
|
|
|
|
|
assert isinstance(bus, mirai.models.bus.ModelEventBus)
|
|
|
|
|
|
|
|
|
|
bus.unsubscribe(event_type, callback)
|
|
|
|
|
|
2024-01-23 22:28:30 +08:00
|
|
|
|
async def run_async(self):
|
2024-02-08 13:12:33 +08:00
|
|
|
|
self.bot_account_id = self.bot.qq
|
2024-01-23 22:28:30 +08:00
|
|
|
|
return await MiraiRunner(self.bot)._run()
|
2023-04-21 16:36:59 +08:00
|
|
|
|
|
2024-02-07 20:03:46 +08:00
|
|
|
|
async def kill(self) -> bool:
|
2023-04-21 16:36:59 +08:00
|
|
|
|
return False
|