refactor: Refactor some model and config

This commit is contained in:
LWR 2022-11-02 20:02:51 +08:00
parent f31fe443ba
commit f2c271c3b8
4 changed files with 25 additions and 25 deletions

View File

@ -173,17 +173,15 @@ class PushTarget(BaseModel):
def __init__(self, **data: Any): def __init__(self, **data: Any):
super().__init__(**data) super().__init__(**data)
if not self.key: if not self.key:
self.key = str(self.id) self.key = "-".join([str(self.id), str(self.type.value)])
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, PushTarget): if isinstance(other, PushTarget):
return self.id == other.id return self.id == other.id and self.type == other.type
elif isinstance(other, int):
return self.id == other
return False return False
def __hash__(self): def __hash__(self):
return hash(self.id) return hash(self.key)
class Message(BaseModel): class Message(BaseModel):

View File

@ -1,7 +1,7 @@
import asyncio import asyncio
import typing import typing
from asyncio import AbstractEventLoop from asyncio import AbstractEventLoop
from typing import Optional, Union, List, Dict, Any from typing import Optional, List, Any
from pydantic import BaseModel, PrivateAttr from pydantic import BaseModel, PrivateAttr
@ -20,7 +20,7 @@ class Up(BaseModel):
uid: int uid: int
"""主播 UID""" """主播 UID"""
targets: Union[List[PushTarget], Dict[int, PushTarget]] targets: List[PushTarget]
"""主播所需推送的所有好友或群""" """主播所需推送的所有好友或群"""
uname: Optional[str] = None uname: Optional[str] = None
@ -43,8 +43,6 @@ class Up(BaseModel):
def __init__(self, **data: Any): def __init__(self, **data: Any):
super().__init__(**data) 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.__room = None
self.__is_reconnect = False self.__is_reconnect = False
self.__loop = asyncio.get_event_loop() self.__loop = asyncio.get_event_loop()

View File

@ -55,17 +55,15 @@ class Bot(BaseModel, AsyncEvent):
for up in self.ups: for up in self.ups:
up.inject_bot(self) up.inject_bot(self)
# 发送消息方法
@self.on("SEND_MESSAGE")
async def send_message(msg: Message):
self.__queue.append(msg)
# Ariadne 启动成功后启动消息发送模块 # 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}] 已启动")
self.__loop.create_task(self.__sender()) self.__loop.create_task(self.__sender())
def send_message(self, msg: Message):
self.__queue.append(msg)
async def __sender(self): async def __sender(self):
""" """
消息发送模块 消息发送模块

View File

