dbfunctions.py 18 KB

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