dbfunctions.py 21 KB

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