aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/plugins/iamai_plugin_reply/__init__.py
diff options
context:
space:
mode:
authorHsiangNianian <admin@jyunko.cn>2023-04-22 19:51:01 +0800
committerHsiangNianian <admin@jyunko.cn>2023-04-22 19:51:01 +0800
commitdb74ade0234a40c2120ad5f2a41bee50ce13de02 (patch)
tree6f66ffb0fd2b8f27df54569161586c2d8a436b96 /test/plugins/iamai_plugin_reply/__init__.py
parent30cde518ee38f63c743c8fe0e90dbad1a93f2cf9 (diff)
downloadHydroRoll-db74ade0234a40c2120ad5f2a41bee50ce13de02.tar.gz
HydroRoll-db74ade0234a40c2120ad5f2a41bee50ce13de02.zip
🤐
Diffstat (limited to 'test/plugins/iamai_plugin_reply/__init__.py')
-rw-r--r--test/plugins/iamai_plugin_reply/__init__.py43
1 files changed, 43 insertions, 0 deletions
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