feat: Update model to inject Bot instance reference into Up
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import typing
|
||||||
from asyncio import AbstractEventLoop
|
from asyncio import AbstractEventLoop
|
||||||
from typing import Optional, Union, List, Dict, Any
|
from typing import Optional, Union, List, Dict, Any
|
||||||
|
|
||||||
@@ -7,6 +8,9 @@ from pydantic import BaseModel, PrivateAttr
|
|||||||
from .live import LiveDanmaku
|
from .live import LiveDanmaku
|
||||||
from .model import PushTarget
|
from .model import PushTarget
|
||||||
|
|
||||||
|
if typing.TYPE_CHECKING:
|
||||||
|
from .sender import Bot
|
||||||
|
|
||||||
|
|
||||||
class Up(BaseModel):
|
class Up(BaseModel):
|
||||||
"""
|
"""
|
||||||
@@ -34,6 +38,9 @@ class Up(BaseModel):
|
|||||||
__loop: Optional[AbstractEventLoop] = PrivateAttr()
|
__loop: Optional[AbstractEventLoop] = PrivateAttr()
|
||||||
"""asyncio 事件循环"""
|
"""asyncio 事件循环"""
|
||||||
|
|
||||||
|
__bot: Optional["Bot"] = PrivateAttr()
|
||||||
|
"""主播所关联 Bot 实例"""
|
||||||
|
|
||||||
def __init__(self, **data: Any):
|
def __init__(self, **data: Any):
|
||||||
super().__init__(**data)
|
super().__init__(**data)
|
||||||
if isinstance(self.targets, list):
|
if isinstance(self.targets, list):
|
||||||
@@ -41,6 +48,10 @@ class Up(BaseModel):
|
|||||||
self.__room = None
|
self.__room = None
|
||||||
self.__is_reconnect = False
|
self.__is_reconnect = False
|
||||||
self.__loop = asyncio.get_event_loop()
|
self.__loop = asyncio.get_event_loop()
|
||||||
|
self.__bot = None
|
||||||
|
|
||||||
|
def inject_bot(self, bot):
|
||||||
|
self.__bot = bot
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, Up):
|
if isinstance(other, Up):
|
||||||
|
|||||||
@@ -51,10 +51,16 @@ class Bot(BaseModel, AsyncEvent):
|
|||||||
)
|
)
|
||||||
self.__queue = []
|
self.__queue = []
|
||||||
|
|
||||||
|
# 注入 Bot 实例引用
|
||||||
|
for up in self.ups:
|
||||||
|
up.inject_bot(self)
|
||||||
|
|
||||||
|
# 发送消息方法
|
||||||
@self.on("SEND_MESSAGE")
|
@self.on("SEND_MESSAGE")
|
||||||
async def send_message(msg: Message):
|
async def send_message(msg: Message):
|
||||||
self.__queue.append(msg)
|
self.__queue.append(msg)
|
||||||
|
|
||||||
|
# Ariadne 启动成功后启动消息发送模块
|
||||||
@self.__bot.broadcast.receiver(ApplicationLaunched)
|
@self.__bot.broadcast.receiver(ApplicationLaunched)
|
||||||
async def start_sender():
|
async def start_sender():
|
||||||
logger.success(f"Bot [{self.qq}] 已启动")
|
logger.success(f"Bot [{self.qq}] 已启动")
|
||||||
|
|||||||
Reference in New Issue
Block a user