dbfunctions.py 15 KB

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