feat: Update model to inject Bot instance reference into Up

This commit is contained in:
LWR 2022-11-01 18:42:37 +08:00
parent 5e32504b94
commit f31fe443ba
2 changed files with 17 additions and 0 deletions

View File

@ -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):

View File

@ -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}] 已启动")