dbfunctions.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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}, {"$set": {"notes": notesd}}, upsert=True
  86. )
  87. return True
  88. return False
  89. """ Filters funcions """
  90. async def get_filters_count() -> dict:
  91. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  92. if not chats:
  93. return {}
  94. chats_count = 0
  95. filters_count = 0
  96. for chat in await chats.to_list(length=1000000000):
  97. filters_name = await get_filters_names(chat["chat_id"])
  98. filters_count += len(filters_name)
  99. chats_count += 1
  100. return {"chats_count": chats_count, "filters_count": filters_count}
  101. async def _get_filters(chat_id: int) -> Dict[str, int]:
  102. _filters = await filtersdb.find_one({"chat_id": chat_id})
  103. if not _filters:
  104. return {}
  105. return _filters["filters"]
  106. async def get_filters_names(chat_id: int) -> List[str]:
  107. _filters = []
  108. for _filter in await _get_filters(chat_id):
  109. _filters.append(_filter)
  110. return _filters
  111. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  112. name = name.lower().strip()
  113. _filters = await _get_filters(chat_id)
  114. if name in _filters:
  115. return _filters[name]
  116. else:
  117. return False
  118. async def save_filter(chat_id: int, name: str, _filter: dict):
  119. name = name.lower().strip()
  120. _filters = await _get_filters(chat_id)
  121. _filters[name] = _filter
  122. await filtersdb.update_one(
  123. {"chat_id": chat_id}, {"$set": {"filters": _filters}}, upsert=True
  124. )
  125. async def delete_filter(chat_id: int, name: str) -> bool:
  126. filtersd = await _get_filters(chat_id)
  127. name = name.lower().strip()
  128. if name in filtersd:
  129. del filtersd[name]
  130. await filtersdb.update_one(
  131. {"chat_id": chat_id}, {"$set": {"filters": filtersd}}, upsert=True
  132. )
  133. return True
  134. return False
  135. """ Warn functions """
  136. async def int_to_alpha(user_id: int) -> str:
  137. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  138. text = ""
  139. user_id = str(user_id)
  140. for i in user_id:
  141. text += alphabet[int(i)]
  142. return text
  143. async def alpha_to_int(user_id_alphabet: str) -> int:
  144. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  145. user_id = ""
  146. for i in user_id_alphabet:
  147. index = alphabet.index(i)
  148. user_id += str(index)
  149. user_id = int(user_id)
  150. return user_id
  151. async def get_warns_count() -> dict:
  152. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  153. if not chats:
  154. return {}
  155. chats_count = 0
  156. warns_count = 0
  157. for chat in await chats.to_list(length=100000000):
  158. for user in chat["warns"]:
  159. warns_count += chat["warns"][user]["warns"]
  160. chats_count += 1
  161. return {"chats_count": chats_count, "warns_count": warns_count}
  162. async def get_warns(chat_id: int) -> Dict[str, int]:
  163. warns = await warnsdb.find_one({"chat_id": chat_id})
  164. if not warns:
  165. return {}
  166. return warns["warns"]
  167. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  168. name = name.lower().strip()
  169. warns = await get_warns(chat_id)
  170. if name in warns:
  171. return warns[name]
  172. async def add_warn(chat_id: int, name: str, warn: dict):
  173. name = name.lower().strip()
  174. warns = await get_warns(chat_id)
  175. warns[name] = warn
  176. await warnsdb.update_one(
  177. {"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
  178. )
  179. async def remove_warns(chat_id: int, name: str) -> bool:
  180. warnsd = await get_warns(chat_id)
  181. name = name.lower().strip()
  182. if name in warnsd:
  183. del warnsd[name]
  184. await warnsdb.update_one(
  185. {"chat_id": chat_id}, {"$set": {"warns": warnsd}}, upsert=True
  186. )
  187. return True
  188. return False
  189. """ Karma functions """
  190. async def get_karmas_count() -> dict:
  191. chats = karmadb.find({"chat_id": {"$lt": 0}})
  192. if not chats:
  193. return {}
  194. chats_count = 0
  195. karmas_count = 0
  196. for chat in await chats.to_list(length=1000000):
  197. for i in chat["karma"]:
  198. karma_ = chat["karma"][i]["karma"]
  199. if karma_ > 0:
  200. karmas_count += karma_
  201. chats_count += 1
  202. return {"chats_count": chats_count, "karmas_count": karmas_count}
  203. async def user_global_karma(user_id) -> int:
  204. chats = karmadb.find({"chat_id": {"$lt": 0}})
  205. if not chats:
  206. return 0
  207. total_karma = 0
  208. for chat in await chats.to_list(length=1000000):
  209. karma = await get_karma(chat["chat_id"], await int_to_alpha(user_id))
  210. if karma:
  211. if int(karma["karma"]) > 0:
  212. total_karma += int(karma["karma"])
  213. return total_karma
  214. async def get_karmas(chat_id: int) -> Dict[str, int]:
  215. karma = await karmadb.find_one({"chat_id": chat_id})
  216. if not karma:
  217. return {}
  218. return karma["karma"]
  219. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  220. name = name.lower().strip()
  221. karmas = await get_karmas(chat_id)
  222. if name in karmas:
  223. return karmas[name]
  224. async def update_karma(chat_id: int, name: str, karma: dict):
  225. name = name.lower().strip()
  226. karmas = await get_karmas(chat_id)
  227. karmas[name] = karma
  228. await karmadb.update_one(
  229. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  230. )
  231. async def is_karma_on(chat_id: int) -> bool:
  232. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  233. if not chat:
  234. return True
  235. return False
  236. async def karma_on(chat_id: int):
  237. is_karma = await is_karma_on(chat_id)
  238. if is_karma:
  239. return
  240. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  241. async def karma_off(chat_id: int):
  242. is_karma = await is_karma_on(chat_id)
  243. if not is_karma:
  244. return
  245. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  246. """ Chats log functions """
  247. async def is_served_chat(chat_id: int) -> bool:
  248. chat = await chatsdb.find_one({"chat_id": chat_id})
  249. if not chat:
  250. return False
  251. return True
  252. async def get_served_chats() -> list:
  253. chats = chatsdb.find({"chat_id": {"$lt": 0}})
  254. if not chats:
  255. return []
  256. chats_list = []
  257. for chat in await chats.to_list(length=1000000000):
  258. chats_list.append(chat)
  259. return chats_list
  260. async def add_served_chat(chat_id: int):
  261. is_served = await is_served_chat(chat_id)
  262. if is_served:
  263. return
  264. return await chatsdb.insert_one({"chat_id": chat_id})
  265. async def remove_served_chat(chat_id: int):
  266. is_served = await is_served_chat(chat_id)
  267. if not is_served:
  268. return
  269. return await chatsdb.delete_one({"chat_id": chat_id})
  270. """ USER LOG FUNCTIONS """
  271. async def is_served_user(user_id: int) -> bool:
  272. user = await usersdb.find_one({"user_id": user_id})
  273. if not user:
  274. return False
  275. return True
  276. async def get_served_users() -> list:
  277. users = usersdb.find({"user_id": {"$gt": 0}})
  278. if not users:
  279. return []
  280. users_list = []
  281. for user in await users.to_list(length=1000000000):
  282. users_list.append(user)
  283. return users_list
  284. async def add_served_user(user_id: int):
  285. is_served = await is_served_user(user_id)
  286. if is_served:
  287. return
  288. return await usersdb.insert_one({"user_id": user_id})
  289. """ Gban functions """
  290. async def get_gbans_count() -> int:
  291. users = gbansdb.find({"user_id": {"$gt": 0}})
  292. users = await users.to_list(length=100000)
  293. return len(users)
  294. async def is_gbanned_user(user_id: int) -> bool:
  295. user = await gbansdb.find_one({"user_id": user_id})
  296. if not user:
  297. return False
  298. return True
  299. async def add_gban_user(user_id: int):
  300. is_gbanned = await is_gbanned_user(user_id)
  301. if is_gbanned:
  302. return
  303. return await gbansdb.insert_one({"user_id": user_id})
  304. async def remove_gban_user(user_id: int):
  305. is_gbanned = await is_gbanned_user(user_id)
  306. if not is_gbanned:
  307. return
  308. return await gbansdb.delete_one({"user_id": user_id})
  309. # Couple Chooser
  310. async def _get_lovers(chat_id: int):
  311. lovers = await coupledb.find_one({"chat_id": chat_id})
  312. if not lovers:
  313. return {}
  314. return lovers["couple"]
  315. async def get_couple(chat_id: int, date: str):
  316. lovers = await _get_lovers(chat_id)
  317. if date in lovers:
  318. return lovers[date]
  319. else:
  320. return False
  321. async def save_couple(chat_id: int, date: str, couple: dict):
  322. lovers = await _get_lovers(chat_id)
  323. lovers[date] = couple
  324. await coupledb.update_one(
  325. {"chat_id": chat_id}, {"$set": {"couple": lovers}}, upsert=True
  326. )
  327. # Captcha
  328. async def is_captcha_on(chat_id: int) -> bool:
  329. chat = await captchadb.find_one({"chat_id": chat_id})
  330. if not chat:
  331. return True
  332. return False
  333. async def captcha_on(chat_id: int):
  334. is_captcha = await is_captcha_on(chat_id)
  335. if is_captcha:
  336. return
  337. return await captchadb.delete_one({"chat_id": chat_id})
  338. async def captcha_off(chat_id: int):
  339. is_captcha = await is_captcha_on(chat_id)
  340. if not is_captcha:
  341. return
  342. return await captchadb.insert_one({"chat_id": chat_id})
  343. """Anti Service System"""
  344. async def is_antiservice_on(chat_id: int) -> bool:
  345. chat = await antiservicedb.find_one({"chat_id": chat_id})
  346. if not chat:
  347. return True
  348. return False
  349. async def antiservice_on(chat_id: int):
  350. is_antiservice = await is_antiservice_on(chat_id)
  351. if is_antiservice:
  352. return
  353. return await antiservicedb.delete_one({"chat_id": chat_id})
  354. async def antiservice_off(chat_id: int):
  355. is_antiservice = await is_antiservice_on(chat_id)
  356. if not is_antiservice:
  357. return
  358. return await antiservicedb.insert_one({"chat_id": chat_id})
  359. """ PM PERMIT """
  360. async def is_pmpermit_approved(user_id: int) -> bool:
  361. user = await pmpermitdb.find_one({"user_id": user_id})
  362. if not user:
  363. return False
  364. return True
  365. async def approve_pmpermit(user_id: int):
  366. is_pmpermit = await is_pmpermit_approved(user_id)
  367. if is_pmpermit:
  368. return
  369. return await pmpermitdb.insert_one({"user_id": user_id})
  370. async def disapprove_pmpermit(user_id: int):
  371. is_pmpermit = await is_pmpermit_approved(user_id)
  372. if not is_pmpermit:
  373. return
  374. return await pmpermitdb.delete_one({"user_id": user_id})
  375. """ WELCOME FUNCTIONS """
  376. async def get_welcome(chat_id: int) -> str:
  377. text = await welcomedb.find_one({"chat_id": chat_id})
  378. return text["text"]
  379. async def set_welcome(chat_id: int, text: str):
  380. return await welcomedb.update_one(
  381. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  382. )
  383. async def del_welcome(chat_id: int):
  384. return await welcomedb.delete_one({"chat_id": chat_id})
  385. """NSFW System"""
  386. async def is_nsfw_on(chat_id: int) -> bool:
  387. chat = await nsfwdb.find_one({"chat_id": chat_id})
  388. if not chat:
  389. return True
  390. return False
  391. async def nsfw_on(chat_id: int):
  392. is_nsfw = await is_nsfw_on(chat_id)
  393. if is_nsfw:
  394. return
  395. return await nsfwdb.delete_one({"chat_id": chat_id})
  396. async def nsfw_off(chat_id: int):
  397. is_nsfw = await is_nsfw_on(chat_id)
  398. if not is_nsfw:
  399. return
  400. return await nsfwdb.insert_one({"chat_id": chat_id})
  401. """ CAPTCHA CACHE SYSTEM """
  402. async def update_captcha_cache(captcha_dict):
  403. pickle = obj_to_str(captcha_dict)
  404. await captcha_cachedb.delete_one({"captcha": "cache"})
  405. if not pickle:
  406. return
  407. await captcha_cachedb.update_one(
  408. {"captcha": "cache"}, {"$set": {"pickled": pickle}}, upsert=True
  409. )
  410. async def get_captcha_cache():
  411. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  412. if not cache:
  413. return []
  414. return str_to_obj(cache["pickled"])
  415. """ BLACKLIST FILTERS SYSTEM """
  416. async def get_blacklist_filters_count() -> dict:
  417. chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
  418. if not chats:
  419. return {"chats_count": 0, "filters_count": 0}
  420. chats_count = 0
  421. filters_count = 0
  422. for chat in await chats.to_list(length=1000000000):
  423. filters = await get_blacklisted_words(chat["chat_id"])
  424. filters_count += len(filters)
  425. chats_count += 1
  426. return {"chats_count": chats_count, "filters_count": filters_count}
  427. async def get_blacklisted_words(chat_id: int) -> List[str]:
  428. _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
  429. if not _filters:
  430. return []
  431. return _filters["filters"]
  432. async def save_blacklist_filter(chat_id: int, word: str):
  433. word = word.lower().strip()
  434. _filters = await get_blacklisted_words(chat_id)
  435. _filters.append(word)
  436. await blacklist_filtersdb.update_one(
  437. {"chat_id": chat_id}, {"$set": {"filters": _filters}}, upsert=True
  438. )
  439. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  440. filtersd = await get_blacklisted_words(chat_id)
  441. word = word.lower().strip()
  442. if word in filtersd:
  443. filtersd.remove(word)
  444. await blacklist_filtersdb.update_one(
  445. {"chat_id": chat_id}, {"$set": {"filters": filtersd}}, upsert=True
  446. )
  447. return True
  448. return False
  449. """ PIPES SYSTEM """
  450. async def activate_pipe(from_chat_id: int, to_chat_id: int, fetcher: str):
  451. pipes = await show_pipes()
  452. pipe = {
  453. "from_chat_id": from_chat_id,
  454. "to_chat_id": to_chat_id,
  455. "fetcher": fetcher,
  456. }
  457. pipes.append(pipe)
  458. return await pipesdb.update_one(
  459. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  460. )
  461. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  462. pipes = await show_pipes()
  463. if not pipes:
  464. return
  465. for pipe in pipes:
  466. if (
  467. pipe["from_chat_id"] == from_chat_id
  468. and pipe["to_chat_id"] == to_chat_id
  469. ):
  470. pipes.remove(pipe)
  471. return await pipesdb.update_one(
  472. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  473. )
  474. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  475. for pipe in await show_pipes():
  476. if (
  477. pipe["from_chat_id"] == from_chat_id
  478. and pipe["to_chat_id"] == to_chat_id
  479. ):
  480. return True
  481. async def show_pipes() -> list:
  482. pipes = await pipesdb.find_one({"pipe": "pipe"})
  483. if not pipes:
  484. return []
  485. return pipes["pipes"]
  486. """ SUDOERS FUNCTIONS """
  487. async def get_sudoers() -> list:
  488. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  489. if not sudoers:
  490. return []
  491. return sudoers["sudoers"]
  492. async def add_sudo(user_id: int) -> bool:
  493. sudoers = await get_sudoers()
  494. sudoers.append(user_id)
  495. await sudoersdb.update_one(
  496. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  497. )
  498. return True
  499. async def remove_sudo(user_id: int) -> bool:
  500. sudoers = await get_sudoers()
  501. sudoers.remove(user_id)
  502. await sudoersdb.update_one(
  503. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  504. )
  505. return True
  506. """ BLACKLISTED CHATS """
  507. async def blacklisted_chats() -> list:
  508. chats = blacklist_chatdb.find({"chat_id": {"$lt": 0}})
  509. return [chat["chat_id"] for chat in await chats.to_list(length=1000000000)]
  510. async def blacklist_chat(chat_id: int) -> bool:
  511. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  512. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  513. return True
  514. return False
  515. async def whitelist_chat(chat_id: int) -> bool:
  516. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  517. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  518. return True
  519. return False
  520. """ Restart stage """
  521. async def start_restart_stage(chat_id: int, message_id: int):
  522. await restart_stagedb.update_one(
  523. {"something": "something"},
  524. {
  525. "$set": {
  526. "chat_id": chat_id,
  527. "message_id": message_id,
  528. }
  529. },
  530. upsert=True,
  531. )
  532. async def clean_restart_stage() -> dict:
  533. data = await restart_stagedb.find_one({"something": "something"})
  534. if not data:
  535. return {}
  536. await restart_stagedb.delete_one({"something": "something"})
  537. return {"chat_id": data["chat_id"], "message_id": data["message_id"]}
  538. """ TRUST DB """
  539. async def get_trust_db(user_id: int) -> list:
  540. user = await trustdb.find_one({"user_id": user_id})
  541. if not user:
  542. return []
  543. return user["data"]
  544. async def update_trust_db(user_id: int, new_data: float):
  545. user = await trustdb.find_one({"user_id": user_id})
  546. data = user["data"] if user else []
  547. if len(data) >= 1000:
  548. data.remove(data[0])
  549. data.append(new_data)
  550. try:
  551. await trustdb.update_one(
  552. {"user_id": user_id}, {"$set": {"data": data}}, upsert=True
  553. )
  554. except Exception:
  555. pass # to prevent stack overflow error