@ -1,8 +1,6 @@
from typing import Any from typing import Any
SIMPLE_CONFIG = { SIMPLE_CONFIG = {
# 是否展示 StarBot Logo
"SHOW_LOGO": True,
# 是否检测最新 StarBot 版本 # 是否检测最新 StarBot 版本
"CHECK_VERSION": True, "CHECK_VERSION": True,
@ -38,6 +36,11 @@ SIMPLE_CONFIG = {
"BILI_JCT": None, "BILI_JCT": None,
"BUVID3": None, "BUVID3": None,
# 是否自动判断仅连接必要的直播间,即当某直播间的开播、下播、直播报告开关均未开启时,自动跳过连接直播间,以节省性能
"ONLY_CONNECT_NECESSARY_ROOM": False,
# 是否自动判断仅处理必要的直播事件,例如当某直播间的下播推送和直播报告中均不包含弹幕相关功能,则不再处理此直播间的弹幕事件,以节省性能
"ONLY_HANDLE_NECESSARY_EVENT": False,
# Bot 主人 QQ用于接收部分 Bot 异常通知等 # Bot 主人 QQ用于接收部分 Bot 异常通知等
"MASTER_QQ": None, "MASTER_QQ": None,
@ -46,8 +49,8 @@ SIMPLE_CONFIG = {
# 是否使用 HTTP API 推送 # 是否使用 HTTP API 推送
"USE_HTTP_API": False, "USE_HTTP_API": False,
# HTTP API 调用地址模板 # HTTP API 端口
"HTTP_API_TEMPLATE": "", "HTTP_API_PORT": 8088,
# 命令触发前缀 # 命令触发前缀
"COMMAND_PREFIX": "", "COMMAND_PREFIX": "",
@ -63,8 +66,6 @@ SIMPLE_CONFIG = {
} }
FULL_CONFIG = { FULL_CONFIG = {
# 是否展示 StarBot Logo
"SHOW_LOGO": True,
# 是否检测最新 StarBot 版本 # 是否检测最新 StarBot 版本
"CHECK_VERSION": True, "CHECK_VERSION": True,
@ -100,6 +101,11 @@ FULL_CONFIG = {
"BILI_JCT": None, "BILI_JCT": None,
"BUVID3": None, "BUVID3": None,
# 是否自动判断仅连接必要的直播间,即当某直播间的开播、下播、直播报告开关均未开启时,自动跳过连接直播间,以节省性能
"ONLY_CONNECT_NECESSARY_ROOM": False,
# 是否自动判断仅处理必要的直播事件,例如当某直播间的下播推送和直播报告中均不包含弹幕相关功能,则不再处理此直播间的弹幕事件,以节省性能
"ONLY_HANDLE_NECESSARY_EVENT": False,
# Bot 主人 QQ用于接收 Bot 异常通知等 # Bot 主人 QQ用于接收 Bot 异常通知等
"MASTER_QQ": None, "MASTER_QQ": None,
@ -108,8 +114,8 @@ FULL_CONFIG = {
# 是否使用 HTTP API 推送 # 是否使用 HTTP API 推送
"USE_HTTP_API": True, "USE_HTTP_API": True,
# HTTP API 调用地址模板 # HTTP API 端口
"HTTP_API_TEMPLATE": "http://localhost/send?key={key}&data={data}", "HTTP_API_PORT": 8088,
# 命令触发前缀 # 命令触发前缀
"COMMAND_PREFIX": "", "COMMAND_PREFIX": "",
@ -143,12 +149,12 @@ def use_simple_config():
""" """
使用最简配置默认配置如下 使用最简配置默认配置如下
展示 StarBot Logo
自动检测最新版本 自动检测最新版本
使用 Redis 默认连接配置 (host: "localhost", port: 6379, db: 0, username: "", password: "") 使用 Redis 默认连接配置 (host: "localhost", port: 6379, db: 0, username: "", password: "")
使用 MySQL 默认连接配置 (host: "localhost", port: 3306, db: "starbot", username: "root", password: "123456") 使用 MySQL 默认连接配置 (host: "localhost", port: 3306, db: "starbot", username: "root", password: "123456")
Mirai 连接端口 7827 Mirai 连接端口 7827
未设置登录 B 站账号所需 Cookie 数据 未设置登录 B 站账号所需 Cookie 数据
不启用节省性能优化
未设置 Bot 主人 QQ 未设置 Bot 主人 QQ
不使用 HTTP 代理 不使用 HTTP 代理
不开启 HTTP API 推送 不开启 HTTP API 推送
@ -163,15 +169,15 @@ def use_full_config():
""" """
使用推荐配置默认配置如下 使用推荐配置默认配置如下
展示 StarBot Logo
自动检测最新版本 自动检测最新版本
使用 Redis 默认连接配置 (host: "localhost", port: 6379, db: 0, username: "", password: "") 使用 Redis 默认连接配置 (host: "localhost", port: 6379, db: 0, username: "", password: "")
使用 MySQL 默认连接配置 (host: "localhost", port: 3306, db: "starbot", username: "root", password: "123456") 使用 MySQL 默认连接配置 (host: "localhost", port: 3306, db: "starbot", username: "root", password: "123456")
Mirai 连接端口 7827 Mirai 连接端口 7827
未设置登录 B 站账号所需 Cookie 数据 未设置登录 B 站账号所需 Cookie 数据
不启用节省性能优化
未设置 Bot 主人 QQ 未设置 Bot 主人 QQ
不使用 HTTP 代理 不使用 HTTP 代理
开启 HTTP API 推送 开启 HTTP API 推送 (port: 8088)
无命令触发前缀 无命令触发前缀
开启风控消息补发 仅补发推送消息 开启风控消息补发 仅补发推送消息
""" """