dbfunctions.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. """
  2. MIT License
  3. Copyright (c) 2023 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 string import ascii_lowercase
  23. from typing import Dict, List, Union
  24. from wbb import db
  25. # SOME THINGS ARE FUCKED UP HERE, LIKE TOGGLEABLES HAVE THEIR OWN COLLECTION
  26. # (SHOULD FIX IT WITH SOMETHING LIKE TOGGLEDB), MOST OF THE CODE IS BAD AF
  27. # AND NEEDS TO BE REWRITTEN, BUT I WON'T, AS IT WILL TAKE
  28. # TOO MUCH TIME AND WILL BE BAD FOR ALREADY STORED DATA
  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. solved_captcha_db = db.solved_captcha
  39. captcha_cachedb = db.captcha_cache
  40. antiservicedb = db.antiservice
  41. pmpermitdb = db.pmpermit
  42. welcomedb = db.welcome_text
  43. blacklist_filtersdb = db.blacklistFilters
  44. pipesdb = db.pipes
  45. sudoersdb = db.sudoers
  46. blacklist_chatdb = db.blacklistChat
  47. restart_stagedb = db.restart_stage
  48. flood_toggle_db = db.flood_toggle
  49. rssdb = db.rss
  50. chatbotdb = db.chatbot
  51. def obj_to_str(obj):
  52. if not obj:
  53. return False
  54. string = codecs.encode(pickle.dumps(obj), "base64").decode()
  55. return string
  56. def str_to_obj(string: str):
  57. obj = pickle.loads(codecs.decode(string.encode(), "base64"))
  58. return obj
  59. async def get_notes_count() -> dict:
  60. chats_count = 0
  61. notes_count = 0
  62. async for chat in notesdb.find({"chat_id": {"$exists": 1}}):
  63. notes_name = await get_note_names(chat["chat_id"])
  64. notes_count += len(notes_name)
  65. chats_count += 1
  66. return {"chats_count": chats_count, "notes_count": notes_count}
  67. async def _get_notes(chat_id: int) -> Dict[str, int]:
  68. _notes = await notesdb.find_one({"chat_id": chat_id})
  69. if not _notes:
  70. return {}
  71. return _notes["notes"]
  72. async def get_note_names(chat_id: int) -> List[str]:
  73. _notes = []
  74. for note in await _get_notes(chat_id):
  75. _notes.append(note)
  76. return _notes
  77. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  78. name = name.lower().strip()
  79. _notes = await _get_notes(chat_id)
  80. if name in _notes:
  81. return _notes[name]
  82. return False
  83. async def save_note(chat_id: int, name: str, note: dict):
  84. name = name.lower().strip()
  85. _notes = await _get_notes(chat_id)
  86. _notes[name] = note
  87. await notesdb.update_one(
  88. {"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True
  89. )
  90. async def delete_note(chat_id: int, name: str) -> bool:
  91. notesd = await _get_notes(chat_id)
  92. name = name.lower().strip()
  93. if name in notesd:
  94. del notesd[name]
  95. await notesdb.update_one(
  96. {"chat_id": chat_id},
  97. {"$set": {"notes": notesd}},
  98. upsert=True,
  99. )
  100. return True
  101. return False
  102. async def deleteall_notes(chat_id: int):
  103. return await notesdb.delete_one({"chat_id": chat_id})
  104. async def get_filters_count() -> dict:
  105. chats_count = 0
  106. filters_count = 0
  107. async for chat in filtersdb.find({"chat_id": {"$lt": 0}}):
  108. filters_name = await get_filters_names(chat["chat_id"])
  109. filters_count += len(filters_name)
  110. chats_count += 1
  111. return {
  112. "chats_count": chats_count,
  113. "filters_count": filters_count,
  114. }
  115. async def _get_filters(chat_id: int) -> Dict[str, int]:
  116. _filters = await filtersdb.find_one({"chat_id": chat_id})
  117. if not _filters:
  118. return {}
  119. return _filters["filters"]
  120. async def get_filters_names(chat_id: int) -> List[str]:
  121. _filters = []
  122. for _filter in await _get_filters(chat_id):
  123. _filters.append(_filter)
  124. return _filters
  125. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  126. name = name.lower().strip()
  127. _filters = await _get_filters(chat_id)
  128. if name in _filters:
  129. return _filters[name]
  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. async def deleteall_filters(chat_id: int):
  153. return await filtersdb.delete_one({"chat_id": chat_id})
  154. async def int_to_alpha(user_id: int) -> str:
  155. alphabet = list(ascii_lowercase)[:10]
  156. text = ""
  157. user_id = str(user_id)
  158. for i in user_id:
  159. text += alphabet[int(i)]
  160. return text
  161. async def alpha_to_int(user_id_alphabet: str) -> int:
  162. alphabet = list(ascii_lowercase)[:10]
  163. user_id = ""
  164. for i in user_id_alphabet:
  165. index = alphabet.index(i)
  166. user_id += str(index)
  167. user_id = int(user_id)
  168. return user_id
  169. async def get_warns_count() -> dict:
  170. chats_count = 0
  171. warns_count = 0
  172. async for chat in warnsdb.find({"chat_id": {"$lt": 0}}):
  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. async def get_karmas_count() -> dict:
  207. chats_count = 0
  208. karmas_count = 0
  209. async for chat in karmadb.find({"chat_id": {"$lt": 0}}):
  210. for i in chat["karma"]:
  211. karma_ = chat["karma"][i]["karma"]
  212. if karma_ > 0:
  213. karmas_count += karma_
  214. chats_count += 1
  215. return {"chats_count": chats_count, "karmas_count": karmas_count}
  216. async def user_global_karma(user_id) -> int:
  217. total_karma = 0
  218. async for chat in karmadb.find({"chat_id": {"$lt": 0}}):
  219. karma = await get_karma(chat["chat_id"], await int_to_alpha(user_id))
  220. if karma and (int(karma["karma"]) > 0):
  221. total_karma += int(karma["karma"])
  222. return total_karma
  223. async def get_karmas(chat_id: int) -> Dict[str, int]:
  224. karma = await karmadb.find_one({"chat_id": chat_id})
  225. if not karma:
  226. return {}
  227. return karma["karma"]
  228. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  229. name = name.lower().strip()
  230. karmas = await get_karmas(chat_id)
  231. if name in karmas:
  232. return karmas[name]
  233. async def update_karma(chat_id: int, name: str, karma: dict):
  234. name = name.lower().strip()
  235. karmas = await get_karmas(chat_id)
  236. karmas[name] = karma
  237. await karmadb.update_one(
  238. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  239. )
  240. async def is_karma_on(chat_id: int) -> bool:
  241. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  242. if not chat:
  243. return True
  244. return False
  245. async def karma_on(chat_id: int):
  246. is_karma = await is_karma_on(chat_id)
  247. if is_karma:
  248. return
  249. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  250. async def karma_off(chat_id: int):
  251. is_karma = await is_karma_on(chat_id)
  252. if not is_karma:
  253. return
  254. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  255. async def is_served_chat(chat_id: int) -> bool:
  256. chat = await chatsdb.find_one({"chat_id": chat_id})
  257. if not chat:
  258. return False
  259. return True
  260. async def get_served_chats() -> list:
  261. chats_list = []
  262. async for chat in chatsdb.find({"chat_id": {"$lt": 0}}):
  263. chats_list.append(chat)
  264. return chats_list
  265. async def add_served_chat(chat_id: int):
  266. is_served = await is_served_chat(chat_id)
  267. if is_served:
  268. return
  269. return await chatsdb.insert_one({"chat_id": chat_id})
  270. async def remove_served_chat(chat_id: int):
  271. is_served = await is_served_chat(chat_id)
  272. if not is_served:
  273. return
  274. return await chatsdb.delete_one({"chat_id": chat_id})
  275. async def is_served_user(user_id: int) -> bool:
  276. user = await usersdb.find_one({"user_id": user_id})
  277. if not user:
  278. return False
  279. return True
  280. async def get_served_users() -> list:
  281. users_list = []
  282. async for user in usersdb.find({"user_id": {"$gt": 0}}):
  283. users_list.append(user)
  284. return users_list
  285. async def add_served_user(user_id: int):
  286. is_served = await is_served_user(user_id)
  287. if is_served:
  288. return
  289. return await usersdb.insert_one({"user_id": user_id})
  290. async def get_gbans_count() -> int:
  291. return len([i async for i in gbansdb.find({"user_id": {"$gt": 0}})])
  292. async def is_gbanned_user(user_id: int) -> bool:
  293. user = await gbansdb.find_one({"user_id": user_id})
  294. if not user:
  295. return False
  296. return True
  297. async def add_gban_user(user_id: int):
  298. is_gbanned = await is_gbanned_user(user_id)
  299. if is_gbanned:
  300. return
  301. return await gbansdb.insert_one({"user_id": user_id})
  302. async def remove_gban_user(user_id: int):
  303. is_gbanned = await is_gbanned_user(user_id)
  304. if not is_gbanned:
  305. return
  306. return await gbansdb.delete_one({"user_id": user_id})
  307. async def _get_lovers(chat_id: int):
  308. lovers = await coupledb.find_one({"chat_id": chat_id})
  309. if not lovers:
  310. return {}
  311. return lovers["couple"]
  312. async def get_couple(chat_id: int, date: str):
  313. lovers = await _get_lovers(chat_id)
  314. if date in lovers:
  315. return lovers[date]
  316. return False
  317. async def save_couple(chat_id: int, date: str, couple: dict):
  318. lovers = await _get_lovers(chat_id)
  319. lovers[date] = couple
  320. await coupledb.update_one(
  321. {"chat_id": chat_id},
  322. {"$set": {"couple": lovers}},
  323. upsert=True,
  324. )
  325. async def is_captcha_on(chat_id: int) -> bool:
  326. chat = await captchadb.find_one({"chat_id": chat_id})
  327. if not chat:
  328. return True
  329. return False
  330. async def captcha_on(chat_id: int):
  331. is_captcha = await is_captcha_on(chat_id)
  332. if is_captcha:
  333. return
  334. return await captchadb.delete_one({"chat_id": chat_id})
  335. async def captcha_off(chat_id: int):
  336. is_captcha = await is_captcha_on(chat_id)
  337. if not is_captcha:
  338. return
  339. return await captchadb.insert_one({"chat_id": chat_id})
  340. async def has_solved_captcha_once(chat_id: int, user_id: int):
  341. has_solved = await solved_captcha_db.find_one(
  342. {"chat_id": chat_id, "user_id": user_id}
  343. )
  344. return bool(has_solved)
  345. async def save_captcha_solved(chat_id: int, user_id: int):
  346. return await solved_captcha_db.update_one(
  347. {"chat_id": chat_id},
  348. {"$set": {"user_id": user_id}},
  349. upsert=True,
  350. )
  351. async def is_antiservice_on(chat_id: int) -> bool:
  352. chat = await antiservicedb.find_one({"chat_id": chat_id})
  353. if not chat:
  354. return True
  355. return False
  356. async def antiservice_on(chat_id: int):
  357. is_antiservice = await is_antiservice_on(chat_id)
  358. if is_antiservice:
  359. return
  360. return await antiservicedb.delete_one({"chat_id": chat_id})
  361. async def antiservice_off(chat_id: int):
  362. is_antiservice = await is_antiservice_on(chat_id)
  363. if not is_antiservice:
  364. return
  365. return await antiservicedb.insert_one({"chat_id": chat_id})
  366. async def is_pmpermit_approved(user_id: int) -> bool:
  367. user = await pmpermitdb.find_one({"user_id": user_id})
  368. if not user:
  369. return False
  370. return True
  371. async def approve_pmpermit(user_id: int):
  372. is_pmpermit = await is_pmpermit_approved(user_id)
  373. if is_pmpermit:
  374. return
  375. return await pmpermitdb.insert_one({"user_id": user_id})
  376. async def disapprove_pmpermit(user_id: int):
  377. is_pmpermit = await is_pmpermit_approved(user_id)
  378. if not is_pmpermit:
  379. return
  380. return await pmpermitdb.delete_one({"user_id": user_id})
  381. async def get_welcome(chat_id: int) -> (str, str, str):
  382. data = await welcomedb.find_one({"chat_id": chat_id})
  383. if not data:
  384. return "", "", ""
  385. welcome = data.get("welcome", "")
  386. raw_text = data.get("raw_text", "")
  387. file_id = data.get("file_id", "")
  388. return welcome, raw_text, file_id
  389. async def set_welcome(chat_id: int, welcome: str, raw_text: str, file_id: str):
  390. update_data = {"welcome": welcome, "raw_text": raw_text, "file_id": file_id}
  391. return await welcomedb.update_one(
  392. {"chat_id": chat_id},
  393. {"$set": update_data},
  394. upsert=True
  395. )
  396. async def del_welcome(chat_id: int):
  397. return await welcomedb.delete_one({"chat_id": chat_id})
  398. async def update_captcha_cache(captcha_dict):
  399. pickle = obj_to_str(captcha_dict)
  400. await captcha_cachedb.delete_one({"captcha": "cache"})
  401. if not pickle:
  402. return
  403. await captcha_cachedb.update_one(
  404. {"captcha": "cache"},
  405. {"$set": {"pickled": pickle}},
  406. upsert=True,
  407. )
  408. async def get_captcha_cache():
  409. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  410. if not cache:
  411. return []
  412. return str_to_obj(cache["pickled"])
  413. async def get_blacklist_filters_count() -> dict:
  414. chats_count = 0
  415. filters_count = 0
  416. async for chat in blacklist_filtersdb.find({"chat_id": {"$lt": 0}}):
  417. filters = await get_blacklisted_words(chat["chat_id"])
  418. filters_count += len(filters)
  419. chats_count += 1
  420. return {
  421. "chats_count": chats_count,
  422. "filters_count": filters_count,
  423. }
  424. async def get_blacklisted_words(chat_id: int) -> List[str]:
  425. _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
  426. if not _filters:
  427. return []
  428. return _filters["filters"]
  429. async def save_blacklist_filter(chat_id: int, word: str):
  430. word = word.lower().strip()
  431. _filters = await get_blacklisted_words(chat_id)
  432. _filters.append(word)
  433. await blacklist_filtersdb.update_one(
  434. {"chat_id": chat_id},
  435. {"$set": {"filters": _filters}},
  436. upsert=True,
  437. )
  438. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  439. filtersd = await get_blacklisted_words(chat_id)
  440. word = word.lower().strip()
  441. if word in filtersd:
  442. filtersd.remove(word)
  443. await blacklist_filtersdb.update_one(
  444. {"chat_id": chat_id},
  445. {"$set": {"filters": filtersd}},
  446. upsert=True,
  447. )
  448. return True
  449. return False
  450. async def activate_pipe(from_chat_id: int, to_chat_id: int, fetcher: str):
  451. pipes = await show_pipes()
  452. pipe = {
  453. "from_chat_id": from_chat_id,
  454. "to_chat_id": to_chat_id,
  455. "fetcher": fetcher,
  456. }
  457. pipes.append(pipe)
  458. return await pipesdb.update_one(
  459. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  460. )
  461. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  462. pipes = await show_pipes()
  463. if not pipes:
  464. return
  465. for pipe in pipes:
  466. if pipe["from_chat_id"] == from_chat_id and pipe["to_chat_id"] == to_chat_id:
  467. pipes.remove(pipe)
  468. return await pipesdb.update_one(
  469. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  470. )
  471. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  472. for pipe in await show_pipes():
  473. if pipe["from_chat_id"] == from_chat_id and pipe["to_chat_id"] == to_chat_id:
  474. return True
  475. async def show_pipes() -> list:
  476. pipes = await pipesdb.find_one({"pipe": "pipe"})
  477. if not pipes:
  478. return []
  479. return pipes["pipes"]
  480. async def get_sudoers() -> list:
  481. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  482. if not sudoers:
  483. return []
  484. return sudoers["sudoers"]
  485. async def add_sudo(user_id: int) -> bool:
  486. sudoers = await get_sudoers()
  487. sudoers.append(user_id)
  488. await sudoersdb.update_one(
  489. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  490. )
  491. return True
  492. async def remove_sudo(user_id: int) -> bool:
  493. sudoers = await get_sudoers()
  494. sudoers.remove(user_id)
  495. await sudoersdb.update_one(
  496. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  497. )
  498. return True
  499. async def blacklisted_chats() -> list:
  500. blacklist_chat = []
  501. async for chat in blacklist_chatdb.find({"chat_id": {"$lt": 0}}):
  502. blacklist_chat.append(chat["chat_id"])
  503. return blacklist_chat
  504. async def blacklist_chat(chat_id: int) -> bool:
  505. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  506. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  507. return True
  508. return False
  509. async def whitelist_chat(chat_id: int) -> bool:
  510. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  511. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  512. return True
  513. return False
  514. async def start_restart_stage(chat_id: int, message_id: int):
  515. await restart_stagedb.update_one(
  516. {"something": "something"},
  517. {
  518. "$set": {
  519. "chat_id": chat_id,
  520. "message_id": message_id,
  521. }
  522. },
  523. upsert=True,
  524. )
  525. async def clean_restart_stage() -> dict:
  526. data = await restart_stagedb.find_one({"something": "something"})
  527. if not data:
  528. return {}
  529. await restart_stagedb.delete_one({"something": "something"})
  530. return {
  531. "chat_id": data["chat_id"],
  532. "message_id": data["message_id"],
  533. }
  534. async def is_flood_on(chat_id: int) -> bool:
  535. chat = await flood_toggle_db.find_one({"chat_id": chat_id})
  536. if not chat:
  537. return True
  538. return False
  539. async def flood_on(chat_id: int):
  540. is_flood = await is_flood_on(chat_id)
  541. if is_flood:
  542. return
  543. return await flood_toggle_db.delete_one({"chat_id": chat_id})
  544. async def flood_off(chat_id: int):
  545. is_flood = await is_flood_on(chat_id)
  546. if not is_flood:
  547. return
  548. return await flood_toggle_db.insert_one({"chat_id": chat_id})
  549. async def add_rss_feed(chat_id: int, url: str, last_title: str):
  550. return await rssdb.update_one(
  551. {"chat_id": chat_id},
  552. {"$set": {"url": url, "last_title": last_title}},
  553. upsert=True,
  554. )
  555. async def remove_rss_feed(chat_id: int):
  556. return await rssdb.delete_one({"chat_id": chat_id})
  557. async def update_rss_feed(chat_id: int, last_title: str):
  558. return await rssdb.update_one(
  559. {"chat_id": chat_id},
  560. {"$set": {"last_title": last_title}},
  561. upsert=True,
  562. )
  563. async def is_rss_active(chat_id: int) -> bool:
  564. return await rssdb.find_one({"chat_id": chat_id})
  565. async def get_rss_feeds() -> list:
  566. data = []
  567. async for feed in rssdb.find({"chat_id": {"$exists": 1}}):
  568. data.append(
  569. dict(
  570. chat_id=feed["chat_id"],
  571. url=feed["url"],
  572. last_title=feed["last_title"],
  573. )
  574. )
  575. return data
  576. async def get_rss_feeds_count() -> int:
  577. return len([i async for i in rssdb.find({"chat_id": {"$exists": 1}})])
  578. async def check_chatbot():
  579. return await chatbotdb.find_one({"chatbot": "chatbot"}) or {
  580. "bot": [],
  581. "userbot": [],
  582. }
  583. async def add_chatbot(chat_id: int, is_userbot: bool = False):
  584. list_id = await check_chatbot()
  585. if is_userbot:
  586. list_id["userbot"].append(chat_id)
  587. else:
  588. list_id["bot"].append(chat_id)
  589. await chatbotdb.update_one({"chatbot": "chatbot"}, {"$set": list_id}, upsert=True)
  590. async def rm_chatbot(chat_id: int, is_userbot: bool = False):
  591. list_id = await check_chatbot()
  592. if is_userbot:
  593. list_id["userbot"].remove(chat_id)
  594. else:
  595. list_id["bot"].remove(chat_id)
  596. await chatbotdb.update_one({"chatbot": "chatbot"}, {"$set": list_id}, upsert=True)