summaryrefslogtreecommitdiff
path: root/ATRI/plugins/chat/data_source.py
diff options
context:
space:
mode:
Diffstat (limited to 'ATRI/plugins/chat/data_source.py')
-rw-r--r--ATRI/plugins/chat/data_source.py39
1 files changed, 19 insertions, 20 deletions
diff --git a/ATRI/plugins/chat/data_source.py b/ATRI/plugins/chat/data_source.py
index 74dde96..15c495d 100644
--- a/ATRI/plugins/chat/data_source.py
+++ b/ATRI/plugins/chat/data_source.py
@@ -15,23 +15,22 @@ __doc__ = """
好像有点涩?(偏文爱,需at
"""
-CHAT_PATH = Path(".") / "ATRI" / "data" / "database" / "chat"
+CHAT_PATH = Path(".") / "data" / "database" / "chat"
os.makedirs(CHAT_PATH, exist_ok=True)
KIMO_URL = "https://cdn.jsdelivr.net/gh/Kyomotoi/AnimeThesaurus/data.json"
class Chat(Service):
+
def __init__(self):
- Service.__init__(
- self, "闲聊", __doc__, rule=to_bot() & is_in_service("闲聊"), priority=5
- )
-
+ Service.__init__(self, "闲聊", __doc__, rule=to_bot() & is_in_service("闲聊"), priority=5)
+
@staticmethod
async def _request(url: str) -> dict:
res = await request.get(url)
data = await res.json()
return data
-
+
@classmethod
async def _generate_data(cls) -> None:
file_name = "kimo.json"
@@ -45,18 +44,18 @@ class Chat(Service):
log.info("生成完成")
except WriteError:
raise WriteError("Writing kimo words failed!")
-
+
@classmethod
async def _load_data(cls) -> dict:
file_name = "kimo.json"
path = CHAT_PATH / file_name
if not path.is_file():
await cls._generate_data()
-
+
with open(path, "r", encoding="utf-8") as r:
data = json.loads(r.read())
return data
-
+
@classmethod
async def update_data(cls) -> None:
log.info("更新闲聊词库ing...")
@@ -64,17 +63,17 @@ class Chat(Service):
path = CHAT_PATH / file_name
if not path.is_file():
await cls._generate_data()
-
+
updata_data = await cls._request(KIMO_URL)
data = json.loads(path.read_bytes())
for i in updata_data:
if i not in data:
data[i] = updata_data[i]
-
+
with open(path, "w", encoding="utf-8") as w:
w.write(json.dumps(data, indent=4))
log.info("闲聊词库更新完成")
-
+
@staticmethod
def name_is(user_id: str, new_name: str):
file_name = "users.json"
@@ -83,7 +82,7 @@ class Chat(Service):
with open(path, "w", encoding="utf-8") as w:
w.write(json.dumps({}))
data = {}
-
+
data = json.loads(path.read_bytes())
data[user_id] = new_name
try:
@@ -91,7 +90,7 @@ class Chat(Service):
w.write(json.dumps(data, indent=4))
except ReadFileError:
raise ReadFileError("Update user name failed!")
-
+
@staticmethod
def load_name(user_id: str) -> str:
file_name = "users.json"
@@ -100,21 +99,21 @@ class Chat(Service):
with open(path, "w", encoding="utf-8") as w:
w.write(json.dumps({}))
return "你"
-
+
data = json.loads(path.read_bytes())
try:
result = data[user_id]
except BaseException:
result = "你"
return result
-
+
@classmethod
async def deal(cls, msg: str, user_id: str) -> str:
keywords = posseg.lcut(msg)
shuffle(keywords)
-
+
data = await cls._load_data()
-
+
repo = str()
for i in keywords:
a = i.word
@@ -126,14 +125,14 @@ class Chat(Service):
pass
if a in data:
repo = data.get(a, str())
-
+
if not repo:
temp_data = list(data)
shuffle(temp_data)
for i in temp_data:
if i in msg:
repo = data.get(i, str())
-
+
a = choice(repo) if type(repo) is list else repo
user_name = cls.load_name(user_id)
repo = a.replace("你", user_name)