aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/plugins/alicebot_plugin_reply
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-04-18 18:03:02 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-04-18 18:03:02 +0800
commit025020c503ef5b8dc0270197c5b1e6d4e0f8807d (patch)
tree25f8c8d8882647e22ea0acebadafef99306f4c98 /plugins/alicebot_plugin_reply
parent074ad0c914ae0b53ace46ed5d4c24205eacbabaa (diff)
downloadHydroRoll-025020c503ef5b8dc0270197c5b1e6d4e0f8807d.tar.gz
HydroRoll-025020c503ef5b8dc0270197c5b1e6d4e0f8807d.zip
✨本体文件
Diffstat (limited to 'plugins/alicebot_plugin_reply')
-rw-r--r--plugins/alicebot_plugin_reply/__init__.py43
-rw-r--r--plugins/alicebot_plugin_reply/config.py11
2 files changed, 54 insertions, 0 deletions
diff --git a/plugins/alicebot_plugin_reply/__init__.py b/plugins/alicebot_plugin_reply/__init__.py
new file mode 100644
index 0000000..396f276
--- /dev/null
+++ b/plugins/alicebot_plugin_reply/__init__.py
@@ -0,0 +1,43 @@
+import re
+import json
+
+from plugins.alicebot_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") 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/plugins/alicebot_plugin_reply/config.py b/plugins/alicebot_plugin_reply/config.py
new file mode 100644
index 0000000..f4f01f4
--- /dev/null
+++ b/plugins/alicebot_plugin_reply/config.py
@@ -0,0 +1,11 @@
+from plugins.alicebot_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。"""