dbfunctions.py 21 KB

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