From 6fc1ce4631dec5de19c348483ef7509c7314880c Mon Sep 17 00:00:00 2001 From: LWR Date: Tue, 14 Feb 2023 20:39:15 +0800 Subject: [PATCH] feat: Custom commands load support --- .gitignore | 2 ++ starbot/core/bot.py | 4 ++++ starbot/core/room.py | 4 ++++ starbot/utils/config.py | 10 ++++++++-- starbot/utils/redis.py | 38 ++++++++++++++++++++++++++++++++++---- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d86d3e7..66099d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea venv + +starbot/commands/private main.py \ No newline at end of file diff --git a/starbot/core/bot.py b/starbot/core/bot.py index 02038c2..14691f6 100644 --- a/starbot/core/bot.py +++ b/starbot/core/bot.py @@ -109,11 +109,15 @@ class StarBot: # 载入命令 logger.info("开始载入命令模块") + custom_commands = config.get("CUSTOM_COMMANDS_PACKAGE") saya = create(Saya) with saya.module_context(): saya.require(f"starbot.commands.builtin") logger.success("内置命令模块载入完毕") + if custom_commands: + saya.require(custom_commands) + logger.success("用户自定义命令模块载入完毕") # 启动消息推送模块 if not self.__datasource.bots: diff --git a/starbot/core/room.py b/starbot/core/room.py index 59adc5d..f0253b4 100644 --- a/starbot/core/room.py +++ b/starbot/core/room.py @@ -63,6 +63,10 @@ class Up(BaseModel): self.__loop = asyncio.get_event_loop() self.__bot = None + @property + def status(self): + return 6 if not self.__room else self.__room.get_status() + def inject_bot(self, bot): self.__bot = bot diff --git a/starbot/utils/config.py b/starbot/utils/config.py index 0facb95..ffe759b 100644 --- a/starbot/utils/config.py +++ b/starbot/utils/config.py @@ -64,9 +64,12 @@ SIMPLE_CONFIG = { # 弹幕词云图片背景色 "DANMU_CLOUD_BACKGROUND_COLOR": "white", # 弹幕词云最大字号 - "DANMU_CLOUD_MAX_FONT_SIZE": 300, + "DANMU_CLOUD_MAX_FONT_SIZE": 200, # 弹幕词云最多词数 - "DANMU_CLOUD_MAX_WORDS": 100, + "DANMU_CLOUD_MAX_WORDS": 80, + + # 需加载的用户自定义命令包 + "CUSTOM_COMMANDS_PACKAGE": None, # Bot 主人 QQ,用于接收部分 Bot 异常通知等 "MASTER_QQ": None, @@ -162,6 +165,9 @@ FULL_CONFIG = { # 弹幕词云最多词数 "DANMU_CLOUD_MAX_WORDS": 80, + # 需加载的用户自定义命令包 + "CUSTOM_COMMANDS_PACKAGE": None, + # Bot 主人 QQ,用于接收 Bot 异常通知等 "MASTER_QQ": None, diff --git a/starbot/utils/redis.py b/starbot/utils/redis.py index 1d1a7c2..4186e2a 100644 --- a/starbot/utils/redis.py +++ b/starbot/utils/redis.py @@ -1,4 +1,4 @@ -from typing import Any, Optional, Union, Tuple, List, Set +from typing import Any, Union, Tuple, List, Set import aioredis from loguru import logger @@ -27,6 +27,36 @@ async def init(): # String +async def expire(key: str, seconds: int): + await __redis.expire(key, seconds) + + +async def exists(key: str) -> bool: + return await __redis.exists(key) + + +async def get(key: str) -> str: + result = await __redis.get(key) + if result is None: + return "" + return result.decode() + + +async def geti(key: str) -> int: + result = await __redis.get(key) + if result is None: + return 0 + return int(result) + + +async def incr(key: str, value: int = 1) -> int: + return await __redis.incr(key, value) + + +async def set_(key: str, value: Union[str, int]): + await __redis.set(key, value) + + async def delete(key: str): await __redis.delete(key) @@ -83,11 +113,11 @@ async def hset(key: str, hkey: Union[str, int], value: Any): await __redis.hset(key, hkey, value) -async def hincrby(key: str, hkey: Union[str, int], value: Optional[int] = 1) -> int: +async def hincrby(key: str, hkey: Union[str, int], value: int = 1) -> int: return await __redis.hincrby(key, hkey, value) -async def hincrbyfloat(key: str, hkey: Union[str, int], value: Optional[float] = 1.0) -> float: +async def hincrbyfloat(key: str, hkey: Union[str, int], value: float = 1.0) -> float: return await __redis.hincrbyfloat(key, hkey, value) @@ -162,7 +192,7 @@ async def zadd(key: str, member: str, score: Union[int, float]): await __redis.zadd(key, {member: score}) -async def zincrby(key: str, member: Union[str, int], score: Optional[Union[int, float]] = 1) -> float: +async def zincrby(key: str, member: Union[str, int], score: Union[int, float] = 1) -> float: return await __redis.zincrby(key, score, member)