dbfunctions.py 22 KB

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