From dfa97c9d24b8124ca38ec5eb605cde932cd6ea78 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Tue, 2 May 2023 23:30:19 +0800 Subject: --- .../plugins/HydroRoll_plugin_base/__init__.py | 93 ---------------------- hydroroll/plugins/HydroRoll_plugin_base/config.py | 30 ------- hydroroll/plugins/HydroRoll_plugin_bot/__init__.py | 30 ------- hydroroll/plugins/HydroRoll_plugin_bot/config.py | 11 --- .../plugins/HydroRoll_plugin_dice/__init__.py | 51 ------------ hydroroll/plugins/HydroRoll_plugin_dice/config.py | 13 --- .../plugins/HydroRoll_plugin_echo/__init__.py | 17 ---- hydroroll/plugins/HydroRoll_plugin_echo/config.py | 11 --- .../plugins/HydroRoll_plugin_luck/__init__.py | 21 ----- hydroroll/plugins/HydroRoll_plugin_luck/config.py | 15 ---- .../plugins/HydroRoll_plugin_send/__init__.py | 28 ------- hydroroll/plugins/HydroRoll_plugin_send/config.py | 15 ---- .../plugins/HydroRoll_plugin_system/__init__.py | 75 ----------------- .../plugins/HydroRoll_plugin_system/config.py | 11 --- hydroroll/plugins/plugin_base/__init__.py | 93 ++++++++++++++++++++++ hydroroll/plugins/plugin_base/config.py | 30 +++++++ hydroroll/plugins/plugin_bot/__init__.py | 30 +++++++ hydroroll/plugins/plugin_bot/config.py | 11 +++ hydroroll/plugins/plugin_echo/__init__.py | 17 ++++ hydroroll/plugins/plugin_echo/config.py | 11 +++ hydroroll/plugins/plugin_luck/__init__.py | 21 +++++ hydroroll/plugins/plugin_luck/config.py | 15 ++++ hydroroll/plugins/plugin_send/__init__.py | 28 +++++++ hydroroll/plugins/plugin_send/config.py | 15 ++++ hydroroll/plugins/plugin_system/__init__.py | 75 +++++++++++++++++ hydroroll/plugins/plugin_system/config.py | 11 +++ hydroroll/plugins/test.py | 8 ++ 27 files changed, 365 insertions(+), 421 deletions(-) delete mode 100644 hydroroll/plugins/HydroRoll_plugin_base/__init__.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_base/config.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_bot/__init__.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_bot/config.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_dice/__init__.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_dice/config.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_echo/__init__.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_echo/config.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_luck/__init__.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_luck/config.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_send/__init__.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_send/config.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_system/__init__.py delete mode 100644 hydroroll/plugins/HydroRoll_plugin_system/config.py create mode 100644 hydroroll/plugins/plugin_base/__init__.py create mode 100644 hydroroll/plugins/plugin_base/config.py create mode 100644 hydroroll/plugins/plugin_bot/__init__.py create mode 100644 hydroroll/plugins/plugin_bot/config.py create mode 100644 hydroroll/plugins/plugin_echo/__init__.py create mode 100644 hydroroll/plugins/plugin_echo/config.py create mode 100644 hydroroll/plugins/plugin_luck/__init__.py create mode 100644 hydroroll/plugins/plugin_luck/config.py create mode 100644 hydroroll/plugins/plugin_send/__init__.py create mode 100644 hydroroll/plugins/plugin_send/config.py create mode 100644 hydroroll/plugins/plugin_system/__init__.py create mode 100644 hydroroll/plugins/plugin_system/config.py create mode 100644 hydroroll/plugins/test.py (limited to 'hydroroll/plugins') diff --git a/hydroroll/plugins/HydroRoll_plugin_base/__init__.py b/hydroroll/plugins/HydroRoll_plugin_base/__init__.py deleted file mode 100644 index a050ae1..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_base/__init__.py +++ /dev/null @@ -1,93 +0,0 @@ -import re -from abc import ABC, abstractmethod -from typing import Type, Union, Generic, TypeVar - -from iamai import Plugin -from iamai.typing import T_State -from iamai.adapter.cqhttp.event import GroupMessageEvent, PrivateMessageEvent - -from .config import BasePluginConfig, RegexPluginConfig, CommandPluginConfig - -T_Config = TypeVar("T_Config", bound=BasePluginConfig) -T_RegexPluginConfig = TypeVar("T_RegexPluginConfig", bound=RegexPluginConfig) -T_CommandPluginConfig = TypeVar("T_CommandPluginConfig", bound=CommandPluginConfig) - - -class BasePlugin( - Plugin[Union[PrivateMessageEvent, GroupMessageEvent], T_State, T_Config], - ABC, - Generic[T_State, T_Config], -): - Config: Type[T_Config] = BasePluginConfig - - def format_str(self, format_str: str, message_str: str = "") -> str: - return format_str.format( - message=message_str, - user_name=self.event.sender.nickname, - user_id=self.event.sender.user_id, - ) - - async def rule(self) -> bool: - is_bot_off = True - - if self.event.adapter.name != "cqhttp": - return False - if self.event.type != "message": - return False - match_str = self.event.message.get_plain_text() - if is_bot_off: - if self.event.message.startswith(f'[CQ:at,qq={self.event.self_id}]'): - match_str = re.sub(fr'^\[CQ:at,qq={self.event.self_id}\]', '', match_str) - else: - return False - if self.config.handle_all_message: - return self.str_match(match_str) - elif self.config.handle_friend_message: - if self.event.message_type == "private": - return self.str_match(match_str) - elif self.config.handle_group_message: - if self.event.message_type == "group": - if ( - self.config.accept_group is None - or self.event.group_id in self.config.accept_group - ): - return self.str_match(match_str) - return False - - @abstractmethod - def str_match(self, msg_str: str) -> bool: - raise NotImplemented - - -class RegexPluginBase(BasePlugin[T_State, T_RegexPluginConfig], ABC): - msg_match: re.Match - re_pattern: re.Pattern - Config: Type[T_RegexPluginConfig] = RegexPluginConfig - - def str_match(self, msg_str: str) -> bool: - msg_str = msg_str.strip() - self.msg_match = self.re_pattern.fullmatch(msg_str) - return bool(self.msg_match) - - -class CommandPluginBase(RegexPluginBase[T_State, T_CommandPluginConfig], ABC): - command_match: re.Match - command_re_pattern: re.Pattern - Config: Type[T_CommandPluginConfig] = CommandPluginConfig - - def str_match(self, msg_str: str) -> bool: - if not hasattr(self, "command_re_pattern"): - self.command_re_pattern = re.compile( - f'[{"".join(self.config.command_prefix)}]' - f'({"|".join(self.config.command)})' - r"\s*(?P.*)", - flags=re.I if self.config.ignore_case else 0, - ) - msg_str = msg_str.strip() - self.command_match = self.command_re_pattern.fullmatch(msg_str) - if not self.command_match: - return False - self.msg_match = self.re_pattern.fullmatch( - self.command_match.group("command_args") - ) - return bool(self.msg_match) \ No newline at end of file diff --git a/hydroroll/plugins/HydroRoll_plugin_base/config.py b/hydroroll/plugins/HydroRoll_plugin_base/config.py deleted file mode 100644 index 311874c..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_base/config.py +++ /dev/null @@ -1,30 +0,0 @@ -from typing import Set, Optional - -from iamai import ConfigModel - - -class BasePluginConfig(ConfigModel): - __config_name__ = "" - handle_all_message: bool = True - """是否处理所有类型的消息,此配置为 True 时会覆盖 handle_friend_message 和 handle_group_message。""" - handle_friend_message: bool = True - """是否处理好友消息。""" - handle_group_message: bool = True - """是否处理群消息。""" - accept_group: Optional[Set[int]] = None - """处理消息的群号,仅当 handle_group_message 为 True 时生效,留空表示处理所有群。""" - message_str: str = "*{user_name} {message}" - """最终发送消息的格式。""" - - -class RegexPluginConfig(BasePluginConfig): - pass - - -class CommandPluginConfig(RegexPluginConfig): - command_prefix: Set[str] = {".", "。","!",":"} - """命令前缀。""" - command: Set[str] = {} - """命令文本。""" - ignore_case: bool = True - """忽略大小写。""" diff --git a/hydroroll/plugins/HydroRoll_plugin_bot/__init__.py b/hydroroll/plugins/HydroRoll_plugin_bot/__init__.py deleted file mode 100644 index 2b3eec8..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_bot/__init__.py +++ /dev/null @@ -1,30 +0,0 @@ -import re -from importlib.metadata import version -from plugins.hydroroll_plugin_base import CommandPluginBase -from hydroroll.config import GlobalConfig - -from .config import Config - - -class HydroBot(CommandPluginBase[None, Config]): - Config = Config - CurrentConfig = GlobalConfig - priority = 0 - - def __post_init__(self): - self.re_pattern = re.compile(r"(?P.*)", flags=re.I) - - def bot_info(self): - info_str = f'{self.CurrentConfig._name} '\ - f'{self.CurrentConfig._version}({self.CurrentConfig._svn}) '\ - f'by {self.CurrentConfig._author} '\ - f'on Python {self.CurrentConfig._python_ver_raw} '\ - f'with {" & ".join([adapter + "("+version("iamai-adapter-"+adapter) +")" for adapter in dict(self.bot.config.adapter)])} '\ - f'for iamai({self.CurrentConfig._iamai_version})' - - return info_str - - async def handle(self) -> None: - await self.event.reply( - self.format_str(self.config.message_str, self.bot_info()) - ) diff --git a/hydroroll/plugins/HydroRoll_plugin_bot/config.py b/hydroroll/plugins/HydroRoll_plugin_bot/config.py deleted file mode 100644 index 717b7b2..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_bot/config.py +++ /dev/null @@ -1,11 +0,0 @@ -from typing import Set - -from plugins.hydroroll_plugin_base import CommandPluginConfig - - -class Config(CommandPluginConfig): - __config_name__ = "plugin_bot_info" - command: Set[str] = {"bot"} - """命令文本。""" - message_str: str = "{message}" - """最终发送消息的格式。""" diff --git a/hydroroll/plugins/HydroRoll_plugin_dice/__init__.py b/hydroroll/plugins/HydroRoll_plugin_dice/__init__.py deleted file mode 100644 index 2c9d2b4..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_dice/__init__.py +++ /dev/null @@ -1,51 +0,0 @@ -import re -import random - -from iamai.log import logger - -from plugins.hydroroll_plugin_base import CommandPluginBase - -from .config import Config - -__all__ = ["Dice"] - -class Dice(CommandPluginBase[None, Config]): - Config = Config - - def __post_init__(self): - self.re_pattern = re.compile( - r"\s*(?P\d+)d(?P\d+)([*x](?P\d+))?", - flags=re.I, - ) - - async def handle(self) -> None: - dice_times = int(self.msg_match.group("dice_times")) - dice_faces = int(self.msg_match.group("dice_faces")) - if self.msg_match.group("dice_multiply") is None: - dice_multiply = None - else: - dice_multiply = int(self.msg_match.group("dice_multiply")) - - if dice_times > self.config.max_dice_times: - await self.event.reply( - self.format_str(self.config.exceed_max_dice_times_str) - ) - return - - dice = [random.randint(1, dice_faces) for _ in range(dice_times)] - dice_sum = sum(dice) - if dice_multiply is None: - result_str = f"{dice_times}D{dice_faces}=" - if dice_times != 1: - result_str += f"{'+'.join(map(lambda x: str(x), dice))}=" - result_str += str(dice_sum) - else: - result_str = f"{dice_times}D{dice_faces}x{dice_multiply}=" - if dice_times != 1: - result_str += ( - f"({'+'.join(map(lambda x: str(x), dice))})x{dice_multiply}=" - ) - result_str += f"{dice_sum}x{dice_multiply}={dice_sum * dice_multiply}" - - logger.info(f"Dice Plugin: {result_str}") - await self.event.reply(self.format_str(self.config.message_str, result_str)) diff --git a/hydroroll/plugins/HydroRoll_plugin_dice/config.py b/hydroroll/plugins/HydroRoll_plugin_dice/config.py deleted file mode 100644 index 4846409..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_dice/config.py +++ /dev/null @@ -1,13 +0,0 @@ -from typing import Set - -from plugins.hydroroll_plugin_base import CommandPluginConfig - - -class Config(CommandPluginConfig): - __config_name__ = "plugin_dice" - command: Set[str] = {"r", "roll", "dice"} - """命令文本。""" - max_dice_times: int = 1000 - """最大单次投掷次数。""" - exceed_max_dice_times_str: str = "错误:超过最大投掷次数。" - """超过最大单次投掷次数时的提示语。""" diff --git a/hydroroll/plugins/HydroRoll_plugin_echo/__init__.py b/hydroroll/plugins/HydroRoll_plugin_echo/__init__.py deleted file mode 100644 index e800384..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_echo/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -import re - -from plugins.hydroroll_plugin_base import CommandPluginBase - -from .config import Config - - -class Echo(CommandPluginBase[None, Config]): - Config = Config - - def __post_init__(self): - self.re_pattern = re.compile(r"(?P.*)", flags=re.I) - - async def handle(self) -> None: - await self.event.reply( - self.format_str(self.config.message_str, self.msg_match.group("echo_str")) - ) diff --git a/hydroroll/plugins/HydroRoll_plugin_echo/config.py b/hydroroll/plugins/HydroRoll_plugin_echo/config.py deleted file mode 100644 index 030bbec..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_echo/config.py +++ /dev/null @@ -1,11 +0,0 @@ -from typing import Set - -from plugins.hydroroll_plugin_base import CommandPluginConfig - - -class Config(CommandPluginConfig): - __config_name__ = "plugin_echo" - command: Set[str] = {"echo"} - """命令文本。""" - message_str: str = "*{user_name} {message}" - """最终发送消息的格式。""" diff --git a/hydroroll/plugins/HydroRoll_plugin_luck/__init__.py b/hydroroll/plugins/HydroRoll_plugin_luck/__init__.py deleted file mode 100644 index a30c964..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_luck/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -import re -import time -import random - -from plugins.hydroroll_plugin_base import CommandPluginBase - -from .config import Config - - -class Luck(CommandPluginBase[None, Config]): - Config = Config - - def __post_init__(self): - self.re_pattern = re.compile(r".*", flags=re.I) - - async def handle(self) -> None: - random.seed( - time.strftime("%Y%j", time.localtime()) + self.format_str("{user_id}") - ) - lucy = random.randint(self.config.min_int, self.config.max_int) - await self.event.reply(self.format_str(self.config.message_str, str(lucy))) diff --git a/hydroroll/plugins/HydroRoll_plugin_luck/config.py b/hydroroll/plugins/HydroRoll_plugin_luck/config.py deleted file mode 100644 index eeef14b..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_luck/config.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import Set - -from plugins.hydroroll_plugin_base import CommandPluginConfig - - -class Config(CommandPluginConfig): - __config_name__ = "plugin_luck" - command: Set[str] = {"luck"} - """命令文本。""" - min_int: int = 0 - """最小随机整数。""" - max_int: int = 100 - """最大随机整数。""" - message_str: str = "{user_name}今天的运气是: {message}" - """最终发送消息的格式。""" diff --git a/hydroroll/plugins/HydroRoll_plugin_send/__init__.py b/hydroroll/plugins/HydroRoll_plugin_send/__init__.py deleted file mode 100644 index 234e5b3..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_send/__init__.py +++ /dev/null @@ -1,28 +0,0 @@ -import re - -from plugins.hydroroll_plugin_base import CommandPluginBase - -from .config import Config - - -class Send(CommandPluginBase[None, Config]): - Config = Config - - def __post_init__(self): - self.re_pattern = re.compile(r"\s*(?P.*)", flags=re.I) - - async def handle(self) -> None: - try: - await self.event.adapter.send( - self.msg_match.group("message"), - "private", - self.config.send_user_id, - ) - except Exception as e: - if self.config.send_filed_msg is not None: - await self.event.reply( - self.format_str(self.config.send_filed_msg, repr(e)) - ) - else: - if self.config.send_success_msg is not None: - await self.event.reply(self.format_str(self.config.send_success_msg)) diff --git a/hydroroll/plugins/HydroRoll_plugin_send/config.py b/hydroroll/plugins/HydroRoll_plugin_send/config.py deleted file mode 100644 index 6903c0f..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_send/config.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import Set, Optional - -from plugins.hydroroll_plugin_base import CommandPluginConfig - - -class Config(CommandPluginConfig): - __config_name__ = "plugin_send" - command: Set[str] = {"send"} - """命令文本。""" - send_user_id: int = 2753364619 - """发送消息的对象的 QQ 号码。""" - send_success_msg: Optional[str] = "已将消息送出√" - """发送成功时回复的消息,设置为 None 表示不发送任何消息。""" - send_filed_msg: Optional[str] = "发送失败:{message}" - """发送失败时回复的消息。""" diff --git a/hydroroll/plugins/HydroRoll_plugin_system/__init__.py b/hydroroll/plugins/HydroRoll_plugin_system/__init__.py deleted file mode 100644 index 6e4f232..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_system/__init__.py +++ /dev/null @@ -1,75 +0,0 @@ -import re -from plugins.hydroroll_plugin_base import CommandPluginBase -from .config import Config -import psutil -import time -from hydroroll.config import GlobalConfig -from iamai.adapter.cqhttp.message import CQHTTPMessageSegment - -class System(CommandPluginBase[None, Config]): - priority: int = 0 - block: bool = True - Config = Config - CurrentConfig = GlobalConfig - - def __post_init__(self): - self.re_pattern = re.compile(r"(?P.*)", flags=re.I) - - def eventReply(self, message: str): - return self.event.reply( - self.format_str(self.config.message_str, message) - ) - - def get_system_status(self) -> str: - cpu_usage = psutil.cpu_percent() - memory_usage = psutil.virtual_memory().percent - disk_usage = psutil.disk_usage('/').percent - - current_time = time.time() - start_time = psutil.Process().create_time() - - uptime_seconds = int(current_time - start_time) - uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) - - info_str = f"{self.CurrentConfig._name} Ver.{self.CurrentConfig._version}" - info_str += f"({self.CurrentConfig._svn}) built in Python {self.CurrentConfig._python_ver}\n" - info_str += f"本地时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}\n" - info_str += f"已启动时间:{uptime_str}\n" - info_str += f"CPU使用率:{cpu_usage}%\n" - info_str += f"内存占用率:{memory_usage}%\n" - info_str += f"磁盘使用率:{disk_usage}%" - - return info_str - - async def handle(self) -> None: - system = self.CurrentConfig.HydroSystem() - try: - sub_command = self.event.get_plain_text().split()[1] - except IndexError: - sub_command = "" - - if sub_command in ["status", "s"]: - await self.event.reply( - self.format_str(self.config.message_str, - self.get_system_status()) - ) - elif sub_command in ["restart", "rst"]: - await self.event.reply( - self.format_str(self.config.message_str, "正在重启系统...") - ) - self.bot.restart() - - elif sub_command in ["reload", "rld"]: - await self.event.reply( - self.format_str(self.config.message_str, "正在重载...") - ) - self.bot.reload_plugins() - await self.event.reply( - self.format_str(self.config.message_str, - f"已加载{len(self.bot.plugins)}枚插件") - ) - else: - await self.event.reply( - CQHTTPMessageSegment.reply(self.event.message_id) + - self.format_str(self.config.message_str, system.help) - ) diff --git a/hydroroll/plugins/HydroRoll_plugin_system/config.py b/hydroroll/plugins/HydroRoll_plugin_system/config.py deleted file mode 100644 index 416fcbe..0000000 --- a/hydroroll/plugins/HydroRoll_plugin_system/config.py +++ /dev/null @@ -1,11 +0,0 @@ -from typing import Set - -from plugins.hydroroll_plugin_base import CommandPluginConfig - - -class Config(CommandPluginConfig): - __config_name__ = "plugin_system_info" - command: Set[str] = {"system"} - """命令文本。""" - message_str: str = "{message}" - """最终发送消息的格式。""" diff --git a/hydroroll/plugins/plugin_base/__init__.py b/hydroroll/plugins/plugin_base/__init__.py new file mode 100644 index 0000000..a050ae1 --- /dev/null +++ b/hydroroll/plugins/plugin_base/__init__.py @@ -0,0 +1,93 @@ +import re +from abc import ABC, abstractmethod +from typing import Type, Union, Generic, TypeVar + +from iamai import Plugin +from iamai.typing import T_State +from iamai.adapter.cqhttp.event import GroupMessageEvent, PrivateMessageEvent + +from .config import BasePluginConfig, RegexPluginConfig, CommandPluginConfig + +T_Config = TypeVar("T_Config", bound=BasePluginConfig) +T_RegexPluginConfig = TypeVar("T_RegexPluginConfig", bound=RegexPluginConfig) +T_CommandPluginConfig = TypeVar("T_CommandPluginConfig", bound=CommandPluginConfig) + + +class BasePlugin( + Plugin[Union[PrivateMessageEvent, GroupMessageEvent], T_State, T_Config], + ABC, + Generic[T_State, T_Config], +): + Config: Type[T_Config] = BasePluginConfig + + def format_str(self, format_str: str, message_str: str = "") -> str: + return format_str.format( + message=message_str, + user_name=self.event.sender.nickname, + user_id=self.event.sender.user_id, + ) + + async def rule(self) -> bool: + is_bot_off = True + + if self.event.adapter.name != "cqhttp": + return False + if self.event.type != "message": + return False + match_str = self.event.message.get_plain_text() + if is_bot_off: + if self.event.message.startswith(f'[CQ:at,qq={self.event.self_id}]'): + match_str = re.sub(fr'^\[CQ:at,qq={self.event.self_id}\]', '', match_str) + else: + return False + if self.config.handle_all_message: + return self.str_match(match_str) + elif self.config.handle_friend_message: + if self.event.message_type == "private": + return self.str_match(match_str) + elif self.config.handle_group_message: + if self.event.message_type == "group": + if ( + self.config.accept_group is None + or self.event.group_id in self.config.accept_group + ): + return self.str_match(match_str) + return False + + @abstractmethod + def str_match(self, msg_str: str) -> bool: + raise NotImplemented + + +class RegexPluginBase(BasePlugin[T_State, T_RegexPluginConfig], ABC): + msg_match: re.Match + re_pattern: re.Pattern + Config: Type[T_RegexPluginConfig] = RegexPluginConfig + + def str_match(self, msg_str: str) -> bool: + msg_str = msg_str.strip() + self.msg_match = self.re_pattern.fullmatch(msg_str) + return bool(self.msg_match) + + +class CommandPluginBase(RegexPluginBase[T_State, T_CommandPluginConfig], ABC): + command_match: re.Match + command_re_pattern: re.Pattern + Config: Type[T_CommandPluginConfig] = CommandPluginConfig + + def str_match(self, msg_str: str) -> bool: + if not hasattr(self, "command_re_pattern"): + self.command_re_pattern = re.compile( + f'[{"".join(self.config.command_prefix)}]' + f'({"|".join(self.config.command)})' + r"\s*(?P.*)", + flags=re.I if self.config.ignore_case else 0, + ) + msg_str = msg_str.strip() + self.command_match = self.command_re_pattern.fullmatch(msg_str) + if not self.command_match: + return False + self.msg_match = self.re_pattern.fullmatch( + self.command_match.group("command_args") + ) + return bool(self.msg_match) \ No newline at end of file diff --git a/hydroroll/plugins/plugin_base/config.py b/hydroroll/plugins/plugin_base/config.py new file mode 100644 index 0000000..311874c --- /dev/null +++ b/hydroroll/plugins/plugin_base/config.py @@ -0,0 +1,30 @@ +from typing import Set, Optional + +from iamai import ConfigModel + + +class BasePluginConfig(ConfigModel): + __config_name__ = "" + handle_all_message: bool = True + """是否处理所有类型的消息,此配置为 True 时会覆盖 handle_friend_message 和 handle_group_message。""" + handle_friend_message: bool = True + """是否处理好友消息。""" + handle_group_message: bool = True + """是否处理群消息。""" + accept_group: Optional[Set[int]] = None + """处理消息的群号,仅当 handle_group_message 为 True 时生效,留空表示处理所有群。""" + message_str: str = "*{user_name} {message}" + """最终发送消息的格式。""" + + +class RegexPluginConfig(BasePluginConfig): + pass + + +class CommandPluginConfig(RegexPluginConfig): + command_prefix: Set[str] = {".", "。","!",":"} + """命令前缀。""" + command: Set[str] = {} + """命令文本。""" + ignore_case: bool = True + """忽略大小写。""" diff --git a/hydroroll/plugins/plugin_bot/__init__.py b/hydroroll/plugins/plugin_bot/__init__.py new file mode 100644 index 0000000..89ba17b --- /dev/null +++ b/hydroroll/plugins/plugin_bot/__init__.py @@ -0,0 +1,30 @@ +import re +from importlib.metadata import version +from plugins.plugin_base import CommandPluginBase +from hydroroll.config import GlobalConfig + +from .config import Config + + +class HydroBot(CommandPluginBase[None, Config]): + Config = Config + CurrentConfig = GlobalConfig + priority = 0 + + def __post_init__(self): + self.re_pattern = re.compile(r"(?P.*)", flags=re.I) + + def bot_info(self): + info_str = f'{self.CurrentConfig._name} '\ + f'{self.CurrentConfig._version}({self.CurrentConfig._svn}) '\ + f'by {self.CurrentConfig._author} '\ + f'on Python {self.CurrentConfig._python_ver_raw} '\ + f'with {" & ".join([adapter + "("+version("iamai-adapter-"+adapter) +")" for adapter in dict(self.bot.config.adapter)])} '\ + f'for iamai({self.CurrentConfig._iamai_version})' + + return info_str + + async def handle(self) -> None: + await self.event.reply( + self.format_str(self.config.message_str, self.bot_info()) + ) diff --git a/hydroroll/plugins/plugin_bot/config.py b/hydroroll/plugins/plugin_bot/config.py new file mode 100644 index 0000000..640f23a --- /dev/null +++ b/hydroroll/plugins/plugin_bot/config.py @@ -0,0 +1,11 @@ +from typing import Set + +from plugins.plugin_base import CommandPluginConfig + + +class Config(CommandPluginConfig): + __config_name__ = "plugin_bot_info" + command: Set[str] = {"bot"} + """命令文本。""" + message_str: str = "{message}" + """最终发送消息的格式。""" diff --git a/hydroroll/plugins/plugin_echo/__init__.py b/hydroroll/plugins/plugin_echo/__init__.py new file mode 100644 index 0000000..faa47df --- /dev/null +++ b/hydroroll/plugins/plugin_echo/__init__.py @@ -0,0 +1,17 @@ +import re + +from plugins.plugin_base import CommandPluginBase + +from .config import Config + + +class Echo(CommandPluginBase[None, Config]): + Config = Config + + def __post_init__(self): + self.re_pattern = re.compile(r"(?P.*)", flags=re.I) + + async def handle(self) -> None: + await self.event.reply( + self.format_str(self.config.message_str, self.msg_match.group("echo_str")) + ) diff --git a/hydroroll/plugins/plugin_echo/config.py b/hydroroll/plugins/plugin_echo/config.py new file mode 100644 index 0000000..c047134 --- /dev/null +++ b/hydroroll/plugins/plugin_echo/config.py @@ -0,0 +1,11 @@ +from typing import Set + +from plugins.plugin_base import CommandPluginConfig + + +class Config(CommandPluginConfig): + __config_name__ = "plugin_echo" + command: Set[str] = {"echo"} + """命令文本。""" + message_str: str = "*{user_name} {message}" + """最终发送消息的格式。""" diff --git a/hydroroll/plugins/plugin_luck/__init__.py b/hydroroll/plugins/plugin_luck/__init__.py new file mode 100644 index 0000000..7967a0b --- /dev/null +++ b/hydroroll/plugins/plugin_luck/__init__.py @@ -0,0 +1,21 @@ +import re +import time +import random + +from plugins.plugin_base import CommandPluginBase + +from .config import Config + + +class Luck(CommandPluginBase[None, Config]): + Config = Config + + def __post_init__(self): + self.re_pattern = re.compile(r".*", flags=re.I) + + async def handle(self) -> None: + random.seed( + time.strftime("%Y%j", time.localtime()) + self.format_str("{user_id}") + ) + lucy = random.randint(self.config.min_int, self.config.max_int) + await self.event.reply(self.format_str(self.config.message_str, str(lucy))) diff --git a/hydroroll/plugins/plugin_luck/config.py b/hydroroll/plugins/plugin_luck/config.py new file mode 100644 index 0000000..6190531 --- /dev/null +++ b/hydroroll/plugins/plugin_luck/config.py @@ -0,0 +1,15 @@ +from typing import Set + +from plugins.plugin_base import CommandPluginConfig + + +class Config(CommandPluginConfig): + __config_name__ = "plugin_luck" + command: Set[str] = {"luck"} + """命令文本。""" + min_int: int = 0 + """最小随机整数。""" + max_int: int = 100 + """最大随机整数。""" + message_str: str = "{user_name}今天的运气是: {message}" + """最终发送消息的格式。""" diff --git a/hydroroll/plugins/plugin_send/__init__.py b/hydroroll/plugins/plugin_send/__init__.py new file mode 100644 index 0000000..2468189 --- /dev/null +++ b/hydroroll/plugins/plugin_send/__init__.py @@ -0,0 +1,28 @@ +import re + +from plugins.plugin_base import CommandPluginBase + +from .config import Config + + +class Send(CommandPluginBase[None, Config]): + Config = Config + + def __post_init__(self): + self.re_pattern = re.compile(r"\s*(?P.*)", flags=re.I) + + async def handle(self) -> None: + try: + await self.event.adapter.send( + self.msg_match.group("message"), + "private", + self.config.send_user_id, + ) + except Exception as e: + if self.config.send_filed_msg is not None: + await self.event.reply( + self.format_str(self.config.send_filed_msg, repr(e)) + ) + else: + if self.config.send_success_msg is not None: + await self.event.reply(self.format_str(self.config.send_success_msg)) diff --git a/hydroroll/plugins/plugin_send/config.py b/hydroroll/plugins/plugin_send/config.py new file mode 100644 index 0000000..5b7fe7b --- /dev/null +++ b/hydroroll/plugins/plugin_send/config.py @@ -0,0 +1,15 @@ +from typing import Set, Optional + +from plugins.plugin_base import CommandPluginConfig + + +class Config(CommandPluginConfig): + __config_name__ = "plugin_send" + command: Set[str] = {"send"} + """命令文本。""" + send_user_id: int = 2753364619 + """发送消息的对象的 QQ 号码。""" + send_success_msg: Optional[str] = "已将消息送出√" + """发送成功时回复的消息,设置为 None 表示不发送任何消息。""" + send_filed_msg: Optional[str] = "发送失败:{message}" + """发送失败时回复的消息。""" diff --git a/hydroroll/plugins/plugin_system/__init__.py b/hydroroll/plugins/plugin_system/__init__.py new file mode 100644 index 0000000..ea797d3 --- /dev/null +++ b/hydroroll/plugins/plugin_system/__init__.py @@ -0,0 +1,75 @@ +import re +from plugins.plugin_base import CommandPluginBase +from .config import Config +import psutil +import time +from hydroroll.config import GlobalConfig +from iamai.adapter.cqhttp.message import CQHTTPMessageSegment + +class System(CommandPluginBase[None, Config]): + priority: int = 0 + block: bool = True + Config = Config + CurrentConfig = GlobalConfig + + def __post_init__(self): + self.re_pattern = re.compile(r"(?P.*)", flags=re.I) + + def eventReply(self, message: str): + return self.event.reply( + self.format_str(self.config.message_str, message) + ) + + def get_system_status(self) -> str: + cpu_usage = psutil.cpu_percent() + memory_usage = psutil.virtual_memory().percent + disk_usage = psutil.disk_usage('/').percent + + current_time = time.time() + start_time = psutil.Process().create_time() + + uptime_seconds = int(current_time - start_time) + uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) + + info_str = f"{self.CurrentConfig._name} Ver.{self.CurrentConfig._version}" + info_str += f"({self.CurrentConfig._svn}) built in Python {self.CurrentConfig._python_ver}\n" + info_str += f"本地时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}\n" + info_str += f"已启动时间:{uptime_str}\n" + info_str += f"CPU使用率:{cpu_usage}%\n" + info_str += f"内存占用率:{memory_usage}%\n" + info_str += f"磁盘使用率:{disk_usage}%" + + return info_str + + async def handle(self) -> None: + system = self.CurrentConfig.HydroSystem() + try: + sub_command = self.event.get_plain_text().split()[1] + except IndexError: + sub_command = "" + + if sub_command in ["status", "s"]: + await self.event.reply( + self.format_str(self.config.message_str, + self.get_system_status()) + ) + elif sub_command in ["restart", "rst"]: + await self.event.reply( + self.format_str(self.config.message_str, "正在重启系统...") + ) + self.bot.restart() + + elif sub_command in ["reload", "rld"]: + await self.event.reply( + self.format_str(self.config.message_str, "正在重载...") + ) + self.bot.reload_plugins() + await self.event.reply( + self.format_str(self.config.message_str, + f"已加载{len(self.bot.plugins)}枚插件") + ) + else: + await self.event.reply( + CQHTTPMessageSegment.reply(self.event.message_id) + + self.format_str(self.config.message_str, system.help) + ) diff --git a/hydroroll/plugins/plugin_system/config.py b/hydroroll/plugins/plugin_system/config.py new file mode 100644 index 0000000..aa5f51b --- /dev/null +++ b/hydroroll/plugins/plugin_system/config.py @@ -0,0 +1,11 @@ +from typing import Set + +from plugins.plugin_base import CommandPluginConfig + + +class Config(CommandPluginConfig): + __config_name__ = "plugin_system_info" + command: Set[str] = {"system"} + """命令文本。""" + message_str: str = "{message}" + """最终发送消息的格式。""" diff --git a/hydroroll/plugins/test.py b/hydroroll/plugins/test.py new file mode 100644 index 0000000..01d0615 --- /dev/null +++ b/hydroroll/plugins/test.py @@ -0,0 +1,8 @@ +from iamai import Plugin + +class Test(Plugin): + async def handle(self) -> None: + ... + + async def rule(self) -> None: + ... \ No newline at end of file -- cgit v1.2.3-70-g09d2