dbfunctions.py 16 KB

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