From 6252090b4e2c7444722f21610c4b6ce6ee111447 Mon Sep 17 00:00:00 2001 From: Soumt Nam Date: Mon, 2 Oct 2023 14:36:36 +0900 Subject: [PATCH] feat: added database module --- .gitignore | 3 +++ minya.py | 2 ++ module_interface.py | 2 +- modules/Ai_tem.py | 1 - modules/Friend.py | 7 +++++-- modules/Minya_DB.py | 31 +++++++++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 modules/Minya_DB.py diff --git a/.gitignore b/.gitignore index 1e3381d..d36cb36 100644 --- a/.gitignore +++ b/.gitignore @@ -178,3 +178,6 @@ poetry.toml pyrightconfig.json # End of https://www.toptal.com/developers/gitignore/api/python,dotenv + +# MinyaDB Module +minyadb diff --git a/minya.py b/minya.py index 66cc8af..7584ae1 100644 --- a/minya.py +++ b/minya.py @@ -59,6 +59,8 @@ class MiNya(Bot): for module in self.__modules: self.__manager.register_module(module) + for module in self.__modules: + module.module_ready() print("") async def on_mention(self, notice: NotificationNote): diff --git a/module_interface.py b/module_interface.py index 41d1fca..6dd0489 100644 --- a/module_interface.py +++ b/module_interface.py @@ -9,7 +9,7 @@ class ModuleInterface: def set_manager(self, manager): self.manager = manager - self.module_ready() + #self.module_ready() def get_execute_pattern(self, regex_pattern): return self.__regex_pattern diff --git a/modules/Ai_tem.py b/modules/Ai_tem.py index 362db0b..b0c413b 100644 --- a/modules/Ai_tem.py +++ b/modules/Ai_tem.py @@ -191,7 +191,6 @@ items = [ '엔터 키', '항아리', '수은', - 'DHMO', '물', '토지', '대륙', diff --git a/modules/Friend.py b/modules/Friend.py index 2fde1bd..2970e48 100644 --- a/modules/Friend.py +++ b/modules/Friend.py @@ -24,11 +24,14 @@ class Friend(ModuleInterface): pass def _get_like(self, userid: str): - pass - + return self._get_db(self.Name, userid) + async def _is_friend_async(self, userid: str): target = await self.manager.bot.user.api.action.get(userid) return target.is_followed and target.is_following def module_ready(self): + self._get_db = self.manager.require("MinyaDB").get_func("get_module_db") + self._set_db = self.manager.require("MinyaDB").get_func("set_module_db") + print("[Friend] Module Ready.") diff --git a/modules/Minya_DB.py b/modules/Minya_DB.py new file mode 100644 index 0000000..75953c6 --- /dev/null +++ b/modules/Minya_DB.py @@ -0,0 +1,31 @@ +from module_interface import ModuleInterface +from pupdb.core import PupDB +import os + +from mipac.models import Note +class MinyaDB(ModuleInterface): + def __init__(self): + super().__init__() + self.name = "MinyaDB" + self.regex_pattern = None + + if not os.path.isdir("./minyadb"): + os.mkdir("./minyadb") + + self.funcs["get_module_db"] = self._get_module_db + self.funcs["set_module_db"] = self._set_module_db + print("[MinyaDB] Database system for modules, MinyaDB loaded.") + + async def execute_module(self, ctx: Note): + pass + + def _get_module_db(self, module_name: str, key: str): + db = PupDB(f"./minyadb/{module_name}.db") + db.get(key) + + def _set_module_db(self, module_name: str, key: str, data): + db = PupDB(f"./minyadb/{module_name}.db") + db.set(key, data) + + def module_ready(self): + print("[MinyaDB] Module Ready.")