dbfunctions.py 22 KB

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