dbfunctions.py 21 KB

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