dbfunctions.py 18 KB

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