diff --git a/starbot/core/room.py b/starbot/core/room.py index aa446ac..be0e0e6 100644 --- a/starbot/core/room.py +++ b/starbot/core/room.py @@ -1,4 +1,5 @@ import asyncio +import typing from asyncio import AbstractEventLoop from typing import Optional, Union, List, Dict, Any @@ -7,6 +8,9 @@ from pydantic import BaseModel, PrivateAttr from .live import LiveDanmaku from .model import PushTarget +if typing.TYPE_CHECKING: + from .sender import Bot + class Up(BaseModel): """ @@ -34,6 +38,9 @@ class Up(BaseModel): __loop: Optional[AbstractEventLoop] = PrivateAttr() """asyncio 事件循环""" + __bot: Optional["Bot"] = PrivateAttr() + """主播所关联 Bot 实例""" + def __init__(self, **data: Any): super().__init__(**data) if isinstance(self.targets, list): @@ -41,6 +48,10 @@ class Up(BaseModel): self.__room = None self.__is_reconnect = False self.__loop = asyncio.get_event_loop() + self.__bot = None + + def inject_bot(self, bot): + self.__bot = bot def __eq__(self, other): if isinstance(other, Up): diff --git a/starbot/core/sender.py b/starbot/core/sender.py index cd7698d..80eb1a5 100644 --- a/starbot/core/sender.py +++ b/starbot/core/sender.py @@ -51,10 +51,16 @@ class Bot(BaseModel, AsyncEvent): ) self.__queue = [] + # 注入 Bot 实例引用 + for up in self.ups: + up.inject_bot(self) + + # 发送消息方法 @self.on("SEND_MESSAGE") async def send_message(msg: Message): self.__queue.append(msg) + # Ariadne 启动成功后启动消息发送模块 @self.__bot.broadcast.receiver(ApplicationLaunched) async def start_sender(): logger.success(f"Bot [{self.qq}] 已启动")