dbfunctions.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. from typing import Dict, List, Union
  21. from wbb import db
  22. from wbb.utils.functions import obj_to_str, str_to_obj
  23. notesdb = db.notes
  24. filtersdb = db.filters
  25. warnsdb = db.warns
  26. karmadb = db.karma
  27. chatsdb = db.chats
  28. usersdb = db.users
  29. gbansdb = db.gban
  30. coupledb = db.couple
  31. captchadb = db.captcha
  32. antiservicedb = db.antiservice
  33. pmpermitdb = db.pmpermit
  34. welcomedb = db.welcome_text
  35. nsfwdb = db.nsfw
  36. captcha_cachedb = db.captcha_cache
  37. blacklist_filtersdb = db.blacklistFilters
  38. pipesdb = db.pipes
  39. sudoersdb = db.sudoers
  40. blacklist_chatdb = db.blacklistChat
  41. restart_stagedb = db.restart_stage
  42. """ Notes functions """
  43. async def get_notes_count() -> dict:
  44. chats = notesdb.find({"chat_id": {"$lt": 0}})
  45. if not chats:
  46. return {}
  47. chats_count = 0
  48. notes_count = 0
  49. for chat in await chats.to_list(length=1000000000):
  50. notes_name = await get_note_names(chat["chat_id"])
  51. notes_count += len(notes_name)
  52. chats_count += 1
  53. return {"chats_count": chats_count, "notes_count": notes_count}
  54. async def _get_notes(chat_id: int) -> Dict[str, int]:
  55. _notes = await notesdb.find_one({"chat_id": chat_id})
  56. if not _notes:
  57. return {}
  58. return _notes["notes"]
  59. async def get_note_names(chat_id: int) -> List[str]:
  60. _notes = []
  61. for note in await _get_notes(chat_id):
  62. _notes.append(note)
  63. return _notes
  64. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  65. name = name.lower().strip()
  66. _notes = await _get_notes(chat_id)
  67. if name in _notes:
  68. return _notes[name]
  69. else:
  70. return False
  71. async def save_note(chat_id: int, name: str, note: dict):
  72. name = name.lower().strip()
  73. _notes = await _get_notes(chat_id)
  74. _notes[name] = note
  75. await notesdb.update_one(
  76. {"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True
  77. )
  78. async def delete_note(chat_id: int, name: str) -> bool:
  79. notesd = await _get_notes(chat_id)
  80. name = name.lower().strip()
  81. if name in notesd:
  82. del notesd[name]
  83. await notesdb.update_one(
  84. {"chat_id": chat_id}, {"$set": {"notes": notesd}}, upsert=True
  85. )
  86. return True
  87. return False
  88. """ Filters funcions """
  89. async def get_filters_count() -> dict:
  90. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  91. if not chats:
  92. return {}
  93. chats_count = 0
  94. filters_count = 0
  95. for chat in await chats.to_list(length=1000000000):
  96. filters_name = await get_filters_names(chat["chat_id"])
  97. filters_count += len(filters_name)
  98. chats_count += 1
  99. return {"chats_count": chats_count, "filters_count": filters_count}
  100. async def _get_filters(chat_id: int) -> Dict[str, int]:
  101. _filters = await filtersdb.find_one({"chat_id": chat_id})
  102. if not _filters:
  103. return {}
  104. return _filters["filters"]
  105. async def get_filters_names(chat_id: int) -> List[str]:
  106. _filters = []
  107. for _filter in await _get_filters(chat_id):
  108. _filters.append(_filter)
  109. return _filters
  110. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  111. name = name.lower().strip()
  112. _filters = await _get_filters(chat_id)
  113. if name in _filters:
  114. return _filters[name]
  115. else:
  116. return False
  117. async def save_filter(chat_id: int, name: str, _filter: dict):
  118. name = name.lower().strip()
  119. _filters = await _get_filters(chat_id)
  120. _filters[name] = _filter
  121. await filtersdb.update_one(
  122. {"chat_id": chat_id}, {"$set": {"filters": _filters}}, upsert=True
  123. )
  124. async def delete_filter(chat_id: int, name: str) -> bool:
  125. filtersd = await _get_filters(chat_id)
  126. name = name.lower().strip()
  127. if name in filtersd:
  128. del filtersd[name]
  129. await filtersdb.update_one(
  130. {"chat_id": chat_id}, {"$set": {"filters": filtersd}}, upsert=True
  131. )
  132. return True
  133. return False
  134. """ Warn functions """
  135. async def int_to_alpha(user_id: int) -> str:
  136. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  137. text = ""
  138. user_id = str(user_id)
  139. for i in user_id:
  140. text += alphabet[int(i)]
  141. return text
  142. async def alpha_to_int(user_id_alphabet: str) -> int:
  143. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  144. user_id = ""
  145. for i in user_id_alphabet:
  146. index = alphabet.index(i)
  147. user_id += str(index)
  148. user_id = int(user_id)
  149. return user_id
  150. async def get_warns_count() -> dict:
  151. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  152. if not chats:
  153. return {}
  154. chats_count = 0
  155. warns_count = 0
  156. for chat in await chats.to_list(length=100000000):
  157. for user in chat["warns"]:
  158. warns_count += chat["warns"][user]["warns"]
  159. chats_count += 1
  160. return {"chats_count": chats_count, "warns_count": warns_count}
  161. async def get_warns(chat_id: int) -> Dict[str, int]:
  162. warns = await warnsdb.find_one({"chat_id": chat_id})
  163. if not warns:
  164. return {}
  165. return warns["warns"]
  166. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  167. name = name.lower().strip()
  168. warns = await get_warns(chat_id)
  169. if name in warns:
  170. return warns[name]
  171. async def add_warn(chat_id: int, name: str, warn: dict):
  172. name = name.lower().strip()
  173. warns = await get_warns(chat_id)
  174. warns[name] = warn
  175. await warnsdb.update_one(
  176. {"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
  177. )
  178. async def remove_warns(chat_id: int, name: str) -> bool:
  179. warnsd = await get_warns(chat_id)
  180. name = name.lower().strip()
  181. if name in warnsd:
  182. del warnsd[name]
  183. await warnsdb.update_one(
  184. {"chat_id": chat_id}, {"$set": {"warns": warnsd}}, upsert=True
  185. )
  186. return True
  187. return False
  188. """ Karma functions """
  189. async def get_karmas_count() -> dict:
  190. chats = karmadb.find({"chat_id": {"$lt": 0}})
  191. if not chats:
  192. return {}
  193. chats_count = 0
  194. karmas_count = 0
  195. for chat in await chats.to_list(length=1000000):
  196. for i in chat["karma"]:
  197. karma_ = chat["karma"][i]["karma"]
  198. if karma_ > 0:
  199. karmas_count += karma_
  200. chats_count += 1
  201. return {"chats_count": chats_count, "karmas_count": karmas_count}
  202. async def user_global_karma(user_id) -> int:
  203. chats = karmadb.find({"chat_id": {"$lt": 0}})
  204. if not chats:
  205. return 0
  206. total_karma = 0
  207. for chat in await chats.to_list(length=1000000):
  208. karma = await get_karma(chat["chat_id"], await int_to_alpha(user_id))
  209. if karma:
  210. if int(karma["karma"]) > 0:
  211. total_karma += int(karma["karma"])
  212. return total_karma
  213. async def get_karmas(chat_id: int) -> Dict[str, int]:
  214. karma = await karmadb.find_one({"chat_id": chat_id})
  215. if not karma:
  216. return {}
  217. return karma["karma"]
  218. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  219. name = name.lower().strip()
  220. karmas = await get_karmas(chat_id)
  221. if name in karmas:
  222. return karmas[name]
  223. async def update_karma(chat_id: int, name: str, karma: dict):
  224. name = name.lower().strip()
  225. karmas = await get_karmas(chat_id)
  226. karmas[name] = karma
  227. await karmadb.update_one(
  228. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  229. )
  230. async def is_karma_on(chat_id: int) -> bool:
  231. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  232. if not chat:
  233. return True
  234. return False
  235. async def karma_on(chat_id: int):
  236. is_karma = await is_karma_on(chat_id)
  237. if is_karma:
  238. return
  239. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  240. async def karma_off(chat_id: int):
  241. is_karma = await is_karma_on(chat_id)
  242. if not is_karma:
  243. return
  244. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  245. """ Chats log functions """
  246. async def is_served_chat(chat_id: int) -> bool:
  247. chat = await chatsdb.find_one({"chat_id": chat_id})
  248. if not chat:
  249. return False
  250. return True
  251. async def get_served_chats() -> list:
  252. chats = chatsdb.find({"chat_id": {"$lt": 0}})
  253. if not chats:
  254. return []
  255. chats_list = []
  256. for chat in await chats.to_list(length=1000000000):
  257. chats_list.append(chat)
  258. return chats_list
  259. async def add_served_chat(chat_id: int):
  260. is_served = await is_served_chat(chat_id)
  261. if is_served:
  262. return
  263. return await chatsdb.insert_one({"chat_id": chat_id})
  264. async def remove_served_chat(chat_id: int):
  265. is_served = await is_served_chat(chat_id)
  266. if not is_served:
  267. return
  268. return await chatsdb.delete_one({"chat_id": chat_id})
  269. """ USER LOG FUNCTIONS """
  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 = usersdb.find({"user_id": {"$gt": 0}})
  277. if not users:
  278. return []
  279. users_list = []
  280. for user in await users.to_list(length=1000000000):
  281. users_list.append(user)
  282. return users_list
  283. async def add_served_user(user_id: int):
  284. is_served = await is_served_user(user_id)
  285. if is_served:
  286. return
  287. return await usersdb.insert_one({"user_id": user_id})
  288. """ Gban functions """
  289. async def get_gbans_count() -> int:
  290. users = gbansdb.find({"user_id": {"$gt": 0}})
  291. users = await users.to_list(length=100000)
  292. return len(users)
  293. async def is_gbanned_user(user_id: int) -> bool:
  294. user = await gbansdb.find_one({"user_id": user_id})
  295. if not user:
  296. return False
  297. return True
  298. async def add_gban_user(user_id: int):
  299. is_gbanned = await is_gbanned_user(user_id)
  300. if is_gbanned:
  301. return
  302. return await gbansdb.insert_one({"user_id": user_id})
  303. async def remove_gban_user(user_id: int):
  304. is_gbanned = await is_gbanned_user(user_id)
  305. if not is_gbanned:
  306. return
  307. return await gbansdb.delete_one({"user_id": user_id})
  308. # Couple Chooser
  309. async def _get_lovers(chat_id: int):
  310. lovers = await coupledb.find_one({"chat_id": chat_id})
  311. if not lovers:
  312. return {}
  313. return lovers["couple"]
  314. async def get_couple(chat_id: int, date: str):
  315. lovers = await _get_lovers(chat_id)
  316. if date in lovers:
  317. return lovers[date]
  318. else:
  319. return False
  320. async def save_couple(chat_id: int, date: str, couple: dict):
  321. lovers = await _get_lovers(chat_id)
  322. lovers[date] = couple
  323. await coupledb.update_one(
  324. {"chat_id": chat_id}, {"$set": {"couple": lovers}}, upsert=True
  325. )
  326. # Captcha
  327. async def is_captcha_on(chat_id: int) -> bool:
  328. chat = await captchadb.find_one({"chat_id": chat_id})
  329. if not chat:
  330. return True
  331. return False
  332. async def captcha_on(chat_id: int):
  333. is_captcha = await is_captcha_on(chat_id)
  334. if is_captcha:
  335. return
  336. return await captchadb.delete_one({"chat_id": chat_id})
  337. async def captcha_off(chat_id: int):
  338. is_captcha = await is_captcha_on(chat_id)
  339. if not is_captcha:
  340. return
  341. return await captchadb.insert_one({"chat_id": chat_id})
  342. """Anti Service System"""
  343. async def is_antiservice_on(chat_id: int) -> bool:
  344. chat = await antiservicedb.find_one({"chat_id": chat_id})
  345. if not chat:
  346. return True
  347. return False
  348. async def antiservice_on(chat_id: int):
  349. is_antiservice = await is_antiservice_on(chat_id)
  350. if is_antiservice:
  351. return
  352. return await antiservicedb.delete_one({"chat_id": chat_id})
  353. async def antiservice_off(chat_id: int):
  354. is_antiservice = await is_antiservice_on(chat_id)
  355. if not is_antiservice:
  356. return
  357. return await antiservicedb.insert_one({"chat_id": chat_id})
  358. """ PM PERMIT """
  359. async def is_pmpermit_approved(user_id: int) -> bool:
  360. user = await pmpermitdb.find_one({"user_id": user_id})
  361. if not user:
  362. return False
  363. return True
  364. async def approve_pmpermit(user_id: int):
  365. is_pmpermit = await is_pmpermit_approved(user_id)
  366. if is_pmpermit:
  367. return
  368. return await pmpermitdb.insert_one({"user_id": user_id})
  369. async def disapprove_pmpermit(user_id: int):
  370. is_pmpermit = await is_pmpermit_approved(user_id)
  371. if not is_pmpermit:
  372. return
  373. return await pmpermitdb.delete_one({"user_id": user_id})
  374. """ WELCOME FUNCTIONS """
  375. async def get_welcome(chat_id: int) -> str:
  376. text = await welcomedb.find_one({"chat_id": chat_id})
  377. return text["text"]
  378. async def set_welcome(chat_id: int, text: str):
  379. return await welcomedb.update_one(
  380. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  381. )
  382. async def del_welcome(chat_id: int):
  383. return await welcomedb.delete_one({"chat_id": chat_id})
  384. """NSFW System"""
  385. async def is_nsfw_on(chat_id: int) -> bool:
  386. chat = await nsfwdb.find_one({"chat_id": chat_id})
  387. if not chat:
  388. return True
  389. return False
  390. async def nsfw_on(chat_id: int):
  391. is_nsfw = await is_nsfw_on(chat_id)
  392. if is_nsfw:
  393. return
  394. return await nsfwdb.delete_one({"chat_id": chat_id})
  395. async def nsfw_off(chat_id: int):
  396. is_nsfw = await is_nsfw_on(chat_id)
  397. if not is_nsfw:
  398. return
  399. return await nsfwdb.insert_one({"chat_id": chat_id})
  400. """ CAPTCHA CACHE SYSTEM """
  401. async def update_captcha_cache(captcha_dict):
  402. pickle = obj_to_str(captcha_dict)
  403. await captcha_cachedb.delete_one({"captcha": "cache"})
  404. if not pickle:
  405. return
  406. await captcha_cachedb.update_one(
  407. {"captcha": "cache"}, {"$set": {"pickled": pickle}}, upsert=True
  408. )
  409. async def get_captcha_cache():
  410. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  411. if not cache:
  412. return []
  413. return str_to_obj(cache["pickled"])
  414. """ BLACKLIST FILTERS SYSTEM """
  415. async def get_blacklist_filters_count() -> dict:
  416. chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
  417. if not chats:
  418. return {"chats_count": 0, "filters_count": 0}
  419. chats_count = 0
  420. filters_count = 0
  421. for chat in await chats.to_list(length=1000000000):
  422. filters = await get_blacklisted_words(chat["chat_id"])
  423. filters_count += len(filters)
  424. chats_count += 1
  425. return {"chats_count": chats_count, "filters_count": filters_count}
  426. async def get_blacklisted_words(chat_id: int) -> List[str]:
  427. _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
  428. if not _filters:
  429. return []
  430. return _filters["filters"]
  431. async def save_blacklist_filter(chat_id: int, word: str):
  432. word = word.lower().strip()
  433. _filters = await get_blacklisted_words(chat_id)
  434. _filters.append(word)
  435. await blacklist_filtersdb.update_one(
  436. {"chat_id": chat_id}, {"$set": {"filters": _filters}}, upsert=True
  437. )
  438. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  439. filtersd = await get_blacklisted_words(chat_id)
  440. word = word.lower().strip()
  441. if word in filtersd:
  442. filtersd.remove(word)
  443. await blacklist_filtersdb.update_one(
  444. {"chat_id": chat_id}, {"$set": {"filters": filtersd}}, upsert=True
  445. )
  446. return True
  447. return False
  448. """ PIPES SYSTEM """
  449. async def activate_pipe(from_chat_id: int, to_chat_id: int, fetcher: str):
  450. pipes = await show_pipes()
  451. pipe = {
  452. "from_chat_id": from_chat_id,
  453. "to_chat_id": to_chat_id,
  454. "fetcher": fetcher,
  455. }
  456. pipes.append(pipe)
  457. return await pipesdb.update_one(
  458. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  459. )
  460. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  461. pipes = await show_pipes()
  462. if not pipes:
  463. return
  464. for pipe in pipes:
  465. if (
  466. pipe["from_chat_id"] == from_chat_id
  467. and pipe["to_chat_id"] == to_chat_id
  468. ):
  469. pipes.remove(pipe)
  470. return await pipesdb.update_one(
  471. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  472. )
  473. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  474. for pipe in await show_pipes():
  475. if (
  476. pipe["from_chat_id"] == from_chat_id
  477. and pipe["to_chat_id"] == to_chat_id
  478. ):
  479. return True
  480. async def show_pipes() -> list:
  481. pipes = await pipesdb.find_one({"pipe": "pipe"})
  482. if not pipes:
  483. return []
  484. return pipes["pipes"]
  485. """ SUDOERS FUNCTIONS """
  486. async def get_sudoers() -> list:
  487. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  488. if not sudoers:
  489. return []
  490. return sudoers["sudoers"]
  491. async def add_sudo(user_id: int) -> bool:
  492. sudoers = await get_sudoers()
  493. sudoers.append(user_id)
  494. await sudoersdb.update_one(
  495. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  496. )
  497. return True
  498. async def remove_sudo(user_id: int) -> bool:
  499. sudoers = await get_sudoers()
  500. sudoers.remove(user_id)
  501. await sudoersdb.update_one(
  502. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  503. )
  504. return True
  505. """ BLACKLISTED CHATS """
  506. async def blacklisted_chats() -> list:
  507. chats = blacklist_chatdb.find({"chat_id": {"$lt": 0}})
  508. return [chat["chat_id"] for chat in await chats.to_list(length=1000000000)]
  509. async def blacklist_chat(chat_id: int) -> bool:
  510. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  511. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  512. return True
  513. return False
  514. async def whitelist_chat(chat_id: int) -> bool:
  515. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  516. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  517. return True
  518. return False
  519. """ Restart stage """
  520. async def start_restart_stage(chat_id: int, message_id: int):
  521. await restart_stagedb.update_one(
  522. {"something": "something"},
  523. {
  524. "$set": {
  525. "chat_id": chat_id,
  526. "message_id": message_id,
  527. }
  528. },
  529. upsert=True,
  530. )
  531. async def clean_restart_stage() -> dict:
  532. data = await restart_stagedb.find_one({"something": "something"})
  533. if not data:
  534. return {}
  535. await restart_stagedb.delete_one({"something": "something"})
  536. return {"chat_id": data["chat_id"], "message_id": data["message_id"]}