1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
import pytz
from datetime import datetime
from ATRI.message import MessageBuilder
from ATRI.exceptions import ThesaurusError
from .db import DBForTS, DBForTAL
from .db import ThesaurusStoragor
class ThesaurusManager:
async def __add_item(self, _id: str, group_id: int, is_main: bool = False):
if is_main:
try:
async with DBForTS() as db:
await db.add_item(_id, group_id)
except Exception:
raise ThesaurusError(f"添加词库(ts)数据失败 目标词id: {_id}")
else:
try:
async with DBForTAL() as db:
await db.add_item(_id, group_id)
except Exception:
raise ThesaurusError(f"添加词库(tal)数据失败 目标词id: {_id}")
async def update_item(
self, _id: str, group_id: int, update_map: dict, is_main: bool = False
):
if is_main:
try:
async with DBForTS() as db:
await db.update_item(_id, group_id, update_map)
except Exception:
raise ThesaurusError(f"更新词库(ts)数据失败 目标词id: {_id}")
else:
try:
async with DBForTAL() as db:
await db.update_item(_id, group_id, update_map)
except Exception:
raise ThesaurusError(f"更新词库(tal)数据失败 目标词id: {_id}")
async def __del_item(self, _id: str, group_id: int, is_main: bool = False):
if is_main:
try:
async with DBForTS() as db:
await db.del_item({"_id": _id, "group_id": group_id})
except Exception:
raise ThesaurusError(f"删除词库(ts)数据失败 目标词id: {_id}")
else:
try:
async with DBForTAL() as db:
await db.del_item({"_id": _id, "group_id": group_id})
except Exception:
raise ThesaurusError(f"删除词库(tal)数据失败 目标词id: {_id}")
async def get_item_list(self, query_map: dict, is_main: bool = False) -> list:
if is_main:
try:
async with DBForTS() as db:
return await db.get_item_list(query_map)
except Exception:
raise ThesaurusError("获取词库(ts)列表数据失败")
else:
try:
async with DBForTAL() as db:
return await db.get_item_list(query_map)
except Exception:
raise ThesaurusError("获取词库(tal)列表数据失败")
async def get_all_items(self, is_main: bool = False) -> list:
if is_main:
try:
async with DBForTS() as db:
return await db.get_all_items()
except Exception:
raise ThesaurusError("获取全部词库(ts)列表数据失败")
else:
try:
async with DBForTAL() as db:
return await db.get_all_items()
except Exception:
raise ThesaurusError("获取全部词库(tal)列表数据失败")
async def add_item(
self,
_id: str,
is_main: bool,
q: str,
a: list,
need_at: int,
t: str,
group_id: int,
operator: str,
operator_id: int,
is_vote: int,
vote_list: list,
) -> str:
query_result = await self.get_item_list(
{"matcher": q, "group_id": group_id}, is_main
)
if query_result:
await self.del_item(_id, group_id, is_main)
item_info = query_result[0]
return (
MessageBuilder(f"{str() if is_main else '(需审核/投票)'}该词条已存在!!")
.text(f"ID: {item_info._id}")
.text("因此, 此新增词条将被删除")
.done()
)
if t == "全匹配":
m_type = 0
elif t == "模糊匹配":
m_type = 1
else:
m_type = 2
item_meta = {
"matcher": q,
"result": a,
"need_at": need_at,
"m_type": m_type,
"group_id": group_id,
"operator": operator,
"operator_id": operator_id,
"update_time": datetime.now(pytz.timezone("Asia/Shanghai")),
"is_vote": is_vote,
"vote_list": vote_list,
}
await self.__add_item(_id, group_id, is_main)
await self.update_item(
_id,
group_id,
item_meta,
is_main,
)
return f"""{"(需审核/投票)" if not is_main else str()}成功加上新词条 ID: {_id}"""
async def del_item(self, _id: str, group_id: int, is_main: bool):
query_result = await self.get_item_list(
{"_id": _id, "group_id": group_id}, is_main
)
if not query_result:
return f"目标id: {_id} 没有记录呢..."
await self.__del_item(_id, group_id, is_main)
return f"成功删除目标id: {_id} 问答信息"
async def vote(self, _id: str, group_id: int, voter: int):
raw_item_info = await self.get_item_list({"_id": _id, "group_id": group_id})
item_info = raw_item_info[0]
vote_list: list = item_info.vote_list
vote_list.append(voter)
await self.update_item(
_id,
group_id,
{
"vote_list": vote_list,
"update_time": datetime.now(pytz.timezone("Asia/Shanghai")),
},
)
class ThesaurusListener:
async def get_item_by_id(self, _id: str) -> ThesaurusStoragor:
try:
async with DBForTS() as db:
data = await db.get_item_list({"_id": _id})
except Exception:
raise ThesaurusError(f"获取词库(ts)数据失败 词条ID: {_id}")
return data[0]
async def get_item_list(self, group_id: int):
try:
async with DBForTS() as db:
return await db.get_item_list({"group_id": group_id})
except Exception:
raise ThesaurusError(f"获取词库(ts)数据失败 目标群号: {group_id}")
|