dbfunctions.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. """
  24. SOME THINGS ARE FUCKED UP HERE, LIKE TOGGLEABLES HAVE THEIR OWN COLLECTION
  25. (SHOULD FIX IT WITH SOMETHING LIKE TOGGLEDB, BUT I WON'T, AS IT WILL TAKE
  26. TOO MUCH TIME AND WILL BE BAD FOR ALREADY STORED DATA)
  27. """
  28. notesdb = db.notes
  29. filtersdb = db.filters
  30. warnsdb = db.warns
  31. karmadb = db.karma
  32. chatsdb = db.chats
  33. usersdb = db.users
  34. gbansdb = db.gban
  35. coupledb = db.couple
  36. captchadb = db.captcha
  37. antiservicedb = db.antiservice
  38. pmpermitdb = db.pmpermit
  39. welcomedb = db.welcome_text
  40. nsfwdb = db.nsfw
  41. captcha_cachedb = db.captcha_cache
  42. blacklist_filtersdb = db.blacklistFilters
  43. pipesdb = db.pipes
  44. sudoersdb = db.sudoers
  45. blacklist_chatdb = db.blacklistChat
  46. restart_stagedb = db.restart_stage
  47. trustdb = db.trust
  48. flood_toggle_db = db.flood_toggle
  49. spam_toggle_db = db.spam_toggle
  50. """ Notes functions """
  51. async def get_notes_count() -> dict:
  52. chats = notesdb.find({"chat_id": {"$lt": 0}})
  53. if not chats:
  54. return {}
  55. chats_count = 0
  56. notes_count = 0
  57. for chat in await chats.to_list(length=1000000000):
  58. notes_name = await get_note_names(chat["chat_id"])
  59. notes_count += len(notes_name)
  60. chats_count += 1
  61. return {"chats_count": chats_count, "notes_count": notes_count}
  62. async def _get_notes(chat_id: int) -> Dict[str, int]:
  63. _notes = await notesdb.find_one({"chat_id": chat_id})
  64. if not _notes:
  65. return {}
  66. return _notes["notes"]
  67. async def get_note_names(chat_id: int) -> List[str]:
  68. _notes = []
  69. for note in await _get_notes(chat_id):
  70. _notes.append(note)
  71. return _notes
  72. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  73. name = name.lower().strip()
  74. _notes = await _get_notes(chat_id)
  75. if name in _notes:
  76. return _notes[name]
  77. else:
  78. return False
  79. async def save_note(chat_id: int, name: str, note: dict):
  80. name = name.lower().strip()
  81. _notes = await _get_notes(chat_id)
  82. _notes[name] = note
  83. await notesdb.update_one(
  84. {"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True
  85. )
  86. async def delete_note(chat_id: int, name: str) -> bool:
  87. notesd = await _get_notes(chat_id)
  88. name = name.lower().strip()
  89. if name in notesd:
  90. del notesd[name]
  91. await notesdb.update_one(
  92. {"chat_id": chat_id},
  93. {"$set": {"notes": notesd}},
  94. upsert=True,
  95. )
  96. return True
  97. return False
  98. """ Filters funcions """
  99. async def get_filters_count() -> dict:
  100. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  101. if not chats:
  102. return {}
  103. chats_count = 0
  104. filters_count = 0
  105. for chat in await chats.to_list(length=1000000000):
  106. filters_name = await get_filters_names(chat["chat_id"])
  107. filters_count += len(filters_name)
  108. chats_count += 1
  109. return {
  110. "chats_count": chats_count,
  111. "filters_count": filters_count,
  112. }
  113. async def _get_filters(chat_id: int) -> Dict[str, int]:
  114. _filters = await filtersdb.find_one({"chat_id": chat_id})
  115. if not _filters:
  116. return {}
  117. return _filters["filters"]
  118. async def get_filters_names(chat_id: int) -> List[str]:
  119. _filters = []
  120. for _filter in await _get_filters(chat_id):
  121. _filters.append(_filter)
  122. return _filters
  123. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  124. name = name.lower().strip()
  125. _filters = await _get_filters(chat_id)
  126. if name in _filters:
  127. return _filters[name]
  128. else:
  129. return False
  130. async def save_filter(chat_id: int, name: str, _filter: dict):
  131. name = name.lower().strip()
  132. _filters = await _get_filters(chat_id)
  133. _filters[name] = _filter
  134. await filtersdb.update_one(
  135. {"chat_id": chat_id},
  136. {"$set": {"filters": _filters}},
  137. upsert=True,
  138. )
  139. async def delete_filter(chat_id: int, name: str) -> bool:
  140. filtersd = await _get_filters(chat_id)
  141. name = name.lower().strip()
  142. if name in filtersd:
  143. del filtersd[name]
  144. await filtersdb.update_one(
  145. {"chat_id": chat_id},
  146. {"$set": {"filters": filtersd}},
  147. upsert=True,
  148. )
  149. return True
  150. return False
  151. """ Warn functions """
  152. async def int_to_alpha(user_id: int) -> str:
  153. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  154. text = ""
  155. user_id = str(user_id)
  156. for i in user_id:
  157. text += alphabet[int(i)]
  158. return text
  159. async def alpha_to_int(user_id_alphabet: str) -> int:
  160. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  161. user_id = ""
  162. for i in user_id_alphabet:
  163. index = alphabet.index(i)
  164. user_id += str(index)
  165. user_id = int(user_id)
  166. return user_id
  167. async def get_warns_count() -> dict:
  168. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  169. if not chats:
  170. return {}
  171. chats_count = 0
  172. warns_count = 0
  173. for chat in await chats.to_list(length=100000000):
  174. for user in chat["warns"]:
  175. warns_count += chat["warns"][user]["warns"]
  176. chats_count += 1
  177. return {"chats_count": chats_count, "warns_count": warns_count}
  178. async def get_warns(chat_id: int) -> Dict[str, int]:
  179. warns = await warnsdb.find_one({"chat_id": chat_id})
  180. if not warns:
  181. return {}
  182. return warns["warns"]
  183. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  184. name = name.lower().strip()
  185. warns = await get_warns(chat_id)
  186. if name in warns:
  187. return warns[name]
  188. async def add_warn(chat_id: int, name: str, warn: dict):
  189. name = name.lower().strip()
  190. warns = await get_warns(chat_id)
  191. warns[name] = warn
  192. await warnsdb.update_one(
  193. {"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
  194. )
  195. async def remove_warns(chat_id: int, name: str) -> bool:
  196. warnsd = await get_warns(chat_id)
  197. name = name.lower().strip()
  198. if name in warnsd:
  199. del warnsd[name]
  200. await warnsdb.update_one(
  201. {"chat_id": chat_id},
  202. {"$set": {"warns": warnsd}},
  203. upsert=True,
  204. )
  205. return True
  206. return False
  207. """ Karma functions """
  208. async def get_karmas_count() -> dict:
  209. chats = karmadb.find({"chat_id": {"$lt": 0}})
  210. if not chats:
  211. return {}
  212. chats_count = 0
  213. karmas_count = 0
  214. for chat in await chats.to_list(length=1000000):
  215. for i in chat["karma"]:
  216. karma_ = chat["karma"][i]["karma"]
  217. if karma_ > 0:
  218. karmas_count += karma_
  219. chats_count += 1
  220. return {"chats_count": chats_count, "karmas_count": karmas_count}
  221. async def user_global_karma(user_id) -> int:
  222. chats = karmadb.find({"chat_id": {"$lt": 0}})
  223. if not chats:
  224. return 0
  225. total_karma = 0
  226. for chat in await chats.to_list(length=1000000):
  227. karma = await get_karma(
  228. chat["chat_id"], await int_to_alpha(user_id)
  229. )
  230. if karma:
  231. if int(karma["karma"]) > 0:
  232. total_karma += int(karma["karma"])
  233. return total_karma
  234. async def get_karmas(chat_id: int) -> Dict[str, int]:
  235. karma = await karmadb.find_one({"chat_id": chat_id})
  236. if not karma:
  237. return {}
  238. return karma["karma"]
  239. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  240. name = name.lower().strip()
  241. karmas = await get_karmas(chat_id)
  242. if name in karmas:
  243. return karmas[name]
  244. async def update_karma(chat_id: int, name: str, karma: dict):
  245. name = name.lower().strip()
  246. karmas = await get_karmas(chat_id)
  247. karmas[name] = karma
  248. await karmadb.update_one(
  249. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  250. )
  251. async def is_karma_on(chat_id: int) -> bool:
  252. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  253. if not chat:
  254. return True
  255. return False
  256. async def karma_on(chat_id: int):
  257. is_karma = await is_karma_on(chat_id)
  258. if is_karma:
  259. return
  260. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  261. async def karma_off(chat_id: int):
  262. is_karma = await is_karma_on(chat_id)
  263. if not is_karma:
  264. return
  265. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  266. """ Chats log functions """
  267. async def is_served_chat(chat_id: int) -> bool:
  268. chat = await chatsdb.find_one({"chat_id": chat_id})
  269. if not chat:
  270. return False
  271. return True
  272. async def get_served_chats() -> list:
  273. chats = chatsdb.find({"chat_id": {"$lt": 0}})
  274. if not chats:
  275. return []
  276. chats_list = []
  277. for chat in await chats.to_list(length=1000000000):
  278. chats_list.append(chat)
  279. return chats_list
  280. async def add_served_chat(chat_id: int):
  281. is_served = await is_served_chat(chat_id)
  282. if is_served:
  283. return
  284. return await chatsdb.insert_one({"chat_id": chat_id})
  285. async def remove_served_chat(chat_id: int):
  286. is_served = await is_served_chat(chat_id)
  287. if not is_served:
  288. return
  289. return await chatsdb.delete_one({"chat_id": chat_id})
  290. """ USER LOG FUNCTIONS """
  291. async def is_served_user(user_id: int) -> bool:
  292. user = await usersdb.find_one({"user_id": user_id})
  293. if not user:
  294. return False
  295. return True
  296. async def get_served_users() -> list:
  297. users = usersdb.find({"user_id": {"$gt": 0}})
  298. if not users:
  299. return []
  300. users_list = []
  301. for user in await users.to_list(length=1000000000):
  302. users_list.append(user)
  303. return users_list
  304. async def add_served_user(user_id: int):
  305. is_served = await is_served_user(user_id)
  306. if is_served:
  307. return
  308. return await usersdb.insert_one({"user_id": user_id})
  309. """ Gban functions """
  310. async def get_gbans_count() -> int:
  311. users = gbansdb.find({"user_id": {"$gt": 0}})
  312. users = await users.to_list(length=100000)
  313. return len(users)
  314. async def is_gbanned_user(user_id: int) -> bool:
  315. user = await gbansdb.find_one({"user_id": user_id})
  316. if not user:
  317. return False
  318. return True
  319. async def add_gban_user(user_id: int):
  320. is_gbanned = await is_gbanned_user(user_id)
  321. if is_gbanned:
  322. return
  323. return await gbansdb.insert_one({"user_id": user_id})
  324. async def remove_gban_user(user_id: int):
  325. is_gbanned = await is_gbanned_user(user_id)
  326. if not is_gbanned:
  327. return
  328. return await gbansdb.delete_one({"user_id": user_id})
  329. # Couple Chooser
  330. async def _get_lovers(chat_id: int):
  331. lovers = await coupledb.find_one({"chat_id": chat_id})
  332. if not lovers:
  333. return {}
  334. return lovers["couple"]
  335. async def get_couple(chat_id: int, date: str):
  336. lovers = await _get_lovers(chat_id)
  337. if date in lovers:
  338. return lovers[date]
  339. else:
  340. return False
  341. async def save_couple(chat_id: int, date: str, couple: dict):
  342. lovers = await _get_lovers(chat_id)
  343. lovers[date] = couple
  344. await coupledb.update_one(
  345. {"chat_id": chat_id},
  346. {"$set": {"couple": lovers}},
  347. upsert=True,
  348. )
  349. # Captcha
  350. async def is_captcha_on(chat_id: int) -> bool:
  351. chat = await captchadb.find_one({"chat_id": chat_id})
  352. if not chat:
  353. return True
  354. return False
  355. async def captcha_on(chat_id: int):
  356. is_captcha = await is_captcha_on(chat_id)
  357. if is_captcha:
  358. return
  359. return await captchadb.delete_one({"chat_id": chat_id})
  360. async def captcha_off(chat_id: int):
  361. is_captcha = await is_captcha_on(chat_id)
  362. if not is_captcha:
  363. return
  364. return await captchadb.insert_one({"chat_id": chat_id})
  365. """Anti Service System"""
  366. async def is_antiservice_on(chat_id: int) -> bool:
  367. chat = await antiservicedb.find_one({"chat_id": chat_id})
  368. if not chat:
  369. return True
  370. return False
  371. async def antiservice_on(chat_id: int):
  372. is_antiservice = await is_antiservice_on(chat_id)
  373. if is_antiservice:
  374. return
  375. return await antiservicedb.delete_one({"chat_id": chat_id})
  376. async def antiservice_off(chat_id: int):
  377. is_antiservice = await is_antiservice_on(chat_id)
  378. if not is_antiservice:
  379. return
  380. return await antiservicedb.insert_one({"chat_id": chat_id})
  381. """ PM PERMIT """
  382. async def is_pmpermit_approved(user_id: int) -> bool:
  383. user = await pmpermitdb.find_one({"user_id": user_id})
  384. if not user:
  385. return False
  386. return True
  387. async def approve_pmpermit(user_id: int):
  388. is_pmpermit = await is_pmpermit_approved(user_id)
  389. if is_pmpermit:
  390. return
  391. return await pmpermitdb.insert_one({"user_id": user_id})
  392. async def disapprove_pmpermit(user_id: int):
  393. is_pmpermit = await is_pmpermit_approved(user_id)
  394. if not is_pmpermit:
  395. return
  396. return await pmpermitdb.delete_one({"user_id": user_id})
  397. """ WELCOME FUNCTIONS """
  398. async def get_welcome(chat_id: int) -> str:
  399. text = await welcomedb.find_one({"chat_id": chat_id})
  400. return text["text"]
  401. async def set_welcome(chat_id: int, text: str):
  402. return await welcomedb.update_one(
  403. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  404. )
  405. async def del_welcome(chat_id: int):
  406. return await welcomedb.delete_one({"chat_id": chat_id})
  407. """NSFW System"""
  408. async def is_nsfw_on(chat_id: int) -> bool:
  409. chat = await nsfwdb.find_one({"chat_id": chat_id})
  410. if not chat:
  411. return True
  412. return False
  413. async def nsfw_on(chat_id: int):
  414. is_nsfw = await is_nsfw_on(chat_id)
  415. if is_nsfw:
  416. return
  417. return await nsfwdb.delete_one({"chat_id": chat_id})
  418. async def nsfw_off(chat_id: int):
  419. is_nsfw = await is_nsfw_on(chat_id)
  420. if not is_nsfw:
  421. return
  422. return await nsfwdb.insert_one({"chat_id": chat_id})
  423. """ CAPTCHA CACHE SYSTEM """
  424. async def update_captcha_cache(captcha_dict):
  425. pickle = obj_to_str(captcha_dict)
  426. await captcha_cachedb.delete_one({"captcha": "cache"})
  427. if not pickle:
  428. return
  429. await captcha_cachedb.update_one(
  430. {"captcha": "cache"},
  431. {"$set": {"pickled": pickle}},
  432. upsert=True,
  433. )
  434. async def get_captcha_cache():
  435. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  436. if not cache:
  437. return []
  438. return str_to_obj(cache["pickled"])
  439. """ BLACKLIST FILTERS SYSTEM """
  440. async def get_blacklist_filters_count() -> dict:
  441. chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
  442. if not chats:
  443. return {"chats_count": 0, "filters_count": 0}
  444. chats_count = 0
  445. filters_count = 0
  446. for chat in await chats.to_list(length=1000000000):
  447. filters = await get_blacklisted_words(chat["chat_id"])
  448. filters_count += len(filters)
  449. chats_count += 1
  450. return {
  451. "chats_count": chats_count,
  452. "filters_count": filters_count,
  453. }
  454. async def get_blacklisted_words(chat_id: int) -> List[str]:
  455. _filters = await blacklist_filtersdb.find_one(
  456. {"chat_id": chat_id}
  457. )
  458. if not _filters:
  459. return []
  460. return _filters["filters"]
  461. async def save_blacklist_filter(chat_id: int, word: str):
  462. word = word.lower().strip()
  463. _filters = await get_blacklisted_words(chat_id)
  464. _filters.append(word)
  465. await blacklist_filtersdb.update_one(
  466. {"chat_id": chat_id},
  467. {"$set": {"filters": _filters}},
  468. upsert=True,
  469. )
  470. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  471. filtersd = await get_blacklisted_words(chat_id)
  472. word = word.lower().strip()
  473. if word in filtersd:
  474. filtersd.remove(word)
  475. await blacklist_filtersdb.update_one(
  476. {"chat_id": chat_id},
  477. {"$set": {"filters": filtersd}},
  478. upsert=True,
  479. )
  480. return True
  481. return False
  482. """ PIPES SYSTEM """
  483. async def activate_pipe(
  484. from_chat_id: int, to_chat_id: int, fetcher: str
  485. ):
  486. pipes = await show_pipes()
  487. pipe = {
  488. "from_chat_id": from_chat_id,
  489. "to_chat_id": to_chat_id,
  490. "fetcher": fetcher,
  491. }
  492. pipes.append(pipe)
  493. return await pipesdb.update_one(
  494. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  495. )
  496. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  497. pipes = await show_pipes()
  498. if not pipes:
  499. return
  500. for pipe in pipes:
  501. if (
  502. pipe["from_chat_id"] == from_chat_id
  503. and pipe["to_chat_id"] == to_chat_id
  504. ):
  505. pipes.remove(pipe)
  506. return await pipesdb.update_one(
  507. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  508. )
  509. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  510. for pipe in await show_pipes():
  511. if (
  512. pipe["from_chat_id"] == from_chat_id
  513. and pipe["to_chat_id"] == to_chat_id
  514. ):
  515. return True
  516. async def show_pipes() -> list:
  517. pipes = await pipesdb.find_one({"pipe": "pipe"})
  518. if not pipes:
  519. return []
  520. return pipes["pipes"]
  521. """ SUDOERS FUNCTIONS """
  522. async def get_sudoers() -> list:
  523. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  524. if not sudoers:
  525. return []
  526. return sudoers["sudoers"]
  527. async def add_sudo(user_id: int) -> bool:
  528. sudoers = await get_sudoers()
  529. sudoers.append(user_id)
  530. await sudoersdb.update_one(
  531. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  532. )
  533. return True
  534. async def remove_sudo(user_id: int) -> bool:
  535. sudoers = await get_sudoers()
  536. sudoers.remove(user_id)
  537. await sudoersdb.update_one(
  538. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  539. )
  540. return True
  541. """ BLACKLISTED CHATS """
  542. async def blacklisted_chats() -> list:
  543. chats = blacklist_chatdb.find({"chat_id": {"$lt": 0}})
  544. return [
  545. chat["chat_id"]
  546. for chat in await chats.to_list(length=1000000000)
  547. ]
  548. async def blacklist_chat(chat_id: int) -> bool:
  549. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  550. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  551. return True
  552. return False
  553. async def whitelist_chat(chat_id: int) -> bool:
  554. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  555. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  556. return True
  557. return False
  558. """ Restart stage """
  559. async def start_restart_stage(chat_id: int, message_id: int):
  560. await restart_stagedb.update_one(
  561. {"something": "something"},
  562. {
  563. "$set": {
  564. "chat_id": chat_id,
  565. "message_id": message_id,
  566. }
  567. },
  568. upsert=True,
  569. )
  570. async def clean_restart_stage() -> dict:
  571. data = await restart_stagedb.find_one({"something": "something"})
  572. if not data:
  573. return {}
  574. await restart_stagedb.delete_one({"something": "something"})
  575. return {
  576. "chat_id": data["chat_id"],
  577. "message_id": data["message_id"],
  578. }
  579. """ TRUST DB """
  580. async def get_trust_db(user_id: int) -> list:
  581. user = await trustdb.find_one({"user_id": user_id})
  582. if not user:
  583. return []
  584. return user["data"]
  585. async def update_trust_db(user_id: int, new_data: float):
  586. user = await trustdb.find_one({"user_id": user_id})
  587. data = user["data"] if user else []
  588. if len(data) >= 100:
  589. data = data[:99]
  590. data.append(new_data)
  591. data = [
  592. i for i in data if isinstance(i, float) or isinstance(i, int)
  593. ]
  594. await trustdb.update_one(
  595. {"user_id": user_id},
  596. {"$set": {"data": data}},
  597. upsert=True,
  598. )
  599. """SPAM DETECTION System"""
  600. async def is_spam_detection_on(chat_id: int) -> bool:
  601. chat = await spam_toggle_db.find_one({"chat_id": chat_id})
  602. if not chat:
  603. return True
  604. return False
  605. async def spam_detection_on(chat_id: int):
  606. is_on = await is_spam_detection_on(chat_id)
  607. if is_on:
  608. return
  609. return await spam_toggle_db.delete_one({"chat_id": chat_id})
  610. async def spam_detection_off(chat_id: int):
  611. is_on = await is_spam_detection_on(chat_id)
  612. if not is_on:
  613. return
  614. return await spam_toggle_db.insert_one({"chat_id": chat_id})
  615. """FLOOD System"""
  616. async def is_flood_on(chat_id: int) -> bool:
  617. chat = await flood_toggle_db.find_one({"chat_id": chat_id})
  618. if not chat:
  619. return True
  620. return False
  621. async def flood_on(chat_id: int):
  622. is_flood = await is_flood_on(chat_id)
  623. if is_flood:
  624. return
  625. return await flood_toggle_db.delete_one({"chat_id": chat_id})
  626. async def flood_off(chat_id: int):
  627. is_flood = await is_flood_on(chat_id)
  628. if not is_flood:
  629. return
  630. return await flood_toggle_db.insert_one({"chat_id": chat_id})