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