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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from os import getenv
|
3 |
+
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
import datetime
|
7 |
+
import time
|
8 |
+
|
9 |
+
from motor import motor_asyncio
|
10 |
+
from motor.core import AgnosticClient
|
11 |
+
|
12 |
+
from logger import LOGS
|
13 |
+
|
14 |
+
load_dotenv()
|
15 |
+
MONGO_URL = os.environ["MONGO_URL"]
|
16 |
+
|
17 |
+
class Database:
|
18 |
+
def __init__(self, uri: str) -> None:
|
19 |
+
self.client: AgnosticClient = motor_asyncio.AsyncIOMotorClient(uri)
|
20 |
+
self.db = self.client["Akeno"]
|
21 |
+
|
22 |
+
self.afk = self.db["afk"]
|
23 |
+
self.antiflood = self.db["antiflood"]
|
24 |
+
self.autopost = self.db["autopost"]
|
25 |
+
self.blacklist = self.db["blacklist"]
|
26 |
+
self.echo = self.db["echo"]
|
27 |
+
self.env = self.db["env"]
|
28 |
+
self.filter = self.db["filter"]
|
29 |
+
self.forcesub = self.db["forcesub"]
|
30 |
+
self.gachabots = self.db["gachabots"]
|
31 |
+
self.cohere = self.db["cohere"]
|
32 |
+
self.chatbot = self.db["chatbot"]
|
33 |
+
self.backup_chatbot = self.db["openaichat"]
|
34 |
+
self.antiarabic = self.db["antiarabic"]
|
35 |
+
self.antinsfw = self.db["antinsfw"]
|
36 |
+
self.gban = self.db["gban"]
|
37 |
+
self.gmute = self.db["gmute"]
|
38 |
+
self.greetings = self.db["greetings"]
|
39 |
+
self.mute = self.db["mute"]
|
40 |
+
self.pmpermit = self.db["pmpermit"]
|
41 |
+
self.session = self.db["session"]
|
42 |
+
self.snips = self.db["snips"]
|
43 |
+
self.stan_users = self.db["stan_users"]
|
44 |
+
|
45 |
+
async def connect(self):
|
46 |
+
try:
|
47 |
+
await self.client.admin.command("ping")
|
48 |
+
LOGS.info(f"Database Connection Established!")
|
49 |
+
except Exception as e:
|
50 |
+
LOGS.info(f"DatabaseErr: {e} ")
|
51 |
+
quit(1)
|
52 |
+
|
53 |
+
async def _close(self):
|
54 |
+
await self.client.close()
|
55 |
+
|
56 |
+
def get_datetime(self) -> str:
|
57 |
+
return datetime.datetime.now().strftime("%d/%m/%Y - %H:%M")
|
58 |
+
|
59 |
+
async def set_env(self, name: str, value: str) -> None:
|
60 |
+
await self.env.update_one(
|
61 |
+
{"name": name}, {"$set": {"value": value}}, upsert=True
|
62 |
+
)
|
63 |
+
|
64 |
+
async def get_env(self, name: str) -> str | None:
|
65 |
+
if await self.is_env(name):
|
66 |
+
data = await self.env.find_one({"name": name})
|
67 |
+
return data["value"]
|
68 |
+
return None
|
69 |
+
|
70 |
+
async def rm_env(self, name: str) -> None:
|
71 |
+
await self.env.delete_one({"name": name})
|
72 |
+
|
73 |
+
async def is_env(self, name: str) -> bool:
|
74 |
+
if await self.env.find_one({"name": name}):
|
75 |
+
return True
|
76 |
+
return False
|
77 |
+
|
78 |
+
async def get_all_env(self) -> list:
|
79 |
+
return [i async for i in self.env.find({})]
|
80 |
+
|
81 |
+
async def is_stan(self, client: int, user_id: int) -> bool:
|
82 |
+
if await self.stan_users.find_one({"client": client, "user_id": user_id}):
|
83 |
+
return True
|
84 |
+
return False
|
85 |
+
|
86 |
+
async def add_stan(self, client: int, user_id: int) -> bool:
|
87 |
+
if await self.is_stan(client, user_id):
|
88 |
+
return False
|
89 |
+
await self.stan_users.insert_one(
|
90 |
+
{"client": client, "user_id": user_id, "date": self.get_datetime()}
|
91 |
+
)
|
92 |
+
return True
|
93 |
+
|
94 |
+
async def rm_stan(self, client: int, user_id: int) -> bool:
|
95 |
+
if not await self.is_stan(client, user_id):
|
96 |
+
return False
|
97 |
+
await self.stan_users.delete_one({"client": client, "user_id": user_id})
|
98 |
+
return True
|
99 |
+
|
100 |
+
async def get_stans(self, client: int) -> list:
|
101 |
+
return [i async for i in self.stan_users.find({"client": client})]
|
102 |
+
|
103 |
+
async def get_all_stans(self) -> list:
|
104 |
+
return [i async for i in self.stan_users.find({})]
|
105 |
+
|
106 |
+
async def is_session(self, user_id: int) -> bool:
|
107 |
+
if await self.session.find_one({"user_id": user_id}):
|
108 |
+
return True
|
109 |
+
return False
|
110 |
+
|
111 |
+
async def update_session(self, user_id: int, session: str) -> None:
|
112 |
+
await self.session.update_one(
|
113 |
+
{"user_id": user_id},
|
114 |
+
{"$set": {"session": session, "date": self.get_datetime()}},
|
115 |
+
upsert=True,
|
116 |
+
)
|
117 |
+
|
118 |
+
async def rm_session(self, user_id: int) -> None:
|
119 |
+
await self.session.delete_one({"user_id": user_id})
|
120 |
+
|
121 |
+
async def get_session(self, user_id: int):
|
122 |
+
if not await self.is_session(user_id):
|
123 |
+
return False
|
124 |
+
data = await self.session.find_one({"user_id": user_id})
|
125 |
+
return data
|
126 |
+
|
127 |
+
async def get_all_sessions(self) -> list:
|
128 |
+
return [i async for i in self.session.find({})]
|
129 |
+
|
130 |
+
async def is_gbanned(self, user_id: int) -> bool:
|
131 |
+
if await self.gban.find_one({"user_id": user_id}):
|
132 |
+
return True
|
133 |
+
return False
|
134 |
+
|
135 |
+
async def add_gban(self, user_id: int, reason: str) -> bool:
|
136 |
+
if await self.is_gbanned(user_id):
|
137 |
+
return False
|
138 |
+
await self.gban.insert_one(
|
139 |
+
{"user_id": user_id, "reason": reason, "date": self.get_datetime()}
|
140 |
+
)
|
141 |
+
return True
|
142 |
+
|
143 |
+
async def rm_gban(self, user_id: int):
|
144 |
+
if not await self.is_gbanned(user_id):
|
145 |
+
return None
|
146 |
+
reason = (await self.gban.find_one({"user_id": user_id}))["reason"]
|
147 |
+
await self.gban.delete_one({"user_id": user_id})
|
148 |
+
return reason
|
149 |
+
|
150 |
+
async def get_gban(self) -> list:
|
151 |
+
return [i async for i in self.gban.find({})]
|
152 |
+
|
153 |
+
async def get_gban_user(self, user_id: int) -> dict | None:
|
154 |
+
if not await self.is_gbanned(user_id):
|
155 |
+
return None
|
156 |
+
return await self.gban.find_one({"user_id": user_id})
|
157 |
+
|
158 |
+
async def is_gmuted(self, user_id: int) -> bool:
|
159 |
+
if await self.gmute.find_one({"user_id": user_id}):
|
160 |
+
return True
|
161 |
+
return False
|
162 |
+
|
163 |
+
async def add_gmute(self, user_id: int, reason: str) -> bool:
|
164 |
+
if await self.is_gmuted(user_id):
|
165 |
+
return False
|
166 |
+
await self.gmute.insert_one(
|
167 |
+
{"user_id": user_id, "reason": reason, "date": self.get_datetime()}
|
168 |
+
)
|
169 |
+
return True
|
170 |
+
|
171 |
+
async def rm_gmute(self, user_id: int):
|
172 |
+
if not await self.is_gmuted(user_id):
|
173 |
+
return None
|
174 |
+
reason = (await self.gmute.find_one({"user_id": user_id}))["reason"]
|
175 |
+
await self.gmute.delete_one({"user_id": user_id})
|
176 |
+
return reason
|
177 |
+
|
178 |
+
async def get_gmute(self) -> list:
|
179 |
+
return [i async for i in self.gmute.find({})]
|
180 |
+
|
181 |
+
async def add_mute(self, client: int, user_id: int, chat_id: int, reason: str):
|
182 |
+
await self.mute.update_one(
|
183 |
+
{"client": client, "user_id": user_id, "chat_id": chat_id},
|
184 |
+
{"$set": {"reason": reason, "date": self.get_datetime()}},
|
185 |
+
upsert=True,
|
186 |
+
)
|
187 |
+
|
188 |
+
async def rm_mute(self, client: int, user_id: int, chat_id: int) -> str:
|
189 |
+
reason = (await self.get_mute(client, user_id, chat_id))["reason"]
|
190 |
+
await self.mute.delete_one({"client": client, "user_id": user_id, "chat_id": chat_id})
|
191 |
+
return reason
|
192 |
+
|
193 |
+
async def is_muted(self, client: int, user_id: int, chat_id: int) -> bool:
|
194 |
+
if await self.get_mute(client, user_id, chat_id):
|
195 |
+
return True
|
196 |
+
return False
|
197 |
+
|
198 |
+
async def get_mute(self, client: int, user_id: int, chat_id: int):
|
199 |
+
data = await self.mute.find_one({"client": client, "user_id": user_id, "chat_id": chat_id})
|
200 |
+
return data
|
201 |
+
|
202 |
+
async def set_afk(
|
203 |
+
self, user_id: int, reason: str, media: int, media_type: str
|
204 |
+
) -> None:
|
205 |
+
await self.afk.update_one(
|
206 |
+
{"user_id": user_id},
|
207 |
+
{
|
208 |
+
"$set": {
|
209 |
+
"reason": reason,
|
210 |
+
"time": time.time(),
|
211 |
+
"media": media,
|
212 |
+
"media_type": media_type,
|
213 |
+
}
|
214 |
+
},
|
215 |
+
upsert=True,
|
216 |
+
)
|
217 |
+
|
218 |
+
async def get_afk(self, user_id: int):
|
219 |
+
data = await self.afk.find_one({"user_id": user_id})
|
220 |
+
return data
|
221 |
+
|
222 |
+
async def is_afk(self, user_id: int) -> bool:
|
223 |
+
if await self.afk.find_one({"user_id": user_id}):
|
224 |
+
return True
|
225 |
+
return False
|
226 |
+
|
227 |
+
async def rm_afk(self, user_id: int) -> None:
|
228 |
+
await self.afk.delete_one({"user_id": user_id})
|
229 |
+
|
230 |
+
async def set_flood(self, client_chat: tuple[int, int], settings: dict):
|
231 |
+
await self.antiflood.update_one(
|
232 |
+
{"client": client_chat[0], "chat": client_chat[1]},
|
233 |
+
{"$set": settings},
|
234 |
+
upsert=True,
|
235 |
+
)
|
236 |
+
|
237 |
+
async def get_flood(self, client_chat: tuple[int, int]):
|
238 |
+
data = await self.antiflood.find_one(
|
239 |
+
{"client": client_chat[0], "chat": client_chat[1]}
|
240 |
+
)
|
241 |
+
return data or {}
|
242 |
+
|
243 |
+
async def is_flood(self, client_chat: tuple[int, int]) -> bool:
|
244 |
+
data = await self.get_flood(client_chat)
|
245 |
+
|
246 |
+
if not data:
|
247 |
+
return False
|
248 |
+
|
249 |
+
if data["limit"] == 0:
|
250 |
+
return False
|
251 |
+
|
252 |
+
return True
|
253 |
+
|
254 |
+
async def get_all_floods(self) -> list:
|
255 |
+
return [i async for i in self.antiflood.find({})]
|
256 |
+
|
257 |
+
async def set_autopost(self, client: int, from_channel: int, to_channel: int):
|
258 |
+
await self.autopost.update_one(
|
259 |
+
{"client": client},
|
260 |
+
{
|
261 |
+
"$push": {
|
262 |
+
"autopost": {
|
263 |
+
"from_channel": from_channel,
|
264 |
+
"to_channel": to_channel,
|
265 |
+
"date": self.get_datetime(),
|
266 |
+
}
|
267 |
+
}
|
268 |
+
},
|
269 |
+
upsert=True,
|
270 |
+
)
|
271 |
+
|
272 |
+
async def get_autopost(self, client: int, from_channel: int):
|
273 |
+
data = await self.autopost.find_one(
|
274 |
+
{
|
275 |
+
"client": client,
|
276 |
+
"autopost": {"$elemMatch": {"from_channel": from_channel}},
|
277 |
+
}
|
278 |
+
)
|
279 |
+
return data
|
280 |
+
|
281 |
+
async def is_autopost(
|
282 |
+
self, client: int, from_channel: int, to_channel: int = None
|
283 |
+
) -> bool:
|
284 |
+
if to_channel:
|
285 |
+
data = await self.autopost.find_one(
|
286 |
+
{
|
287 |
+
"client": client,
|
288 |
+
"autopost": {
|
289 |
+
"$elemMatch": {
|
290 |
+
"from_channel": from_channel,
|
291 |
+
"to_channel": to_channel,
|
292 |
+
}
|
293 |
+
},
|
294 |
+
}
|
295 |
+
)
|
296 |
+
else:
|
297 |
+
data = await self.autopost.find_one(
|
298 |
+
{
|
299 |
+
"client": client,
|
300 |
+
"autopost": {"$elemMatch": {"from_channel": from_channel}},
|
301 |
+
}
|
302 |
+
)
|
303 |
+
return True if data else False
|
304 |
+
|
305 |
+
async def rm_autopost(self, client: int, from_channel: int, to_channel: int):
|
306 |
+
await self.autopost.update_one(
|
307 |
+
{"client": client},
|
308 |
+
{
|
309 |
+
"$pull": {
|
310 |
+
"autopost": {
|
311 |
+
"from_channel": from_channel,
|
312 |
+
"to_channel": to_channel,
|
313 |
+
}
|
314 |
+
}
|
315 |
+
},
|
316 |
+
)
|
317 |
+
|
318 |
+
async def get_all_autoposts(self, client: int) -> list:
|
319 |
+
return [i async for i in self.autopost.find({"client": client})]
|
320 |
+
|
321 |
+
async def add_blacklist(self, client: int, chat: int, blacklist: str):
|
322 |
+
await self.blacklist.update_one(
|
323 |
+
{"client": client, "chat": chat},
|
324 |
+
{"$push": {"blacklist": blacklist}},
|
325 |
+
upsert=True,
|
326 |
+
)
|
327 |
+
|
328 |
+
async def rm_blacklist(self, client: int, chat: int, blacklist: str):
|
329 |
+
await self.blacklist.update_one(
|
330 |
+
{"client": client, "chat": chat},
|
331 |
+
{"$pull": {"blacklist": blacklist}},
|
332 |
+
)
|
333 |
+
|
334 |
+
async def is_blacklist(self, client: int, chat: int, blacklist: str) -> bool:
|
335 |
+
blacklists = await self.get_all_blacklists(client, chat)
|
336 |
+
if blacklist in blacklists:
|
337 |
+
return True
|
338 |
+
return False
|
339 |
+
|
340 |
+
async def get_all_blacklists(self, client: int, chat: int) -> list:
|
341 |
+
data = await self.blacklist.find_one({"client": client, "chat": chat})
|
342 |
+
|
343 |
+
if not data:
|
344 |
+
return []
|
345 |
+
|
346 |
+
return data["blacklist"]
|
347 |
+
|
348 |
+
async def get_blacklist_clients(self) -> list:
|
349 |
+
return [i async for i in self.blacklist.find({})]
|
350 |
+
|
351 |
+
async def set_echo(self, client: int, chat: int, user: int):
|
352 |
+
await self.echo.update_one(
|
353 |
+
{"client": client, "chat": chat},
|
354 |
+
{"$push": {"echo": user}},
|
355 |
+
upsert=True,
|
356 |
+
)
|
357 |
+
|
358 |
+
async def rm_echo(self, client: int, chat: int, user: int):
|
359 |
+
await self.echo.update_one(
|
360 |
+
{"client": client, "chat": chat},
|
361 |
+
{"$pull": {"echo": user}},
|
362 |
+
)
|
363 |
+
|
364 |
+
async def is_echo(self, client: int, chat: int, user: int) -> bool:
|
365 |
+
data = await self.get_all_echo(client, chat)
|
366 |
+
if user in data:
|
367 |
+
return True
|
368 |
+
return False
|
369 |
+
|
370 |
+
async def get_all_echo(self, client: int, chat: int) -> list:
|
371 |
+
data = await self.echo.find_one({"client": client, "chat": chat})
|
372 |
+
|
373 |
+
if not data:
|
374 |
+
return []
|
375 |
+
|
376 |
+
return data["echo"]
|
377 |
+
|
378 |
+
async def set_filter(self, client: int, chat: int, keyword: str, msgid: int):
|
379 |
+
await self.filter.update_one(
|
380 |
+
{"client": client, "chat": chat},
|
381 |
+
{"$push": {"filter": {"keyword": keyword, "msgid": msgid}}},
|
382 |
+
upsert=True,
|
383 |
+
)
|
384 |
+
|
385 |
+
async def rm_filter(self, client: int, chat: int, keyword: str):
|
386 |
+
await self.filter.update_one(
|
387 |
+
{"client": client, "chat": chat},
|
388 |
+
{"$pull": {"filter": {"keyword": keyword}}},
|
389 |
+
)
|
390 |
+
|
391 |
+
async def rm_all_filters(self, client: int, chat: int):
|
392 |
+
await self.filter.delete_one({"client": client, "chat": chat})
|
393 |
+
|
394 |
+
async def is_filter(self, client: int, chat: int, keyword: str) -> bool:
|
395 |
+
data = await self.get_filter(client, chat, keyword)
|
396 |
+
return True if data else False
|
397 |
+
|
398 |
+
async def get_filter(self, client: int, chat: int, keyword: str):
|
399 |
+
data = await self.filter.find_one(
|
400 |
+
{
|
401 |
+
"client": client,
|
402 |
+
"chat": chat,
|
403 |
+
"filter": {"$elemMatch": {"keyword": keyword}},
|
404 |
+
}
|
405 |
+
)
|
406 |
+
return data
|
407 |
+
|
408 |
+
async def get_all_filters(self, client: int, chat: int) -> list:
|
409 |
+
data = await self.filter.find_one({"client": client, "chat": chat})
|
410 |
+
|
411 |
+
if not data:
|
412 |
+
return []
|
413 |
+
|
414 |
+
return data["filter"]
|
415 |
+
|
416 |
+
async def set_snip(self, client: int, chat: int, keyword: str, msgid: int):
|
417 |
+
await self.snips.update_one(
|
418 |
+
{"client": client, "chat": chat},
|
419 |
+
{"$push": {"snips": {"keyword": keyword, "msgid": msgid}}},
|
420 |
+
upsert=True,
|
421 |
+
)
|
422 |
+
|
423 |
+
async def rm_snip(self, client: int, chat: int, keyword: str):
|
424 |
+
await self.snips.update_one(
|
425 |
+
{"client": client, "chat": chat},
|
426 |
+
{"$pull": {"snips": {"keyword": keyword}}},
|
427 |
+
)
|
428 |
+
|
429 |
+
async def rm_all_snips(self, client: int, chat: int):
|
430 |
+
await self.snips.delete_one({"client": client, "chat": chat})
|
431 |
+
|
432 |
+
async def is_snip(self, client: int, chat: int, keyword: str) -> bool:
|
433 |
+
data = await self.get_snip(client, chat, keyword)
|
434 |
+
return True if data else False
|
435 |
+
|
436 |
+
async def get_snip(self, client: int, chat: int, keyword: str):
|
437 |
+
data = await self.snips.find_one(
|
438 |
+
{
|
439 |
+
"client": client,
|
440 |
+
"chat": chat,
|
441 |
+
"snips": {"$elemMatch": {"keyword": keyword}},
|
442 |
+
}
|
443 |
+
)
|
444 |
+
return data
|
445 |
+
|
446 |
+
async def get_all_snips(self, client: int, chat: int) -> list:
|
447 |
+
data = await self.snips.find_one({"client": client, "chat": chat})
|
448 |
+
|
449 |
+
if not data:
|
450 |
+
return []
|
451 |
+
|
452 |
+
return data["snips"]
|
453 |
+
|
454 |
+
async def add_pmpermit(self, client: int, user: int):
|
455 |
+
await self.pmpermit.update_one(
|
456 |
+
{"client": client, "user": user},
|
457 |
+
{"$set": {"date": self.get_datetime()}},
|
458 |
+
upsert=True,
|
459 |
+
)
|
460 |
+
|
461 |
+
async def rm_pmpermit(self, client: int, user: int):
|
462 |
+
await self.pmpermit.delete_one({"client": client, "user": user})
|
463 |
+
|
464 |
+
async def is_pmpermit(self, client: int, user: int) -> bool:
|
465 |
+
data = await self.get_pmpermit(client, user)
|
466 |
+
return True if data else False
|
467 |
+
|
468 |
+
async def get_pmpermit(self, client: int, user: int):
|
469 |
+
data = await self.pmpermit.find_one({"client": client, "user": user})
|
470 |
+
return data
|
471 |
+
|
472 |
+
async def get_all_pmpermits(self, client: int) -> list:
|
473 |
+
return [i async for i in self.pmpermit.find({"client": client})]
|
474 |
+
|
475 |
+
async def set_welcome(self, client: int, chat: int, message: int):
|
476 |
+
await self.greetings.update_one(
|
477 |
+
{"client": client, "chat": chat, "welcome": True},
|
478 |
+
{"$set": {"message": message}},
|
479 |
+
upsert=True,
|
480 |
+
)
|
481 |
+
|
482 |
+
async def rm_welcome(self, client: int, chat: int):
|
483 |
+
await self.greetings.delete_one(
|
484 |
+
{"client": client, "chat": chat, "welcome": True}
|
485 |
+
)
|
486 |
+
|
487 |
+
async def is_welcome(self, client: int, chat: int) -> bool:
|
488 |
+
data = await self.get_welcome(client, chat)
|
489 |
+
return True if data else False
|
490 |
+
|
491 |
+
async def get_welcome(self, client: int, chat: int):
|
492 |
+
data = await self.greetings.find_one(
|
493 |
+
{"client": client, "chat": chat, "welcome": True}
|
494 |
+
)
|
495 |
+
return data
|
496 |
+
|
497 |
+
async def set_goodbye(self, client: int, chat: int, message: int):
|
498 |
+
await self.greetings.update_one(
|
499 |
+
{"client": client, "chat": chat, "welcome": False},
|
500 |
+
{"$set": {"message": message}},
|
501 |
+
upsert=True,
|
502 |
+
)
|
503 |
+
|
504 |
+
async def rm_goodbye(self, client: int, chat: int):
|
505 |
+
await self.greetings.delete_one(
|
506 |
+
{"client": client, "chat": chat, "welcome": False}
|
507 |
+
)
|
508 |
+
|
509 |
+
async def is_goodbye(self, client: int, chat: int) -> bool:
|
510 |
+
data = await self.get_goodbye(client, chat)
|
511 |
+
return True if data else False
|
512 |
+
|
513 |
+
async def get_goodbye(self, client: int, chat: int):
|
514 |
+
data = await self.greetings.find_one(
|
515 |
+
{"client": client, "chat": chat, "welcome": False}
|
516 |
+
)
|
517 |
+
return data
|
518 |
+
|
519 |
+
async def get_all_greetings(self, client: int) -> list:
|
520 |
+
return [i async for i in self.greetings.find({"client": client})]
|
521 |
+
|
522 |
+
async def add_forcesub(self, chat: int, must_join: int):
|
523 |
+
await self.forcesub.update_one(
|
524 |
+
{"chat": chat},
|
525 |
+
{"$push": {"must_join": must_join}},
|
526 |
+
upsert=True,
|
527 |
+
)
|
528 |
+
|
529 |
+
async def rm_forcesub(self, chat: int, must_join: int) -> int:
|
530 |
+
await self.forcesub.update_one(
|
531 |
+
{"chat": chat},
|
532 |
+
{"$pull": {"must_join": must_join}},
|
533 |
+
)
|
534 |
+
data = await self.forcesub.find_one({"chat": chat})
|
535 |
+
return len(data["must_join"])
|
536 |
+
|
537 |
+
async def rm_all_forcesub(self, in_chat: int):
|
538 |
+
await self.forcesub.delete_one({"chat": in_chat})
|
539 |
+
|
540 |
+
async def is_forcesub(self, chat: int, must_join: int) -> bool:
|
541 |
+
data = await self.get_forcesub(chat)
|
542 |
+
if must_join in data["must_join"]:
|
543 |
+
return True
|
544 |
+
return False
|
545 |
+
|
546 |
+
async def get_forcesub(self, in_chat: int):
|
547 |
+
data = await self.forcesub.find_one({"chat": in_chat})
|
548 |
+
return data
|
549 |
+
|
550 |
+
async def get_all_forcesubs(self) -> list:
|
551 |
+
return [i async for i in self.forcesub.find({})]
|
552 |
+
|
553 |
+
async def add_gachabot(
|
554 |
+
self, client: int, bot: tuple[int, str], catch_command: str, chat_id: int
|
555 |
+
):
|
556 |
+
await self.gachabots.update_one(
|
557 |
+
{"client": client, "bot": bot[0]},
|
558 |
+
{
|
559 |
+
"$set": {
|
560 |
+
"username": bot[1],
|
561 |
+
"catch_command": catch_command,
|
562 |
+
"chat_id": chat_id,
|
563 |
+
"date": self.get_datetime(),
|
564 |
+
}
|
565 |
+
},
|
566 |
+
upsert=True,
|
567 |
+
)
|
568 |
+
|
569 |
+
async def rm_gachabot(self, client: int, bot: int, chat_id: int = None):
|
570 |
+
if chat_id:
|
571 |
+
await self.gachabots.delete_one(
|
572 |
+
{"client": client, "bot": bot, "chat_id": chat_id}
|
573 |
+
)
|
574 |
+
else:
|
575 |
+
await self.gachabots.delete_one({"client": client, "bot": bot})
|
576 |
+
|
577 |
+
async def is_gachabot(self, client: int, bot: int, chat_id: int) -> bool:
|
578 |
+
data = await self.get_gachabot(client, bot, chat_id)
|
579 |
+
return True if data else False
|
580 |
+
|
581 |
+
async def get_gachabot(self, client: int, bot: int, chat_id: int):
|
582 |
+
data = await self.gachabots.find_one(
|
583 |
+
{"client": client, "bot": bot, "chat_id": chat_id}
|
584 |
+
)
|
585 |
+
|
586 |
+
return data
|
587 |
+
|
588 |
+
async def get_all_gachabots(self, client: int) -> list:
|
589 |
+
return [i async for i in self.gachabots.find({"client": client})]
|
590 |
+
|
591 |
+
async def get_all_gachabots_id(self) -> list:
|
592 |
+
data = await self.gachabots.distinct("bot")
|
593 |
+
return data
|
594 |
+
|
595 |
+
async def _get_cohere_chat_from_db(self, user_id):
|
596 |
+
user_data = await self.cohere.find_one({"user_id": user_id})
|
597 |
+
return user_data.get("cohere_chat", []) if user_data else []
|
598 |
+
|
599 |
+
async def _update_cohere_chat_in_db(self, user_id, cohere_chat):
|
600 |
+
await self.cohere.update_one(
|
601 |
+
{"user_id": user_id},
|
602 |
+
{"$set": {"cohere_chat": cohere_chat}},
|
603 |
+
upsert=True
|
604 |
+
)
|
605 |
+
|
606 |
+
async def _clear_history_in_db(self, user_id):
|
607 |
+
unset_clear = {"cohere_chat": None}
|
608 |
+
return await self.cohere.update_one({"user_id": user_id}, {"$unset": unset_clear})
|
609 |
+
|
610 |
+
async def clear_database(self, user_id):
|
611 |
+
"""Clear the cohere history for the current user."""
|
612 |
+
result = await self._clear_history_in_db(user_id)
|
613 |
+
if result.modified_count > 0:
|
614 |
+
return "Chat history cleared successfully."
|
615 |
+
else:
|
616 |
+
return "No chat history found to clear."
|
617 |
+
|
618 |
+
async def set_chat_setting(self, chat_id, isboolean):
|
619 |
+
await self.antiarabic.update_one(
|
620 |
+
{"chat_id": chat_id},
|
621 |
+
{"$set": {"arabic": isboolean}},
|
622 |
+
upsert=True
|
623 |
+
)
|
624 |
+
|
625 |
+
async def chat_antiarabic(self, chat_id):
|
626 |
+
user_data = await self.antiarabic.find_one({"chat_id": chat_id})
|
627 |
+
if user_data:
|
628 |
+
return user_data.get("arabic", False)
|
629 |
+
return False
|
630 |
+
|
631 |
+
async def add_chatbot(self, chat_id, user_id):
|
632 |
+
await self.chatbot.update_one(
|
633 |
+
{"chat_id": chat_id},
|
634 |
+
{"$set": {"user_id": user_id}},
|
635 |
+
upsert=True
|
636 |
+
)
|
637 |
+
|
638 |
+
async def get_chatbot(self, chat_id):
|
639 |
+
user_data = await self.chatbot.find_one({"chat_id": chat_id})
|
640 |
+
return user_data.get("user_id") if user_data else None
|
641 |
+
|
642 |
+
async def remove_chatbot(self, chat_id):
|
643 |
+
unset_data = {"user_id": None}
|
644 |
+
return await self.chatbot.update_one({"chat_id": chat_id}, {"$unset": unset_data})
|
645 |
+
|
646 |
+
async def _update_openai_chat_in_db(self, user_id, chatbot_chat):
|
647 |
+
await self.backup_chatbot.update_one(
|
648 |
+
{"user_id": user_id},
|
649 |
+
{"$set": {"chatbot_chat": chatbot_chat}},
|
650 |
+
upsert=True
|
651 |
+
)
|
652 |
+
|
653 |
+
async def _get_openai_chat_from_db(self, user_id):
|
654 |
+
user_data = await self.backup_chatbot.find_one({"user_id": user_id})
|
655 |
+
return user_data.get("chatbot_chat", []) if user_data else []
|
656 |
+
|
657 |
+
async def _clear_chatbot_history_in_db(self, user_id):
|
658 |
+
unset_clear = {"chatbot_chat": None}
|
659 |
+
return await self.backup_chatbot.update_one({"user_id": user_id}, {"$unset": unset_clear})
|
660 |
+
|
661 |
+
async def _clear_chatbot_database(self, user_id):
|
662 |
+
result = await self._clear_chatbot_history_in_db(user_id)
|
663 |
+
if result.modified_count > 0:
|
664 |
+
return "Chat history cleared successfully."
|
665 |
+
else:
|
666 |
+
return "No chat history found to clear."
|
667 |
+
|
668 |
+
async def set_chat_setting_antinsfw(self, chat_id, isboolean):
|
669 |
+
await self.antinsfw.update_one(
|
670 |
+
{"chat_id": chat_id},
|
671 |
+
{"$set": {"antinsfw": isboolean}},
|
672 |
+
upsert=True
|
673 |
+
)
|
674 |
+
|
675 |
+
async def chat_antinsfw(self, chat_id):
|
676 |
+
user_data = await self.antinsfw.find_one({"chat_id": chat_id})
|
677 |
+
if user_data:
|
678 |
+
return user_data.get("antinsfw", False)
|
679 |
+
return False
|
680 |
+
|
681 |
+
|
682 |
+
db = Database(MONGO_URL)
|