feat: Auto connect or disconnect live room when reload user and only connect necessary room config was enabled

This commit is contained in:
LWR
2023-03-03 00:16:12 +08:00
parent d85c18a62a
commit d897580e0e
3 changed files with 66 additions and 45 deletions

View File

@@ -382,6 +382,8 @@ class MySQLDataSource(DataSource):
super().format_data()
logger.success(f"已成功重载 {up.uname} (UID: {up.uid}, 房间号: {up.room_id}) 的推送配置")
await up.auto_reload_connect()
async def load_new(self, uid: int):
"""
从 MySQL 中追加读取指定 UID 的用户

View File

@@ -648,7 +648,7 @@ class LiveDanmaku(AsyncEvent):
raise LiveException('尚未连接服务器')
self.__status = self.STATUS_CLOSING
logger.info(f'正在关闭直播间 {self.room_display_id} 的连接')
logger.debug(f'正在关闭直播间 {self.room_display_id} 的连接')
# 取消所有任务
while len(self.__tasks) > 0:
@@ -657,7 +657,7 @@ class LiveDanmaku(AsyncEvent):
self.__status = self.STATUS_CLOSED
await self.__ws.close()
logger.success(f'直播间 {self.room_display_id} 的连接已关闭')
logger.debug(f'直播间 {self.room_display_id} 的连接已关闭')
async def __main(self):
"""
@@ -737,7 +737,7 @@ class LiveDanmaku(AsyncEvent):
self.__status = self.STATUS_CLOSING
elif msg.type == aiohttp.WSMsgType.CLOSED:
logger.info(f'直播间 {self.room_display_id} 的连接已关闭')
logger.debug(f'直播间 {self.room_display_id} 的连接已关闭')
self.__status = self.STATUS_CLOSED
# 正常断开情况下跳出循环

View File

@@ -94,9 +94,6 @@ class Up(BaseModel):
return any([self.__any_live_report_item_enabled(a) for a in attribute])
return any(map(lambda t: t.live_report.enabled and t.live_report.__getattribute__(attribute), self.targets))
def __any_dynamic_update_enabled(self):
return any(map(lambda conf: conf.enabled, map(lambda group: group.dynamic_update, self.targets)))
async def connect(self):
"""
连接直播间
@@ -343,7 +340,6 @@ class Up(BaseModel):
await redis.incr_room_guard_time(self.room_id, int(time.time()), month)
if self.__any_dynamic_update_enabled():
@self.__room.on("DYNAMIC_UPDATE")
async def dynamic_update(event):
"""
@@ -386,6 +382,29 @@ class Up(BaseModel):
await self.__bot.send_dynamic_at(self)
await self.__bot.send_dynamic_update(self, dynamic_update_args)
async def disconnect(self):
"""
断开连接直播间
"""
if self.__room is not None and self.__room.get_status() == 2:
await self.__room.disconnect()
self.__is_reconnect = False
logger.success(f"已断开连接 {self.uname} 的直播间 {self.room_id}")
await self.accumulate_and_reset_data()
async def auto_reload_connect(self):
"""
自动判断仅连接必要的直播间开启时,重载配置时自动处理直播间连接状态
"""
if config.get("ONLY_CONNECT_NECESSARY_ROOM"):
if any([self.__any_live_on_enabled(), self.__any_live_off_enabled(), self.__any_live_report_enabled()]):
if self.__room is None or self.__room.get_status() != 2:
await self.connect()
else:
if self.__room is not None and self.__room.get_status() == 2:
await self.disconnect()
async def __generate_live_report_param(self):
"""
计算直播报告所需数据