From db74ade0234a40c2120ad5f2a41bee50ce13de02 Mon Sep 17 00:00:00 2001 From: HsiangNianian Date: Sat, 22 Apr 2023 19:51:01 +0800 Subject: 🤐 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HydroRoll/Hydro/__init__.py | 2 - HydroRoll/Hydro/iamai_plugin_dice/__init__.py | 51 ------------- HydroRoll/Hydro/iamai_plugin_dice/config.py | 13 ---- HydroRoll/Hydro/roll.py | 12 --- HydroRoll/__init__.py | 5 +- HydroRoll/bot.py | 34 +++++++++ HydroRoll/config.py | 29 ++++++++ HydroRoll/config.toml | 28 ------- HydroRoll/data/bot_info.json | 3 - HydroRoll/data/reply_data.json | 17 ----- HydroRoll/main.py | 7 -- .../plugins/HydroRoll_plugin_base/__init__.py | 85 ++++++++++++++++++++++ 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 | 73 +++++++++++++++++++ .../plugins/HydroRoll_plugin_system/config.py | 11 +++ HydroRoll/plugins/__init__.py | 1 - HydroRoll/plugins/iamai_plugin_base/__init__.py | 85 ---------------------- HydroRoll/plugins/iamai_plugin_base/config.py | 30 -------- .../plugins/iamai_plugin_bot_info/__init__.py | 20 ----- HydroRoll/plugins/iamai_plugin_bot_info/config.py | 11 --- HydroRoll/plugins/iamai_plugin_dice/__init__.py | 51 ------------- HydroRoll/plugins/iamai_plugin_dice/config.py | 13 ---- HydroRoll/plugins/iamai_plugin_echo/__init__.py | 17 ----- HydroRoll/plugins/iamai_plugin_echo/config.py | 11 --- HydroRoll/plugins/iamai_plugin_luck/__init__.py | 21 ------ HydroRoll/plugins/iamai_plugin_luck/config.py | 15 ---- HydroRoll/plugins/iamai_plugin_reply/__init__.py | 43 ----------- HydroRoll/plugins/iamai_plugin_reply/config.py | 11 --- HydroRoll/plugins/iamai_plugin_send/__init__.py | 28 ------- HydroRoll/plugins/iamai_plugin_send/config.py | 15 ---- .../plugins/iamai_plugin_system_info/__init__.py | 41 ----------- .../plugins/iamai_plugin_system_info/config.py | 11 --- HydroRoll/plugins/plugin.py | 1 - data.csv | 4 + test.py | 18 +++++ test/config.toml | 28 +++++++ test/data/bot_info.json | 3 + test/data/reply_data.json | 17 +++++ test/main.py | 6 ++ test/plugins/iamai_plugin_base/__init__.py | 85 ++++++++++++++++++++++ test/plugins/iamai_plugin_base/config.py | 30 ++++++++ test/plugins/iamai_plugin_reply/__init__.py | 43 +++++++++++ test/plugins/iamai_plugin_reply/config.py | 11 +++ 53 files changed, 723 insertions(+), 559 deletions(-) delete mode 100644 HydroRoll/Hydro/__init__.py delete mode 100644 HydroRoll/Hydro/iamai_plugin_dice/__init__.py delete mode 100644 HydroRoll/Hydro/iamai_plugin_dice/config.py delete mode 100644 HydroRoll/Hydro/roll.py create mode 100644 HydroRoll/bot.py create mode 100644 HydroRoll/config.py delete mode 100644 HydroRoll/config.toml delete mode 100644 HydroRoll/data/bot_info.json delete mode 100644 HydroRoll/data/reply_data.json delete mode 100644 HydroRoll/main.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_base/__init__.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_base/config.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_bot/__init__.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_bot/config.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_dice/__init__.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_dice/config.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_echo/__init__.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_echo/config.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_luck/__init__.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_luck/config.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_send/__init__.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_send/config.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_system/__init__.py create mode 100644 HydroRoll/plugins/HydroRoll_plugin_system/config.py delete mode 100644 HydroRoll/plugins/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_base/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_base/config.py delete mode 100644 HydroRoll/plugins/iamai_plugin_bot_info/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_bot_info/config.py delete mode 100644 HydroRoll/plugins/iamai_plugin_dice/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_dice/config.py delete mode 100644 HydroRoll/plugins/iamai_plugin_echo/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_echo/config.py delete mode 100644 HydroRoll/plugins/iamai_plugin_luck/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_luck/config.py delete mode 100644 HydroRoll/plugins/iamai_plugin_reply/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_reply/config.py delete mode 100644 HydroRoll/plugins/iamai_plugin_send/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_send/config.py delete mode 100644 HydroRoll/plugins/iamai_plugin_system_info/__init__.py delete mode 100644 HydroRoll/plugins/iamai_plugin_system_info/config.py delete mode 100644 HydroRoll/plugins/plugin.py create mode 100644 data.csv create mode 100644 test.py create mode 100644 test/config.toml create mode 100644 test/data/bot_info.json create mode 100644 test/data/reply_data.json create mode 100644 test/main.py create mode 100644 test/plugins/iamai_plugin_base/__init__.py create mode 100644 test/plugins/iamai_plugin_base/config.py create mode 100644 test/plugins/iamai_plugin_reply/__init__.py create mode 100644 test/plugins/iamai_plugin_reply/config.py diff --git a/HydroRoll/Hydro/__init__.py b/HydroRoll/Hydro/__init__.py deleted file mode 100644 index 4171cac..0000000 --- a/HydroRoll/Hydro/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .iamai_plugin_dice import Dice -from .roll import Roll \ No newline at end of file diff --git a/HydroRoll/Hydro/iamai_plugin_dice/__init__.py b/HydroRoll/Hydro/iamai_plugin_dice/__init__.py deleted file mode 100644 index 396278a..0000000 --- a/HydroRoll/Hydro/iamai_plugin_dice/__init__.py +++ /dev/null @@ -1,51 +0,0 @@ -import re -import random - -from iamai.log import logger - -from plugins.iamai_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/Hydro/iamai_plugin_dice/config.py b/HydroRoll/Hydro/iamai_plugin_dice/config.py deleted file mode 100644 index a8c6700..0000000 --- a/HydroRoll/Hydro/iamai_plugin_dice/config.py +++ /dev/null @@ -1,13 +0,0 @@ -from typing import Set - -from plugins.iamai_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/Hydro/roll.py b/HydroRoll/Hydro/roll.py deleted file mode 100644 index c08bce9..0000000 --- a/HydroRoll/Hydro/roll.py +++ /dev/null @@ -1,12 +0,0 @@ -from iamai import Plugin - -class Roll(Plugin): - async def handle(self) -> None: - await self.event.reply("1") - - async def rule(self) -> bool: - return( - self.event.adapter.name == "cqhttp" and - self.event.type == "message" and - self.event.get_plain_text() == "1" - ) \ No newline at end of file diff --git a/HydroRoll/__init__.py b/HydroRoll/__init__.py index d73cd0c..c511260 100644 --- a/HydroRoll/__init__.py +++ b/HydroRoll/__init__.py @@ -1 +1,4 @@ -name = "HydroRoll" \ No newline at end of file +name = "HydroR" + +from HydroR.bot import Bot +from HydroR.config import GlobalConfig \ No newline at end of file diff --git a/HydroRoll/bot.py b/HydroRoll/bot.py new file mode 100644 index 0000000..9dcc623 --- /dev/null +++ b/HydroRoll/bot.py @@ -0,0 +1,34 @@ +from iamai import Bot as _Bot +from typing import Optional, Dict, List, Type, Any, Union +from pathlib import Path + +# 获取当前目录路径 +current_dir = Path.cwd() + +# 获取当前脚本文件路径 +script_file = current_dir.resolve() / __file__ + +# 获取当前脚本文件所在目录路径 +script_dir = script_file.parent + +__all__ = ["Bot"] + +class Bot: + def __init__( + self, + *, + config_file: Optional[str] = "config.toml", + config_dict: Optional[Dict] = None, + hot_reload: bool = False, + ) -> None: + self.bot = _Bot(hot_reload=hot_reload, + config_file=config_file, + config_dict=config_dict + ) + self.bot.load_plugins_from_dirs(Path(f"{script_dir}/plugins")) + + def run(self) -> None: + self.bot.run() + + def restart(self) -> None: + self.bot.restart() \ No newline at end of file diff --git a/HydroRoll/config.py b/HydroRoll/config.py new file mode 100644 index 0000000..1e132e4 --- /dev/null +++ b/HydroRoll/config.py @@ -0,0 +1,29 @@ +import argparse +import sys +import platform +from importlib.metadata import version +from iamai import Plugin +# 创建全局 ArgumentParser 对象 +global_parser = argparse.ArgumentParser(description='HydroRoll[水系] 全局命令参数') + +# 定义全局配置类 +class GlobalConfig: + _name = "HydroRoll" + _version = "0.1.0" + _svn = "2" + _author = "简律纯" + _iamai_version = version('iamai') + _python_ver = sys.version + _python_ver_raw= '.'.join(map(str, platform.python_version_tuple()[:3])) + # 定义系统组件 + class HydroSystem: + def __init__(self): + self.parser = argparse.ArgumentParser(description='HydroRoll[水系].system 系统命令参数') + self.subparsers = self.parser.add_subparsers() + self.status_parser = self.subparsers.add_parser('status', aliases=['s'], help='系统状态') + self.reload_parser = self.subparsers.add_parser('reload', aliases=['rld'], help='重新加载系统') + self.restart_parser = self.subparsers.add_parser('restart', aliases=['rst'], help='重启系统') + self.help = '\n'.join(self.parser.format_help().replace('\r\n', '\n').replace('\r', '').split('\n')[2:-3]) + class HydroBot: + def __init__(self) -> None: + self.parser = argparse.ArgumentParser(description="Bot命令") \ No newline at end of file diff --git a/HydroRoll/config.toml b/HydroRoll/config.toml deleted file mode 100644 index 25ce660..0000000 --- a/HydroRoll/config.toml +++ /dev/null @@ -1,28 +0,0 @@ -[Chien] -version = "v0.1.0" -svn = "1" -author = "简律纯" - -[Chien.self] -header = "Hydro系[1]号" -info = "一只水系骰子..." - - -[bot] -plugins = [] -plugin_dirs = ["plugins"] -adapters = ["iamai.adapter.cqhttp","iamai.adapter.apscheduler"] - -[bot.log] -level = "INFO" -verbose_exception = true - -[adapter.cqhttp] -adapter_type = "reverse-ws" -host = "127.0.0.1" -port = 8080 -url = "/cqhttp/ws" -show_raw = false - -[adapter.apscheduler] -scheduler_config = { "apscheduler.timezone" = "Asia/Shanghai" } \ No newline at end of file diff --git a/HydroRoll/data/bot_info.json b/HydroRoll/data/bot_info.json deleted file mode 100644 index 5ff30dc..0000000 --- a/HydroRoll/data/bot_info.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - nick : "本大魔王" -} \ No newline at end of file diff --git a/HydroRoll/data/reply_data.json b/HydroRoll/data/reply_data.json deleted file mode 100644 index e4977ee..0000000 --- a/HydroRoll/data/reply_data.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "rule": "hello", - "message": "你好!" - }, - { - "rule": "test", - "message": [ - { - "type": "text", - "data": { - "text": "测试" - } - } - ] - } -] \ No newline at end of file diff --git a/HydroRoll/main.py b/HydroRoll/main.py deleted file mode 100644 index 793bbb7..0000000 --- a/HydroRoll/main.py +++ /dev/null @@ -1,7 +0,0 @@ -from iamai import Bot - - -bot = Bot(hot_reload=True) - -if __name__ == "__main__": - bot.run() diff --git a/HydroRoll/plugins/HydroRoll_plugin_base/__init__.py b/HydroRoll/plugins/HydroRoll_plugin_base/__init__.py new file mode 100644 index 0000000..8fe559b --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_base/__init__.py @@ -0,0 +1,85 @@ +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: + if self.event.adapter.name != "cqhttp": + return False + if self.event.type != "message_sent": + return False + if self.config.handle_all_message: + return self.str_match(self.event.message.get_plain_text()) + elif self.config.handle_friend_message: + if self.event.message_type == "private": + return self.str_match(self.event.message.get_plain_text()) + 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(self.event.message.get_plain_text()) + 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) diff --git a/HydroRoll/plugins/HydroRoll_plugin_base/config.py b/HydroRoll/plugins/HydroRoll_plugin_base/config.py new file mode 100644 index 0000000..04924e0 --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_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/HydroRoll_plugin_bot/__init__.py b/HydroRoll/plugins/HydroRoll_plugin_bot/__init__.py new file mode 100644 index 0000000..92cfb3c --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_bot/__init__.py @@ -0,0 +1,30 @@ +import re +from importlib.metadata import version +from plugins.HydroRoll_plugin_base import CommandPluginBase +from HydroR.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} ' + info_str += f'{self.CurrentConfig._version}({self.CurrentConfig._svn}) ' + info_str += f'by {self.CurrentConfig._author} ' + info_str += f'on Python {self.CurrentConfig._python_ver_raw}\n' + info_str += f'with {" & ".join([adapter + "("+version("iamai-adapter-"+adapter) +")" for adapter in dict(self.bot.config.adapter)])}\n' + info_str += 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 new file mode 100644 index 0000000..cf92e81 --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_bot/config.py @@ -0,0 +1,11 @@ +from typing import Set + +from plugins.iamai_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 new file mode 100644 index 0000000..3e76c6b --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_dice/__init__.py @@ -0,0 +1,51 @@ +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 new file mode 100644 index 0000000..a8c6700 --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_dice/config.py @@ -0,0 +1,13 @@ +from typing import Set + +from plugins.iamai_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 new file mode 100644 index 0000000..fe138f0 --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_echo/__init__.py @@ -0,0 +1,17 @@ +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 new file mode 100644 index 0000000..b1fe445 --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_echo/config.py @@ -0,0 +1,11 @@ +from typing import Set + +from plugins.iamai_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 new file mode 100644 index 0000000..8ef097a --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_luck/__init__.py @@ -0,0 +1,21 @@ +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 new file mode 100644 index 0000000..3983266 --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_luck/config.py @@ -0,0 +1,15 @@ +from typing import Set + +from plugins.iamai_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 new file mode 100644 index 0000000..881faa2 --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_send/__init__.py @@ -0,0 +1,28 @@ +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 new file mode 100644 index 0000000..959822c --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_send/config.py @@ -0,0 +1,15 @@ +from typing import Set, Optional + +from plugins.iamai_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 new file mode 100644 index 0000000..3d6d69c --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_system/__init__.py @@ -0,0 +1,73 @@ +import re +from plugins.HydroRoll_plugin_base import CommandPluginBase +from .config import Config +import psutil +import time +from HydroR.config import GlobalConfig + + +class System(CommandPluginBase[None, Config]): + priority: int = 0 + 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( + 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 new file mode 100644 index 0000000..8571c38 --- /dev/null +++ b/HydroRoll/plugins/HydroRoll_plugin_system/config.py @@ -0,0 +1,11 @@ +from typing import Set + +from plugins.iamai_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/__init__.py b/HydroRoll/plugins/__init__.py deleted file mode 100644 index 6cccc74..0000000 --- a/HydroRoll/plugins/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from Hydro import Dice \ No newline at end of file diff --git a/HydroRoll/plugins/iamai_plugin_base/__init__.py b/HydroRoll/plugins/iamai_plugin_base/__init__.py deleted file mode 100644 index 8fe559b..0000000 --- a/HydroRoll/plugins/iamai_plugin_base/__init__.py +++ /dev/null @@ -1,85 +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: - if self.event.adapter.name != "cqhttp": - return False - if self.event.type != "message_sent": - return False - if self.config.handle_all_message: - return self.str_match(self.event.message.get_plain_text()) - elif self.config.handle_friend_message: - if self.event.message_type == "private": - return self.str_match(self.event.message.get_plain_text()) - 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(self.event.message.get_plain_text()) - 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) diff --git a/HydroRoll/plugins/iamai_plugin_base/config.py b/HydroRoll/plugins/iamai_plugin_base/config.py deleted file mode 100644 index 04924e0..0000000 --- a/HydroRoll/plugins/iamai_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/iamai_plugin_bot_info/__init__.py b/HydroRoll/plugins/iamai_plugin_bot_info/__init__.py deleted file mode 100644 index 3cc869e..0000000 --- a/HydroRoll/plugins/iamai_plugin_bot_info/__init__.py +++ /dev/null @@ -1,20 +0,0 @@ -import re -import sys -import platform -from importlib.metadata import version -from plugins.iamai_plugin_base import CommandPluginBase - -from .config import Config - - -class BotInfo(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, - f"{self.bot.config.Chien['self']['header']} HydroRoll!{self.bot.config.Chien['version']}({self.bot.config.Chien['svn']}) by {self.bot.config.Chien['author']} on Python {'.'.join(map(str, platform.python_version_tuple()[:3]))} with {' & '.join([adapter + '('+version('iamai-adapter-'+adapter)+')' for adapter in dict(self.bot.config.adapter)])} for iamai({version('iamai')})\n{self.bot.config.Chien['self']['info']}") - ) diff --git a/HydroRoll/plugins/iamai_plugin_bot_info/config.py b/HydroRoll/plugins/iamai_plugin_bot_info/config.py deleted file mode 100644 index cf92e81..0000000 --- a/HydroRoll/plugins/iamai_plugin_bot_info/config.py +++ /dev/null @@ -1,11 +0,0 @@ -from typing import Set - -from plugins.iamai_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/iamai_plugin_dice/__init__.py b/HydroRoll/plugins/iamai_plugin_dice/__init__.py deleted file mode 100644 index 396278a..0000000 --- a/HydroRoll/plugins/iamai_plugin_dice/__init__.py +++ /dev/null @@ -1,51 +0,0 @@ -import re -import random - -from iamai.log import logger - -from plugins.iamai_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/iamai_plugin_dice/config.py b/HydroRoll/plugins/iamai_plugin_dice/config.py deleted file mode 100644 index a8c6700..0000000 --- a/HydroRoll/plugins/iamai_plugin_dice/config.py +++ /dev/null @@ -1,13 +0,0 @@ -from typing import Set - -from plugins.iamai_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/iamai_plugin_echo/__init__.py b/HydroRoll/plugins/iamai_plugin_echo/__init__.py deleted file mode 100644 index 98cfa39..0000000 --- a/HydroRoll/plugins/iamai_plugin_echo/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -import re - -from plugins.iamai_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/iamai_plugin_echo/config.py b/HydroRoll/plugins/iamai_plugin_echo/config.py deleted file mode 100644 index b1fe445..0000000 --- a/HydroRoll/plugins/iamai_plugin_echo/config.py +++ /dev/null @@ -1,11 +0,0 @@ -from typing import Set - -from plugins.iamai_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/iamai_plugin_luck/__init__.py b/HydroRoll/plugins/iamai_plugin_luck/__init__.py deleted file mode 100644 index f56019b..0000000 --- a/HydroRoll/plugins/iamai_plugin_luck/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -import re -import time -import random - -from plugins.iamai_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/iamai_plugin_luck/config.py b/HydroRoll/plugins/iamai_plugin_luck/config.py deleted file mode 100644 index 3983266..0000000 --- a/HydroRoll/plugins/iamai_plugin_luck/config.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import Set - -from plugins.iamai_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/iamai_plugin_reply/__init__.py b/HydroRoll/plugins/iamai_plugin_reply/__init__.py deleted file mode 100644 index 6835ca8..0000000 --- a/HydroRoll/plugins/iamai_plugin_reply/__init__.py +++ /dev/null @@ -1,43 +0,0 @@ -import re -import json - -from plugins.iamai_plugin_base import BasePlugin - -from .config import Config - - -class Reply(BasePlugin[None, Config]): - priority: int = 1 - Config = Config - - def __post_init__(self): - with open(self.config.data_file, "r",encoding="utf-8") as fp: - if self.config.data_type == "json": - json_data = json.load(fp) - else: - raise ValueError(f"data_type must be json, not {self.config.data_type}") - self.rule_to_message = { - item["rule"]: item["message"] - for item in json_data - if isinstance(item, dict) - and "rule" in item.keys() - and "message" in item.keys() - } - - async def handle(self) -> None: - msg = self.rule_to_message[self.msg_match.re.pattern] - if isinstance(msg, str): - await self.event.reply(self.format_str(msg, self.msg_match.string)) - else: - await self.event.reply(msg) - - def str_match(self, msg_str: str) -> bool: - msg_str = msg_str.strip() - for rule in self.rule_to_message.keys(): - msg_match = re.fullmatch( - rule, msg_str, flags=re.I if self.config.ignore_case else 0 - ) - if msg_match: - self.msg_match = msg_match - return bool(self.msg_match) - return False diff --git a/HydroRoll/plugins/iamai_plugin_reply/config.py b/HydroRoll/plugins/iamai_plugin_reply/config.py deleted file mode 100644 index e024fb6..0000000 --- a/HydroRoll/plugins/iamai_plugin_reply/config.py +++ /dev/null @@ -1,11 +0,0 @@ -from plugins.iamai_plugin_base import CommandPluginConfig - - -class Config(CommandPluginConfig): - __config_name__ = "plugin_reply" - data_type: str = "json" - """数据类型,目前只支持 json。""" - data_file: str = "data/reply_data.json" - """数据文件位置。""" - ignore_case: bool = True - """是否忽略大小写,默认为 True。""" diff --git a/HydroRoll/plugins/iamai_plugin_send/__init__.py b/HydroRoll/plugins/iamai_plugin_send/__init__.py deleted file mode 100644 index 4e4a516..0000000 --- a/HydroRoll/plugins/iamai_plugin_send/__init__.py +++ /dev/null @@ -1,28 +0,0 @@ -import re - -from plugins.iamai_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/iamai_plugin_send/config.py b/HydroRoll/plugins/iamai_plugin_send/config.py deleted file mode 100644 index 959822c..0000000 --- a/HydroRoll/plugins/iamai_plugin_send/config.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import Set, Optional - -from plugins.iamai_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/iamai_plugin_system_info/__init__.py b/HydroRoll/plugins/iamai_plugin_system_info/__init__.py deleted file mode 100644 index 31fdf63..0000000 --- a/HydroRoll/plugins/iamai_plugin_system_info/__init__.py +++ /dev/null @@ -1,41 +0,0 @@ -import re -import sys -from plugins.iamai_plugin_base import CommandPluginBase -from .config import Config -import psutil -import time - - -class SystemInfo(CommandPluginBase[None, Config]): - priority: int = 0 - Config = Config - - def __post_init__(self): - self.re_pattern = re.compile(r"(?P.*)", flags=re.I) - - def get_system_info(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"HydroRoll!{self.bot.config.Chien['version']}({self.bot.config.Chien['svn']}) built in Python {sys.version}\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_info = self.get_system_info() - message_str = f"{system_info}" - await self.event.reply( - self.format_str(self.config.message_str,message_str) - ) \ No newline at end of file diff --git a/HydroRoll/plugins/iamai_plugin_system_info/config.py b/HydroRoll/plugins/iamai_plugin_system_info/config.py deleted file mode 100644 index 8571c38..0000000 --- a/HydroRoll/plugins/iamai_plugin_system_info/config.py +++ /dev/null @@ -1,11 +0,0 @@ -from typing import Set - -from plugins.iamai_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.py b/HydroRoll/plugins/plugin.py deleted file mode 100644 index 6cccc74..0000000 --- a/HydroRoll/plugins/plugin.py +++ /dev/null @@ -1 +0,0 @@ -from Hydro import Dice \ No newline at end of file diff --git a/data.csv b/data.csv new file mode 100644 index 0000000..d06fcc3 --- /dev/null +++ b/data.csv @@ -0,0 +1,4 @@ +name,age,email +Alice,18,alice@example.com +Bob,20,bob@example.com +Charlie,22,charlie@example.com diff --git a/test.py b/test.py new file mode 100644 index 0000000..882ed9a --- /dev/null +++ b/test.py @@ -0,0 +1,18 @@ +import random + +# 生成一个随机整数 +num = random.randint(1, 10) +print(num) + +# 从序列中随机选择一个元素 +fruits = ['apple', 'banana', 'orange'] +fruit = random.choice(fruits) +print(fruit) + +# 打乱序列中元素的顺序 +random.shuffle(fruits) +print(fruits) + +# 生成一个 0 到 1 之间的随机小数 +r = random.uniform(1,2) +print('%.2f' % r) diff --git a/test/config.toml b/test/config.toml new file mode 100644 index 0000000..55d49a5 --- /dev/null +++ b/test/config.toml @@ -0,0 +1,28 @@ +[HydroRoll] +version = "v0.1.0" +svn = "1" +author = "简律纯" + +[HydroRoll.self] +header = "Hydro系[1]号" +# info = "一只水系骰子..." + + +[bot] +plugins = [] +plugin_dirs = ["plugins"] +adapters = ["iamai.adapter.cqhttp","iamai.adapter.apscheduler"] + +[bot.log] +level = "INFO" +verbose_exception = true + +[adapter.cqhttp] +adapter_type = "reverse-ws" +host = "127.0.0.1" +port = 8080 +url = "/cqhttp/ws" +show_raw = false + +[adapter.apscheduler] +scheduler_config = { "apscheduler.timezone" = "Asia/Shanghai" } \ No newline at end of file diff --git a/test/data/bot_info.json b/test/data/bot_info.json new file mode 100644 index 0000000..7890022 --- /dev/null +++ b/test/data/bot_info.json @@ -0,0 +1,3 @@ +{ + nick : "本大魔王" +} \ No newline at end of file diff --git a/test/data/reply_data.json b/test/data/reply_data.json new file mode 100644 index 0000000..e4977ee --- /dev/null +++ b/test/data/reply_data.json @@ -0,0 +1,17 @@ +[ + { + "rule": "hello", + "message": "你好!" + }, + { + "rule": "test", + "message": [ + { + "type": "text", + "data": { + "text": "测试" + } + } + ] + } +] \ No newline at end of file diff --git a/test/main.py b/test/main.py new file mode 100644 index 0000000..1e9b864 --- /dev/null +++ b/test/main.py @@ -0,0 +1,6 @@ +from HydroR import Bot + +bot = Bot(hot_reload=True) + +if __name__ == "__main__": + bot.run() diff --git a/test/plugins/iamai_plugin_base/__init__.py b/test/plugins/iamai_plugin_base/__init__.py new file mode 100644 index 0000000..8fe559b --- /dev/null +++ b/test/plugins/iamai_plugin_base/__init__.py @@ -0,0 +1,85 @@ +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: + if self.event.adapter.name != "cqhttp": + return False + if self.event.type != "message_sent": + return False + if self.config.handle_all_message: + return self.str_match(self.event.message.get_plain_text()) + elif self.config.handle_friend_message: + if self.event.message_type == "private": + return self.str_match(self.event.message.get_plain_text()) + 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(self.event.message.get_plain_text()) + 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) diff --git a/test/plugins/iamai_plugin_base/config.py b/test/plugins/iamai_plugin_base/config.py new file mode 100644 index 0000000..04924e0 --- /dev/null +++ b/test/plugins/iamai_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/test/plugins/iamai_plugin_reply/__init__.py b/test/plugins/iamai_plugin_reply/__init__.py new file mode 100644 index 0000000..6835ca8 --- /dev/null +++ b/test/plugins/iamai_plugin_reply/__init__.py @@ -0,0 +1,43 @@ +import re +import json + +from plugins.iamai_plugin_base import BasePlugin + +from .config import Config + + +class Reply(BasePlugin[None, Config]): + priority: int = 1 + Config = Config + + def __post_init__(self): + with open(self.config.data_file, "r",encoding="utf-8") as fp: + if self.config.data_type == "json": + json_data = json.load(fp) + else: + raise ValueError(f"data_type must be json, not {self.config.data_type}") + self.rule_to_message = { + item["rule"]: item["message"] + for item in json_data + if isinstance(item, dict) + and "rule" in item.keys() + and "message" in item.keys() + } + + async def handle(self) -> None: + msg = self.rule_to_message[self.msg_match.re.pattern] + if isinstance(msg, str): + await self.event.reply(self.format_str(msg, self.msg_match.string)) + else: + await self.event.reply(msg) + + def str_match(self, msg_str: str) -> bool: + msg_str = msg_str.strip() + for rule in self.rule_to_message.keys(): + msg_match = re.fullmatch( + rule, msg_str, flags=re.I if self.config.ignore_case else 0 + ) + if msg_match: + self.msg_match = msg_match + return bool(self.msg_match) + return False diff --git a/test/plugins/iamai_plugin_reply/config.py b/test/plugins/iamai_plugin_reply/config.py new file mode 100644 index 0000000..e024fb6 --- /dev/null +++ b/test/plugins/iamai_plugin_reply/config.py @@ -0,0 +1,11 @@ +from plugins.iamai_plugin_base import CommandPluginConfig + + +class Config(CommandPluginConfig): + __config_name__ = "plugin_reply" + data_type: str = "json" + """数据类型,目前只支持 json。""" + data_file: str = "data/reply_data.json" + """数据文件位置。""" + ignore_case: bool = True + """是否忽略大小写,默认为 True。""" -- cgit v1.2.3-70-g09d2