From f2c271c3b858dbae5dcb8d4eac51d325ee794d44 Mon Sep 17 00:00:00 2001 From: LWR Date: Wed, 2 Nov 2022 20:02:51 +0800 Subject: [PATCH] refactor: Refactor some model and config --- starbot/core/model.py | 8 +++----- starbot/core/room.py | 6 ++---- starbot/core/sender.py | 8 +++----- starbot/utils/config.py | 28 +++++++++++++++++----------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/starbot/core/model.py b/starbot/core/model.py index 639b74a..4f4f215 100644 --- a/starbot/core/model.py +++ b/starbot/core/model.py @@ -173,17 +173,15 @@ class PushTarget(BaseModel): def __init__(self, **data: Any): super().__init__(**data) if not self.key: - self.key = str(self.id) + self.key = "-".join([str(self.id), str(self.type.value)]) def __eq__(self, other): if isinstance(other, PushTarget): - return self.id == other.id - elif isinstance(other, int): - return self.id == other + return self.id == other.id and self.type == other.type return False def __hash__(self): - return hash(self.id) + return hash(self.key) class Message(BaseModel): diff --git a/starbot/core/room.py b/starbot/core/room.py index be0e0e6..09d8dd1 100644 --- a/starbot/core/room.py +++ b/starbot/core/room.py @@ -1,7 +1,7 @@ import asyncio import typing from asyncio import AbstractEventLoop -from typing import Optional, Union, List, Dict, Any +from typing import Optional, List, Any from pydantic import BaseModel, PrivateAttr @@ -20,7 +20,7 @@ class Up(BaseModel): uid: int """主播 UID""" - targets: Union[List[PushTarget], Dict[int, PushTarget]] + targets: List[PushTarget] """主播所需推送的所有好友或群""" uname: Optional[str] = None @@ -43,8 +43,6 @@ class Up(BaseModel): def __init__(self, **data: Any): super().__init__(**data) - if isinstance(self.targets, list): - self.targets = dict(zip(map(lambda t: t.id, self.targets), self.targets)) self.__room = None self.__is_reconnect = False self.__loop = asyncio.get_event_loop() diff --git a/starbot/core/sender.py b/starbot/core/sender.py index 80eb1a5..933f726 100644 --- a/starbot/core/sender.py +++ b/starbot/core/sender.py @@ -55,17 +55,15 @@ class Bot(BaseModel, AsyncEvent): 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}] 已启动") self.__loop.create_task(self.__sender()) + def send_message(self, msg: Message): + self.__queue.append(msg) + async def __sender(self): """ 消息发送模块 diff --git a/starbot/utils/config.py b/starbot/utils/config.py index 9272fda..fd26c3f 100644 --- a/starbot/utils/config.py +++ b/starbot/utils/config.py @@ -1,8 +1,6 @@ from typing import Any SIMPLE_CONFIG = { - # 是否展示 StarBot Logo - "SHOW_LOGO": True, # 是否检测最新 StarBot 版本 "CHECK_VERSION": True, @@ -38,6 +36,11 @@ SIMPLE_CONFIG = { "BILI_JCT": None, "BUVID3": None, + # 是否自动判断仅连接必要的直播间,即当某直播间的开播、下播、直播报告开关均未开启时,自动跳过连接直播间,以节省性能 + "ONLY_CONNECT_NECESSARY_ROOM": False, + # 是否自动判断仅处理必要的直播事件,例如当某直播间的下播推送和直播报告中均不包含弹幕相关功能,则不再处理此直播间的弹幕事件,以节省性能 + "ONLY_HANDLE_NECESSARY_EVENT": False, + # Bot 主人 QQ,用于接收部分 Bot 异常通知等 "MASTER_QQ": None, @@ -46,8 +49,8 @@ SIMPLE_CONFIG = { # 是否使用 HTTP API 推送 "USE_HTTP_API": False, - # HTTP API 调用地址模板 - "HTTP_API_TEMPLATE": "", + # HTTP API 端口 + "HTTP_API_PORT": 8088, # 命令触发前缀 "COMMAND_PREFIX": "", @@ -63,8 +66,6 @@ SIMPLE_CONFIG = { } FULL_CONFIG = { - # 是否展示 StarBot Logo - "SHOW_LOGO": True, # 是否检测最新 StarBot 版本 "CHECK_VERSION": True, @@ -100,6 +101,11 @@ FULL_CONFIG = { "BILI_JCT": None, "BUVID3": None, + # 是否自动判断仅连接必要的直播间,即当某直播间的开播、下播、直播报告开关均未开启时,自动跳过连接直播间,以节省性能 + "ONLY_CONNECT_NECESSARY_ROOM": False, + # 是否自动判断仅处理必要的直播事件,例如当某直播间的下播推送和直播报告中均不包含弹幕相关功能,则不再处理此直播间的弹幕事件,以节省性能 + "ONLY_HANDLE_NECESSARY_EVENT": False, + # Bot 主人 QQ,用于接收 Bot 异常通知等 "MASTER_QQ": None, @@ -108,8 +114,8 @@ FULL_CONFIG = { # 是否使用 HTTP API 推送 "USE_HTTP_API": True, - # HTTP API 调用地址模板 - "HTTP_API_TEMPLATE": "http://localhost/send?key={key}&data={data}", + # HTTP API 端口 + "HTTP_API_PORT": 8088, # 命令触发前缀 "COMMAND_PREFIX": "", @@ -143,12 +149,12 @@ def use_simple_config(): """ 使用最简配置,默认配置如下: - 展示 StarBot Logo 自动检测最新版本 使用 Redis 默认连接配置 (host: "localhost", port: 6379, db: 0, username: "", password: "") 使用 MySQL 默认连接配置 (host: "localhost", port: 3306, db: "starbot", username: "root", password: "123456") Mirai 连接端口 7827 未设置登录 B 站账号所需 Cookie 数据 + 不启用节省性能优化 未设置 Bot 主人 QQ 不使用 HTTP 代理 不开启 HTTP API 推送 @@ -163,15 +169,15 @@ def use_full_config(): """ 使用推荐配置,默认配置如下: - 展示 StarBot Logo 自动检测最新版本 使用 Redis 默认连接配置 (host: "localhost", port: 6379, db: 0, username: "", password: "") 使用 MySQL 默认连接配置 (host: "localhost", port: 3306, db: "starbot", username: "root", password: "123456") Mirai 连接端口 7827 未设置登录 B 站账号所需 Cookie 数据 + 不启用节省性能优化 未设置 Bot 主人 QQ 不使用 HTTP 代理 - 开启 HTTP API 推送 + 开启 HTTP API 推送 (port: 8088) 无命令触发前缀 开启风控消息补发 仅补发推送消息 """