dbfunctions.py 22 KB

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