dbfunctions.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from typing import Dict, List, Union
  21. from wbb import db
  22. from wbb.utils.functions import obj_to_str, str_to_obj
  23. notesdb = db.notes
  24. filtersdb = db.filters
  25. warnsdb = db.warns
  26. karmadb = db.karma
  27. chatsdb = db.chats
  28. usersdb = db.users
  29. gbansdb = db.gban
  30. coupledb = db.couple
  31. captchadb = db.captcha
  32. antiservicedb = db.antiservice
  33. pmpermitdb = db.pmpermit
  34. welcomedb = db.welcome_text
  35. nsfwdb = db.nsfw
  36. captcha_cachedb = db.captcha_cache
  37. blacklist_filtersdb = db.blacklistFilters
  38. pipesdb = db.pipes
  39. sudoersdb = db.sudoers
  40. blacklist_chatdb = db.blacklistChat
  41. restart_stagedb = db.restart_stage
  42. trustdb = db.trust
  43. """ Notes functions """
  44. async def get_notes_count() -> dict:
  45. chats = notesdb.find({"chat_id": {"$lt": 0}})
  46. if not chats:
  47. return {}
  48. chats_count = 0
  49. notes_count = 0
  50. for chat in await chats.to_list(length=1000000000):
  51. notes_name = await get_note_names(chat["chat_id"])
  52. notes_count += len(notes_name)
  53. chats_count += 1
  54. return {"chats_count": chats_count, "notes_count": notes_count}
  55. async def _get_notes(chat_id: int) -> Dict[str, int]:
  56. _notes = await notesdb.find_one({"chat_id": chat_id})
  57. if not _notes:
  58. return {}
  59. return _notes["notes"]
  60. async def get_note_names(chat_id: int) -> List[str]:
  61. _notes = []
  62. for note in await _get_notes(chat_id):
  63. _notes.append(note)
  64. return _notes
  65. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  66. name = name.lower().strip()
  67. _notes = await _get_notes(chat_id)
  68. if name in _notes:
  69. return _notes[name]
  70. else:
  71. return False
  72. async def save_note(chat_id: int, name: str, note: dict):
  73. name = name.lower().strip()
  74. _notes = await _get_notes(chat_id)
  75. _notes[name] = note
  76. await notesdb.update_one(
  77. {"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True
  78. )
  79. async def delete_note(chat_id: int, name: str) -> bool:
  80. notesd = await _get_notes(chat_id)
  81. name = name.lower().strip()
  82. if name in notesd:
  83. del notesd[name]
  84. await notesdb.update_one(
  85. {"chat_id": chat_id},
  86. {"$set": {"notes": notesd}},
  87. upsert=True,
  88. )
  89. return True
  90. return False
  91. """ Filters funcions """
  92. async def get_filters_count() -> dict:
  93. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  94. if not chats:
  95. return {}
  96. chats_count = 0
  97. filters_count = 0
  98. for chat in await chats.to_list(length=1000000000):
  99. filters_name = await get_filters_names(chat["chat_id"])
  100. filters_count += len(filters_name)
  101. chats_count += 1
  102. return {
  103. "chats_count": chats_count,
  104. "filters_count": filters_count,
  105. }
  106. async def _get_filters(chat_id: int) -> Dict[str, int]:
  107. _filters = await filtersdb.find_one({"chat_id": chat_id})
  108. if not _filters:
  109. return {}
  110. return _filters["filters"]
  111. async def get_filters_names(chat_id: int) -> List[str]:
  112. _filters = []
  113. for _filter in await _get_filters(chat_id):
  114. _filters.append(_filter)
  115. return _filters
  116. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  117. name = name.lower().strip()
  118. _filters = await _get_filters(chat_id)
  119. if name in _filters:
  120. return _filters[name]
  121. else:
  122. return False
  123. async def save_filter(chat_id: int, name: str, _filter: dict):
  124. name = name.lower().strip()
  125. _filters = await _get_filters(chat_id)
  126. _filters[name] = _filter
  127. await filtersdb.update_one(
  128. {"chat_id": chat_id},
  129. {"$set": {"filters": _filters}},
  130. upsert=True,
  131. )
  132. async def delete_filter(chat_id: int, name: str) -> bool:
  133. filtersd = await _get_filters(chat_id)
  134. name = name.lower().strip()
  135. if name in filtersd:
  136. del filtersd[name]
  137. await filtersdb.update_one(
  138. {"chat_id": chat_id},
  139. {"$set": {"filters": filtersd}},
  140. upsert=True,
  141. )
  142. return True
  143. return False
  144. """ Warn functions """
  145. async def int_to_alpha(user_id: int) -> str:
  146. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  147. text = ""
  148. user_id = str(user_id)
  149. for i in user_id:
  150. text += alphabet[int(i)]
  151. return text
  152. async def alpha_to_int(user_id_alphabet: str) -> int:
  153. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  154. user_id = ""
  155. for i in user_id_alphabet:
  156. index = alphabet.index(i)
  157. user_id += str(index)
  158. user_id = int(user_id)
  159. return user_id
  160. async def get_warns_count() -> dict:
  161. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  162. if not chats:
  163. return {}
  164. chats_count = 0
  165. warns_count = 0
  166. for chat in await chats.to_list(length=100000000):
  167. for user in chat["warns"]:
  168. warns_count += chat["warns"][user]["warns"]
  169. chats_count += 1
  170. return {"chats_count": chats_count, "warns_count": warns_count}
  171. async def get_warns(chat_id: int) -> Dict[str, int]:
  172. warns = await warnsdb.find_one({"chat_id": chat_id})
  173. if not warns:
  174. return {}
  175. return warns["warns"]
  176. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  177. name = name.lower().strip()
  178. warns = await get_warns(chat_id)
  179. if name in warns:
  180. return warns[name]
  181. async def add_warn(chat_id: int, name: str, warn: dict):
  182. name = name.lower().strip()
  183. warns = await get_warns(chat_id)
  184. warns[name] = warn
  185. await warnsdb.update_one(
  186. {"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
  187. )
  188. async def remove_warns(chat_id: int, name: str) -> bool:
  189. warnsd = await get_warns(chat_id)
  190. name = name.lower().strip()
  191. if name in warnsd:
  192. del warnsd[name]
  193. await warnsdb.update_one(
  194. {"chat_id": chat_id},
  195. {"$set": {"warns": warnsd}},
  196. upsert=True,
  197. )
  198. return True
  199. return False
  200. """ Karma functions """
  201. async def get_karmas_count() -> dict:
  202. chats = karmadb.find({"chat_id": {"$lt": 0}})
  203. if not chats:
  204. return {}
  205. chats_count = 0
  206. karmas_count = 0
  207. for chat in await chats.to_list(length=1000000):
  208. for i in chat["karma"]:
  209. karma_ = chat["karma"][i]["karma"]
  210. if karma_ > 0:
  211. karmas_count += karma_
  212. chats_count += 1
  213. return {"chats_count": chats_count, "karmas_count": karmas_count}
  214. async def user_global_karma(user_id) -> int:
  215. chats = karmadb.find({"chat_id": {"$lt": 0}})
  216. if not chats:
  217. return 0
  218. total_karma = 0
  219. for chat in await chats.to_list(length=1000000):
  220. karma = await get_karma(
  221. chat["chat_id"], await int_to_alpha(user_id)
  222. )
  223. if karma:
  224. if int(karma["karma"]) > 0:
  225. total_karma += int(karma["karma"])
  226. return total_karma
  227. async def get_karmas(chat_id: int) -> Dict[str, int]:
  228. karma = await karmadb.find_one({"chat_id": chat_id})
  229. if not karma:
  230. return {}
  231. return karma["karma"]
  232. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  233. name = name.lower().strip()
  234. karmas = await get_karmas(chat_id)
  235. if name in karmas:
  236. return karmas[name]
  237. async def update_karma(chat_id: int, name: str, karma: dict):
  238. name = name.lower().strip()
  239. karmas = await get_karmas(chat_id)
  240. karmas[name] = karma
  241. await karmadb.update_one(
  242. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  243. )
  244. async def is_karma_on(chat_id: int) -> bool:
  245. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  246. if not chat:
  247. return True
  248. return False
  249. async def karma_on(chat_id: int):
  250. is_karma = await is_karma_on(chat_id)
  251. if is_karma:
  252. return
  253. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  254. async def karma_off(chat_id: int):
  255. is_karma = await is_karma_on(chat_id)
  256. if not is_karma:
  257. return
  258. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  259. """ Chats log functions """
  260. async def is_served_chat(chat_id: int) -> bool:
  261. chat = await chatsdb.find_one({"chat_id": chat_id})
  262. if not chat:
  263. return False
  264. return True
  265. async def get_served_chats() -> list:
  266. chats = chatsdb.find({"chat_id": {"$lt": 0}})
  267. if not chats:
  268. return []
  269. chats_list = []
  270. for chat in await chats.to_list(length=1000000000):
  271. chats_list.append(chat)
  272. return chats_list
  273. async def add_served_chat(chat_id: int):
  274. is_served = await is_served_chat(chat_id)
  275. if is_served:
  276. return
  277. return await chatsdb.insert_one({"chat_id": chat_id})
  278. async def remove_served_chat(chat_id: int):
  279. is_served = await is_served_chat(chat_id)
  280. if not is_served:
  281. return
  282. return await chatsdb.delete_one({"chat_id": chat_id})
  283. """ USER LOG FUNCTIONS """
  284. async def is_served_user(user_id: int) -> bool:
  285. user = await usersdb.find_one({"user_id": user_id})
  286. if not user:
  287. return False
  288. return True
  289. async def get_served_users() -> list:
  290. users = usersdb.find({"user_id": {"$gt": 0}})
  291. if not users:
  292. return []
  293. users_list = []
  294. for user in await users.to_list(length=1000000000):
  295. users_list.append(user)
  296. return users_list
  297. async def add_served_user(user_id: int):
  298. is_served = await is_served_user(user_id)
  299. if is_served:
  300. return
  301. return await usersdb.insert_one({"user_id": user_id})
  302. """ Gban functions """
  303. async def get_gbans_count() -> int:
  304. users = gbansdb.find({"user_id": {"$gt": 0}})
  305. users = await users.to_list(length=100000)
  306. return len(users)
  307. async def is_gbanned_user(user_id: int) -> bool:
  308. user = await gbansdb.find_one({"user_id": user_id})
  309. if not user:
  310. return False
  311. return True
  312. async def add_gban_user(user_id: int):
  313. is_gbanned = await is_gbanned_user(user_id)
  314. if is_gbanned:
  315. return
  316. return await gbansdb.insert_one({"user_id": user_id})
  317. async def remove_gban_user(user_id: int):
  318. is_gbanned = await is_gbanned_user(user_id)
  319. if not is_gbanned:
  320. return
  321. return await gbansdb.delete_one({"user_id": user_id})
  322. # Couple Chooser
  323. async def _get_lovers(chat_id: int):
  324. lovers = await coupledb.find_one({"chat_id": chat_id})
  325. if not lovers:
  326. return {}
  327. return lovers["couple"]
  328. async def get_couple(chat_id: int, date: str):
  329. lovers = await _get_lovers(chat_id)
  330. if date in lovers:
  331. return lovers[date]
  332. else:
  333. return False
  334. async def save_couple(chat_id: int, date: str, couple: dict):
  335. lovers = await _get_lovers(chat_id)
  336. lovers[date] = couple
  337. await coupledb.update_one(
  338. {"chat_id": chat_id},
  339. {"$set": {"couple": lovers}},
  340. upsert=True,
  341. )
  342. # Captcha
  343. async def is_captcha_on(chat_id: int) -> bool:
  344. chat = await captchadb.find_one({"chat_id": chat_id})
  345. if not chat:
  346. return True
  347. return False
  348. async def captcha_on(chat_id: int):
  349. is_captcha = await is_captcha_on(chat_id)
  350. if is_captcha:
  351. return
  352. return await captchadb.delete_one({"chat_id": chat_id})
  353. async def captcha_off(chat_id: int):
  354. is_captcha = await is_captcha_on(chat_id)
  355. if not is_captcha:
  356. return
  357. return await captchadb.insert_one({"chat_id": chat_id})
  358. """Anti Service System"""
  359. async def is_antiservice_on(chat_id: int) -> bool:
  360. chat = await antiservicedb.find_one({"chat_id": chat_id})
  361. if not chat:
  362. return True
  363. return False
  364. async def antiservice_on(chat_id: int):
  365. is_antiservice = await is_antiservice_on(chat_id)
  366. if is_antiservice:
  367. return
  368. return await antiservicedb.delete_one({"chat_id": chat_id})
  369. async def antiservice_off(chat_id: int):
  370. is_antiservice = await is_antiservice_on(chat_id)
  371. if not is_antiservice:
  372. return
  373. return await antiservicedb.insert_one({"chat_id": chat_id})
  374. """ PM PERMIT """
  375. async def is_pmpermit_approved(user_id: int) -> bool:
  376. user = await pmpermitdb.find_one({"user_id": user_id})
  377. if not user:
  378. return False
  379. return True
  380. async def approve_pmpermit(user_id: int):
  381. is_pmpermit = await is_pmpermit_approved(user_id)
  382. if is_pmpermit:
  383. return
  384. return await pmpermitdb.insert_one({"user_id": user_id})
  385. async def disapprove_pmpermit(user_id: int):
  386. is_pmpermit = await is_pmpermit_approved(user_id)
  387. if not is_pmpermit:
  388. return
  389. return await pmpermitdb.delete_one({"user_id": user_id})
  390. """ WELCOME FUNCTIONS """
  391. async def get_welcome(chat_id: int) -> str:
  392. text = await welcomedb.find_one({"chat_id": chat_id})
  393. return text["text"]
  394. async def set_welcome(chat_id: int, text: str):
  395. return await welcomedb.update_one(
  396. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  397. )
  398. async def del_welcome(chat_id: int):
  399. return await welcomedb.delete_one({"chat_id": chat_id})
  400. """NSFW System"""
  401. async def is_nsfw_on(chat_id: int) -> bool:
  402. chat = await nsfwdb.find_one({"chat_id": chat_id})
  403. if not chat:
  404. return True
  405. return False
  406. async def nsfw_on(chat_id: int):
  407. is_nsfw = await is_nsfw_on(chat_id)
  408. if is_nsfw:
  409. return
  410. return await nsfwdb.delete_one({"chat_id": chat_id})
  411. async def nsfw_off(chat_id: int):
  412. is_nsfw = await is_nsfw_on(chat_id)
  413. if not is_nsfw:
  414. return
  415. return await nsfwdb.insert_one({"chat_id": chat_id})
  416. """ CAPTCHA CACHE SYSTEM """
  417. async def update_captcha_cache(captcha_dict):
  418. pickle = obj_to_str(captcha_dict)
  419. await captcha_cachedb.delete_one({"captcha": "cache"})
  420. if not pickle:
  421. return
  422. await captcha_cachedb.update_one(
  423. {"captcha": "cache"},
  424. {"$set": {"pickled": pickle}},
  425. upsert=True,
  426. )
  427. async def get_captcha_cache():
  428. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  429. if not cache:
  430. return []
  431. return str_to_obj(cache["pickled"])
  432. """ BLACKLIST FILTERS SYSTEM """
  433. async def get_blacklist_filters_count() -> dict:
  434. chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
  435. if not chats:
  436. return {"chats_count": 0, "filters_count": 0}
  437. chats_count = 0
  438. filters_count = 0
  439. for chat in await chats.to_list(length=1000000000):
  440. filters = await get_blacklisted_words(chat["chat_id"])
  441. filters_count += len(filters)
  442. chats_count += 1
  443. return {
  444. "chats_count": chats_count,
  445. "filters_count": filters_count,
  446. }
  447. async def get_blacklisted_words(chat_id: int) -> List[str]:
  448. _filters = await blacklist_filtersdb.find_one(
  449. {"chat_id": chat_id}
  450. )
  451. if not _filters:
  452. return []
  453. return _filters["filters"]
  454. async def save_blacklist_filter(chat_id: int, word: str):
  455. word = word.lower().strip()
  456. _filters = await get_blacklisted_words(chat_id)
  457. _filters.append(word)
  458. await blacklist_filtersdb.update_one(
  459. {"chat_id": chat_id},
  460. {"$set": {"filters": _filters}},
  461. upsert=True,
  462. )
  463. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  464. filtersd = await get_blacklisted_words(chat_id)
  465. word = word.lower().strip()
  466. if word in filtersd:
  467. filtersd.remove(word)
  468. await blacklist_filtersdb.update_one(
  469. {"chat_id": chat_id},
  470. {"$set": {"filters": filtersd}},
  471. upsert=True,
  472. )
  473. return True
  474. return False
  475. """ PIPES SYSTEM """
  476. async def activate_pipe(
  477. from_chat_id: int, to_chat_id: int, fetcher: str
  478. ):
  479. pipes = await show_pipes()
  480. pipe = {
  481. "from_chat_id": from_chat_id,
  482. "to_chat_id": to_chat_id,
  483. "fetcher": fetcher,
  484. }
  485. pipes.append(pipe)
  486. return await pipesdb.update_one(
  487. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  488. )
  489. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  490. pipes = await show_pipes()
  491. if not pipes:
  492. return
  493. for pipe in pipes:
  494. if (
  495. pipe["from_chat_id"] == from_chat_id
  496. and pipe["to_chat_id"] == to_chat_id
  497. ):
  498. pipes.remove(pipe)
  499. return await pipesdb.update_one(
  500. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  501. )
  502. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  503. for pipe in await show_pipes():
  504. if (
  505. pipe["from_chat_id"] == from_chat_id
  506. and pipe["to_chat_id"] == to_chat_id
  507. ):
  508. return True
  509. async def show_pipes() -> list:
  510. pipes = await pipesdb.find_one({"pipe": "pipe"})
  511. if not pipes:
  512. return []
  513. return pipes["pipes"]
  514. """ SUDOERS FUNCTIONS """
  515. async def get_sudoers() -> list:
  516. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  517. if not sudoers:
  518. return []
  519. return sudoers["sudoers"]
  520. async def add_sudo(user_id: int) -> bool:
  521. sudoers = await get_sudoers()
  522. sudoers.append(user_id)
  523. await sudoersdb.update_one(
  524. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  525. )
  526. return True
  527. async def remove_sudo(user_id: int) -> bool:
  528. sudoers = await get_sudoers()
  529. sudoers.remove(user_id)
  530. await sudoersdb.update_one(
  531. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  532. )
  533. return True
  534. """ BLACKLISTED CHATS """
  535. async def blacklisted_chats() -> list:
  536. chats = blacklist_chatdb.find({"chat_id": {"$lt": 0}})
  537. return [
  538. chat["chat_id"]
  539. for chat in await chats.to_list(length=1000000000)
  540. ]
  541. async def blacklist_chat(chat_id: int) -> bool:
  542. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  543. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  544. return True
  545. return False
  546. async def whitelist_chat(chat_id: int) -> bool:
  547. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  548. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  549. return True
  550. return False
  551. """ Restart stage """
  552. async def start_restart_stage(chat_id: int, message_id: int):
  553. await restart_stagedb.update_one(
  554. {"something": "something"},
  555. {
  556. "$set": {
  557. "chat_id": chat_id,
  558. "message_id": message_id,
  559. }
  560. },
  561. upsert=True,
  562. )
  563. async def clean_restart_stage() -> dict:
  564. data = await restart_stagedb.find_one({"something": "something"})
  565. if not data:
  566. return {}
  567. await restart_stagedb.delete_one({"something": "something"})
  568. return {
  569. "chat_id": data["chat_id"],
  570. "message_id": data["message_id"],
  571. }
  572. """ TRUST DB """
  573. async def get_trust_db(user_id: int) -> list:
  574. user = await trustdb.find_one({"user_id": user_id})
  575. if not user:
  576. return []
  577. return user["data"]
  578. async def update_trust_db(user_id: int, new_data: float):
  579. user = await trustdb.find_one({"user_id": user_id})
  580. data = user["data"] if user else []
  581. if len(data) >= 1000:
  582. data.remove(data[0])
  583. data.append(new_data)
  584. try:
  585. await trustdb.update_one(
  586. {"user_id": user_id},
  587. {"$set": {"data": data}},
  588. upsert=True,
  589. )
  590. except Exception:
  591. pass # to prevent stack overflow error