dbfunctions.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 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 get_filters_count() -> dict:
  103. chats_count = 0
  104. filters_count = 0
  105. async for chat in filtersdb.find({"chat_id": {"$lt": 0}}):
  106. filters_name = await get_filters_names(chat["chat_id"])
  107. filters_count += len(filters_name)
  108. chats_count += 1
  109. return {
  110. "chats_count": chats_count,
  111. "filters_count": filters_count,
  112. }
  113. async def _get_filters(chat_id: int) -> Dict[str, int]:
  114. _filters = await filtersdb.find_one({"chat_id": chat_id})
  115. if not _filters:
  116. return {}
  117. return _filters["filters"]
  118. async def get_filters_names(chat_id: int) -> List[str]:
  119. _filters = []
  120. for _filter in await _get_filters(chat_id):
  121. _filters.append(_filter)
  122. return _filters
  123. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  124. name = name.lower().strip()
  125. _filters = await _get_filters(chat_id)
  126. if name in _filters:
  127. return _filters[name]
  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. async def int_to_alpha(user_id: int) -> str:
  151. alphabet = list(ascii_lowercase)[:10]
  152. text = ""
  153. user_id = str(user_id)
  154. for i in user_id:
  155. text += alphabet[int(i)]
  156. return text
  157. async def alpha_to_int(user_id_alphabet: str) -> int:
  158. alphabet = list(ascii_lowercase)[:10]
  159. user_id = ""
  160. for i in user_id_alphabet:
  161. index = alphabet.index(i)
  162. user_id += str(index)
  163. user_id = int(user_id)
  164. return user_id
  165. async def get_warns_count() -> dict:
  166. chats_count = 0
  167. warns_count = 0
  168. async for chat in warnsdb.find({"chat_id": {"$lt": 0}}):
  169. for user in chat["warns"]:
  170. warns_count += chat["warns"][user]["warns"]
  171. chats_count += 1
  172. return {"chats_count": chats_count, "warns_count": warns_count}
  173. async def get_warns(chat_id: int) -> Dict[str, int]:
  174. warns = await warnsdb.find_one({"chat_id": chat_id})
  175. if not warns:
  176. return {}
  177. return warns["warns"]
  178. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  179. name = name.lower().strip()
  180. warns = await get_warns(chat_id)
  181. if name in warns:
  182. return warns[name]
  183. async def add_warn(chat_id: int, name: str, warn: dict):
  184. name = name.lower().strip()
  185. warns = await get_warns(chat_id)
  186. warns[name] = warn
  187. await warnsdb.update_one(
  188. {"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
  189. )
  190. async def remove_warns(chat_id: int, name: str) -> bool:
  191. warnsd = await get_warns(chat_id)
  192. name = name.lower().strip()
  193. if name in warnsd:
  194. del warnsd[name]
  195. await warnsdb.update_one(
  196. {"chat_id": chat_id},
  197. {"$set": {"warns": warnsd}},
  198. upsert=True,
  199. )
  200. return True
  201. return False
  202. async def get_karmas_count() -> dict:
  203. chats_count = 0
  204. karmas_count = 0
  205. async for chat in karmadb.find({"chat_id": {"$lt": 0}}):
  206. for i in chat["karma"]:
  207. karma_ = chat["karma"][i]["karma"]
  208. if karma_ > 0:
  209. karmas_count += karma_
  210. chats_count += 1
  211. return {"chats_count": chats_count, "karmas_count": karmas_count}
  212. async def user_global_karma(user_id) -> int:
  213. total_karma = 0
  214. async for chat in karmadb.find({"chat_id": {"$lt": 0}}):
  215. karma = await get_karma(chat["chat_id"], await int_to_alpha(user_id))
  216. if karma and (int(karma["karma"]) > 0):
  217. total_karma += int(karma["karma"])
  218. return total_karma
  219. async def get_karmas(chat_id: int) -> Dict[str, int]:
  220. karma = await karmadb.find_one({"chat_id": chat_id})
  221. if not karma:
  222. return {}
  223. return karma["karma"]
  224. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  225. name = name.lower().strip()
  226. karmas = await get_karmas(chat_id)
  227. if name in karmas:
  228. return karmas[name]
  229. async def update_karma(chat_id: int, name: str, karma: dict):
  230. name = name.lower().strip()
  231. karmas = await get_karmas(chat_id)
  232. karmas[name] = karma
  233. await karmadb.update_one(
  234. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  235. )
  236. async def is_karma_on(chat_id: int) -> bool:
  237. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  238. if not chat:
  239. return True
  240. return False
  241. async def karma_on(chat_id: int):
  242. is_karma = await is_karma_on(chat_id)
  243. if is_karma:
  244. return
  245. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  246. async def karma_off(chat_id: int):
  247. is_karma = await is_karma_on(chat_id)
  248. if not is_karma:
  249. return
  250. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  251. async def is_served_chat(chat_id: int) -> bool:
  252. chat = await chatsdb.find_one({"chat_id": chat_id})
  253. if not chat:
  254. return False
  255. return True
  256. async def get_served_chats() -> list:
  257. chats_list = []
  258. async for chat in chatsdb.find({"chat_id": {"$lt": 0}}):
  259. chats_list.append(chat)
  260. return chats_list
  261. async def add_served_chat(chat_id: int):
  262. is_served = await is_served_chat(chat_id)
  263. if is_served:
  264. return
  265. return await chatsdb.insert_one({"chat_id": chat_id})
  266. async def remove_served_chat(chat_id: int):
  267. is_served = await is_served_chat(chat_id)
  268. if not is_served:
  269. return
  270. return await chatsdb.delete_one({"chat_id": chat_id})
  271. async def is_served_user(user_id: int) -> bool:
  272. user = await usersdb.find_one({"user_id": user_id})
  273. if not user:
  274. return False
  275. return True
  276. async def get_served_users() -> list:
  277. users_list = []
  278. async for user in usersdb.find({"user_id": {"$gt": 0}}):
  279. users_list.append(user)
  280. return users_list
  281. async def add_served_user(user_id: int):
  282. is_served = await is_served_user(user_id)
  283. if is_served:
  284. return
  285. return await usersdb.insert_one({"user_id": user_id})
  286. async def get_gbans_count() -> int:
  287. return len([i async for i in gbansdb.find({"user_id": {"$gt": 0}})])
  288. async def is_gbanned_user(user_id: int) -> bool:
  289. user = await gbansdb.find_one({"user_id": user_id})
  290. if not user:
  291. return False
  292. return True
  293. async def add_gban_user(user_id: int):
  294. is_gbanned = await is_gbanned_user(user_id)
  295. if is_gbanned:
  296. return
  297. return await gbansdb.insert_one({"user_id": user_id})
  298. async def remove_gban_user(user_id: int):
  299. is_gbanned = await is_gbanned_user(user_id)
  300. if not is_gbanned:
  301. return
  302. return await gbansdb.delete_one({"user_id": user_id})
  303. async def _get_lovers(chat_id: int):
  304. lovers = await coupledb.find_one({"chat_id": chat_id})
  305. if not lovers:
  306. return {}
  307. return lovers["couple"]
  308. async def get_couple(chat_id: int, date: str):
  309. lovers = await _get_lovers(chat_id)
  310. if date in lovers:
  311. return lovers[date]
  312. return False
  313. async def save_couple(chat_id: int, date: str, couple: dict):
  314. lovers = await _get_lovers(chat_id)
  315. lovers[date] = couple
  316. await coupledb.update_one(
  317. {"chat_id": chat_id},
  318. {"$set": {"couple": lovers}},
  319. upsert=True,
  320. )
  321. async def is_captcha_on(chat_id: int) -> bool:
  322. chat = await captchadb.find_one({"chat_id": chat_id})
  323. if not chat:
  324. return True
  325. return False
  326. async def captcha_on(chat_id: int):
  327. is_captcha = await is_captcha_on(chat_id)
  328. if is_captcha:
  329. return
  330. return await captchadb.delete_one({"chat_id": chat_id})
  331. async def captcha_off(chat_id: int):
  332. is_captcha = await is_captcha_on(chat_id)
  333. if not is_captcha:
  334. return
  335. return await captchadb.insert_one({"chat_id": chat_id})
  336. async def has_solved_captcha_once(chat_id: int, user_id: int):
  337. has_solved = await solved_captcha_db.find_one(
  338. {"chat_id": chat_id, "user_id": user_id}
  339. )
  340. return bool(has_solved)
  341. async def save_captcha_solved(chat_id: int, user_id: int):
  342. return await solved_captcha_db.update_one(
  343. {"chat_id": chat_id},
  344. {"$set": {"user_id": user_id}},
  345. upsert=True,
  346. )
  347. async def is_antiservice_on(chat_id: int) -> bool:
  348. chat = await antiservicedb.find_one({"chat_id": chat_id})
  349. if not chat:
  350. return True
  351. return False
  352. async def antiservice_on(chat_id: int):
  353. is_antiservice = await is_antiservice_on(chat_id)
  354. if is_antiservice:
  355. return
  356. return await antiservicedb.delete_one({"chat_id": chat_id})
  357. async def antiservice_off(chat_id: int):
  358. is_antiservice = await is_antiservice_on(chat_id)
  359. if not is_antiservice:
  360. return
  361. return await antiservicedb.insert_one({"chat_id": chat_id})
  362. async def is_pmpermit_approved(user_id: int) -> bool:
  363. user = await pmpermitdb.find_one({"user_id": user_id})
  364. if not user:
  365. return False
  366. return True
  367. async def approve_pmpermit(user_id: int):
  368. is_pmpermit = await is_pmpermit_approved(user_id)
  369. if is_pmpermit:
  370. return
  371. return await pmpermitdb.insert_one({"user_id": user_id})
  372. async def disapprove_pmpermit(user_id: int):
  373. is_pmpermit = await is_pmpermit_approved(user_id)
  374. if not is_pmpermit:
  375. return
  376. return await pmpermitdb.delete_one({"user_id": user_id})
  377. async def get_welcome(chat_id: int) -> str:
  378. text = await welcomedb.find_one({"chat_id": chat_id})
  379. if not text:
  380. return ""
  381. return text["text"]
  382. async def set_welcome(chat_id: int, text: str):
  383. return await welcomedb.update_one(
  384. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  385. )
  386. async def del_welcome(chat_id: int):
  387. return await welcomedb.delete_one({"chat_id": chat_id})
  388. async def update_captcha_cache(captcha_dict):
  389. pickle = obj_to_str(captcha_dict)
  390. await captcha_cachedb.delete_one({"captcha": "cache"})
  391. if not pickle:
  392. return
  393. await captcha_cachedb.update_one(
  394. {"captcha": "cache"},
  395. {"$set": {"pickled": pickle}},
  396. upsert=True,
  397. )
  398. async def get_captcha_cache():
  399. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  400. if not cache:
  401. return []
  402. return str_to_obj(cache["pickled"])
  403. async def get_blacklist_filters_count() -> dict:
  404. chats_count = 0
  405. filters_count = 0
  406. async for chat in blacklist_filtersdb.find({"chat_id": {"$lt": 0}}):
  407. filters = await get_blacklisted_words(chat["chat_id"])
  408. filters_count += len(filters)
  409. chats_count += 1
  410. return {
  411. "chats_count": chats_count,
  412. "filters_count": filters_count,
  413. }
  414. async def get_blacklisted_words(chat_id: int) -> List[str]:
  415. _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
  416. if not _filters:
  417. return []
  418. return _filters["filters"]
  419. async def save_blacklist_filter(chat_id: int, word: str):
  420. word = word.lower().strip()
  421. _filters = await get_blacklisted_words(chat_id)
  422. _filters.append(word)
  423. await blacklist_filtersdb.update_one(
  424. {"chat_id": chat_id},
  425. {"$set": {"filters": _filters}},
  426. upsert=True,
  427. )
  428. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  429. filtersd = await get_blacklisted_words(chat_id)
  430. word = word.lower().strip()
  431. if word in filtersd:
  432. filtersd.remove(word)
  433. await blacklist_filtersdb.update_one(
  434. {"chat_id": chat_id},
  435. {"$set": {"filters": filtersd}},
  436. upsert=True,
  437. )
  438. return True
  439. return False
  440. async def activate_pipe(from_chat_id: int, to_chat_id: int, fetcher: str):
  441. pipes = await show_pipes()
  442. pipe = {
  443. "from_chat_id": from_chat_id,
  444. "to_chat_id": to_chat_id,
  445. "fetcher": fetcher,
  446. }
  447. pipes.append(pipe)
  448. return await pipesdb.update_one(
  449. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  450. )
  451. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  452. pipes = await show_pipes()
  453. if not pipes:
  454. return
  455. for pipe in pipes:
  456. if (
  457. pipe["from_chat_id"] == from_chat_id
  458. and pipe["to_chat_id"] == to_chat_id
  459. ):
  460. pipes.remove(pipe)
  461. return await pipesdb.update_one(
  462. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  463. )
  464. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  465. for pipe in await show_pipes():
  466. if (
  467. pipe["from_chat_id"] == from_chat_id
  468. and pipe["to_chat_id"] == to_chat_id
  469. ):
  470. return True
  471. async def show_pipes() -> list:
  472. pipes = await pipesdb.find_one({"pipe": "pipe"})
  473. if not pipes:
  474. return []
  475. return pipes["pipes"]
  476. async def get_sudoers() -> list:
  477. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  478. if not sudoers:
  479. return []
  480. return sudoers["sudoers"]
  481. async def add_sudo(user_id: int) -> bool:
  482. sudoers = await get_sudoers()
  483. sudoers.append(user_id)
  484. await sudoersdb.update_one(
  485. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  486. )
  487. return True
  488. async def remove_sudo(user_id: int) -> bool:
  489. sudoers = await get_sudoers()
  490. sudoers.remove(user_id)
  491. await sudoersdb.update_one(
  492. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  493. )
  494. return True
  495. async def blacklisted_chats() -> list:
  496. blacklist_chat = []
  497. async for chat in blacklist_chatdb.find({"chat_id": {"$lt": 0}}):
  498. blacklist_chat.append(chat["chat_id"])
  499. return blacklist_chat
  500. async def blacklist_chat(chat_id: int) -> bool:
  501. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  502. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  503. return True
  504. return False
  505. async def whitelist_chat(chat_id: int) -> bool:
  506. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  507. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  508. return True
  509. return False
  510. async def start_restart_stage(chat_id: int, message_id: int):
  511. await restart_stagedb.update_one(
  512. {"something": "something"},
  513. {
  514. "$set": {
  515. "chat_id": chat_id,
  516. "message_id": message_id,
  517. }
  518. },
  519. upsert=True,
  520. )
  521. async def clean_restart_stage() -> dict:
  522. data = await restart_stagedb.find_one({"something": "something"})
  523. if not data:
  524. return {}
  525. await restart_stagedb.delete_one({"something": "something"})
  526. return {
  527. "chat_id": data["chat_id"],
  528. "message_id": data["message_id"],
  529. }
  530. async def is_flood_on(chat_id: int) -> bool:
  531. chat = await flood_toggle_db.find_one({"chat_id": chat_id})
  532. if not chat:
  533. return True
  534. return False
  535. async def flood_on(chat_id: int):
  536. is_flood = await is_flood_on(chat_id)
  537. if is_flood:
  538. return
  539. return await flood_toggle_db.delete_one({"chat_id": chat_id})
  540. async def flood_off(chat_id: int):
  541. is_flood = await is_flood_on(chat_id)
  542. if not is_flood:
  543. return
  544. return await flood_toggle_db.insert_one({"chat_id": chat_id})
  545. async def add_rss_feed(chat_id: int, url: str, last_title: str):
  546. return await rssdb.update_one(
  547. {"chat_id": chat_id},
  548. {"$set": {"url": url, "last_title": last_title}},
  549. upsert=True,
  550. )
  551. async def remove_rss_feed(chat_id: int):
  552. return await rssdb.delete_one({"chat_id": chat_id})
  553. async def update_rss_feed(chat_id: int, last_title: str):
  554. return await rssdb.update_one(
  555. {"chat_id": chat_id},
  556. {"$set": {"last_title": last_title}},
  557. upsert=True,
  558. )
  559. async def is_rss_active(chat_id: int) -> bool:
  560. return await rssdb.find_one({"chat_id": chat_id})
  561. async def get_rss_feeds() -> list:
  562. data = []
  563. async for feed in rssdb.find({"chat_id": {"$exists": 1}}):
  564. data.append(
  565. dict(
  566. chat_id=feed["chat_id"],
  567. url=feed["url"],
  568. last_title=feed["last_title"],
  569. )
  570. )
  571. return data
  572. async def get_rss_feeds_count() -> int:
  573. return len([i async for i in rssdb.find({"chat_id": {"$exists": 1}})])
  574. async def check_chatbot():
  575. return (
  576. await chatbotdb.find_one({"chatbot": "chatbot"}) or {"bot": [], "userbot": []}
  577. )
  578. async def add_chatbot(chat_id: int, is_userbot: bool = False):
  579. list_id = await check_chatbot()
  580. if is_userbot:
  581. list_id["userbot"].append(chat_id)
  582. else:
  583. list_id["bot"].append(chat_id)
  584. await chatbotdb.update_one({"chatbot": "chatbot"}, {"$set": list_id}, upsert=True)
  585. async def rm_chatbot(chat_id: int, is_userbot: bool = False):
  586. list_id = await check_chatbot()
  587. if is_userbot:
  588. list_id["userbot"].remove(chat_id)
  589. else:
  590. list_id["bot"].remove(chat_id)
  591. await chatbotdb.update_one({"chatbot": "chatbot"}, {"$set": list_id}, upsert=True)