dbfunctions.py 17 KB

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