dbfunctions.py 17 KB

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