dbfunctions.py 17 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 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.blacklist_filters
  37. pipesdb = db.pipes
  38. sudoersdb = db.sudoers
  39. blacklist_chatdb = db.blacklist_chat
  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. karmas_count += chat["karma"][i]["karma"]
  196. chats_count += 1
  197. return {"chats_count": chats_count, "karmas_count": karmas_count}
  198. async def user_global_karma(user_id) -> int:
  199. chats = karmadb.find({"chat_id": {"$lt": 0}})
  200. if not chats:
  201. return 0
  202. total_karma = 0
  203. for chat in await chats.to_list(length=1000000):
  204. karma = await get_karma(chat["chat_id"], await int_to_alpha(user_id))
  205. if karma:
  206. if int(karma["karma"]) > 0:
  207. total_karma += int(karma["karma"])
  208. return total_karma
  209. async def get_karmas(chat_id: int) -> Dict[str, int]:
  210. karma = await karmadb.find_one({"chat_id": chat_id})
  211. if not karma:
  212. return {}
  213. return karma["karma"]
  214. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  215. name = name.lower().strip()
  216. karmas = await get_karmas(chat_id)
  217. if name in karmas:
  218. return karmas[name]
  219. async def update_karma(chat_id: int, name: str, karma: dict):
  220. name = name.lower().strip()
  221. karmas = await get_karmas(chat_id)
  222. karmas[name] = karma
  223. await karmadb.update_one(
  224. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  225. )
  226. async def is_karma_on(chat_id: int) -> bool:
  227. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  228. if not chat:
  229. return True
  230. return False
  231. async def karma_on(chat_id: int):
  232. is_karma = await is_karma_on(chat_id)
  233. if is_karma:
  234. return
  235. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  236. async def karma_off(chat_id: int):
  237. is_karma = await is_karma_on(chat_id)
  238. if not is_karma:
  239. return
  240. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  241. """ Chats log functions """
  242. async def is_served_chat(chat_id: int) -> bool:
  243. chat = await chatsdb.find_one({"chat_id": chat_id})
  244. if not chat:
  245. return False
  246. return True
  247. async def get_served_chats() -> list:
  248. chats = chatsdb.find({"chat_id": {"$lt": 0}})
  249. if not chats:
  250. return []
  251. chats_list = []
  252. for chat in await chats.to_list(length=1000000000):
  253. chats_list.append(chat)
  254. return chats_list
  255. async def add_served_chat(chat_id: int):
  256. is_served = await is_served_chat(chat_id)
  257. if is_served:
  258. return
  259. return await chatsdb.insert_one({"chat_id": chat_id})
  260. async def remove_served_chat(chat_id: int):
  261. is_served = await is_served_chat(chat_id)
  262. if not is_served:
  263. return
  264. return await chatsdb.delete_one({"chat_id": chat_id})
  265. """ Gban functions """
  266. async def get_gbans_count() -> int:
  267. users = gbansdb.find({"user_id": {"$gt": 0}})
  268. users = await users.to_list(length=100000)
  269. return len(users)
  270. async def is_gbanned_user(user_id: int) -> bool:
  271. user = await gbansdb.find_one({"user_id": user_id})
  272. if not user:
  273. return False
  274. return True
  275. async def add_gban_user(user_id: int):
  276. is_gbanned = await is_gbanned_user(user_id)
  277. if is_gbanned:
  278. return
  279. return await gbansdb.insert_one({"user_id": user_id})
  280. async def remove_gban_user(user_id: int):
  281. is_gbanned = await is_gbanned_user(user_id)
  282. if not is_gbanned:
  283. return
  284. return await gbansdb.delete_one({"user_id": user_id})
  285. # Couple Chooser
  286. async def _get_lovers(chat_id: int):
  287. lovers = await coupledb.find_one({"chat_id": chat_id})
  288. if not lovers:
  289. return {}
  290. return lovers["couple"]
  291. async def get_couple(chat_id: int, date: str):
  292. lovers = await _get_lovers(chat_id)
  293. if date in lovers:
  294. return lovers[date]
  295. else:
  296. return False
  297. async def save_couple(chat_id: int, date: str, couple: dict):
  298. lovers = await _get_lovers(chat_id)
  299. lovers[date] = couple
  300. await coupledb.update_one(
  301. {"chat_id": chat_id}, {"$set": {"couple": lovers}}, upsert=True
  302. )
  303. # Captcha
  304. async def is_captcha_on(chat_id: int) -> bool:
  305. chat = await captchadb.find_one({"chat_id": chat_id})
  306. if not chat:
  307. return True
  308. return False
  309. async def captcha_on(chat_id: int):
  310. is_captcha = await is_captcha_on(chat_id)
  311. if is_captcha:
  312. return
  313. return await captchadb.delete_one({"chat_id": chat_id})
  314. async def captcha_off(chat_id: int):
  315. is_captcha = await is_captcha_on(chat_id)
  316. if not is_captcha:
  317. return
  318. return await captchadb.insert_one({"chat_id": chat_id})
  319. """Anti Service System"""
  320. async def is_antiservice_on(chat_id: int) -> bool:
  321. chat = await antiservicedb.find_one({"chat_id": chat_id})
  322. if not chat:
  323. return True
  324. return False
  325. async def antiservice_on(chat_id: int):
  326. is_antiservice = await is_antiservice_on(chat_id)
  327. if is_antiservice:
  328. return
  329. return await antiservicedb.delete_one({"chat_id": chat_id})
  330. async def antiservice_off(chat_id: int):
  331. is_antiservice = await is_antiservice_on(chat_id)
  332. if not is_antiservice:
  333. return
  334. return await antiservicedb.insert_one({"chat_id": chat_id})
  335. """ PM PERMIT """
  336. async def is_pmpermit_approved(user_id: int) -> bool:
  337. user = await pmpermitdb.find_one({"user_id": user_id})
  338. if not user:
  339. return False
  340. return True
  341. async def approve_pmpermit(user_id: int):
  342. is_pmpermit = await is_pmpermit_approved(user_id)
  343. if is_pmpermit:
  344. return
  345. return await pmpermitdb.insert_one({"user_id": user_id})
  346. async def disapprove_pmpermit(user_id: int):
  347. is_pmpermit = await is_pmpermit_approved(user_id)
  348. if not is_pmpermit:
  349. return
  350. return await pmpermitdb.delete_one({"user_id": user_id})
  351. """ WELCOME FUNCTIONS """
  352. async def get_welcome(chat_id: int) -> str:
  353. text = await welcomedb.find_one({"chat_id": chat_id})
  354. return text["text"]
  355. async def set_welcome(chat_id: int, text: str):
  356. return await welcomedb.update_one(
  357. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  358. )
  359. async def del_welcome(chat_id: int):
  360. return await welcomedb.delete_one({"chat_id": chat_id})
  361. """NSFW System"""
  362. async def is_nsfw_on(chat_id: int) -> bool:
  363. chat = await nsfwdb.find_one({"chat_id": chat_id})
  364. if not chat:
  365. return True
  366. return False
  367. async def nsfw_on(chat_id: int):
  368. is_nsfw = await is_nsfw_on(chat_id)
  369. if is_nsfw:
  370. return
  371. return await nsfwdb.delete_one({"chat_id": chat_id})
  372. async def nsfw_off(chat_id: int):
  373. is_nsfw = await is_nsfw_on(chat_id)
  374. if not is_nsfw:
  375. return
  376. return await nsfwdb.insert_one({"chat_id": chat_id})
  377. """ CAPTCHA CACHE SYSTEM """
  378. async def update_captcha_cache(captcha_dict):
  379. pickle = obj_to_str(captcha_dict)
  380. await captcha_cachedb.delete_one({"captcha": "cache"})
  381. if not pickle:
  382. return
  383. await captcha_cachedb.update_one(
  384. {"captcha": "cache"}, {"$set": {"pickled": pickle}}, upsert=True
  385. )
  386. async def get_captcha_cache():
  387. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  388. if not cache:
  389. return []
  390. return str_to_obj(cache["pickled"])
  391. """ BLACKLIST FILTERS SYSTEM """
  392. async def get_blacklist_filters_count() -> dict:
  393. chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
  394. if not chats:
  395. return {}
  396. chats_count = 0
  397. filters_count = 0
  398. for chat in await chats.to_list(length=1000000000):
  399. filters = await get_blacklisted_words(chat["chat_id"])
  400. filters_count += len(filters)
  401. chats_count += 1
  402. return {"chats_count": chats_count, "filters_count": filters_count}
  403. async def get_blacklisted_words(chat_id: int) -> List[str]:
  404. _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
  405. if not _filters:
  406. return []
  407. return _filters["filters"]
  408. async def save_blacklist_filter(chat_id: int, word: str):
  409. word = word.lower().strip()
  410. _filters = await get_blacklisted_words(chat_id)
  411. _filters.append(word)
  412. await blacklist_filtersdb.update_one(
  413. {"chat_id": chat_id}, {"$set": {"filters": _filters}}, upsert=True
  414. )
  415. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  416. filtersd = await get_blacklisted_words(chat_id)
  417. word = word.lower().strip()
  418. if word in filtersd:
  419. filtersd.remove(word)
  420. await blacklist_filtersdb.update_one(
  421. {"chat_id": chat_id}, {"$set": {"filters": filtersd}}, upsert=True
  422. )
  423. return True
  424. return False
  425. """ PIPES SYSTEM """
  426. async def activate_pipe(from_chat_id: int, to_chat_id: int, fetcher: str):
  427. pipes = await show_pipes()
  428. pipe = {"from_chat_id": from_chat_id, "to_chat_id": to_chat_id, "fetcher": fetcher}
  429. pipes.append(pipe)
  430. return await pipesdb.update_one(
  431. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  432. )
  433. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  434. pipes = await show_pipes()
  435. if not pipes:
  436. return
  437. for pipe in pipes:
  438. if pipe["from_chat_id"] == from_chat_id and pipe["to_chat_id"] == to_chat_id:
  439. pipes.remove(pipe)
  440. return await pipesdb.update_one(
  441. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  442. )
  443. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  444. for pipe in await show_pipes():
  445. if pipe["from_chat_id"] == from_chat_id and pipe["to_chat_id"] == to_chat_id:
  446. return True
  447. async def show_pipes() -> list:
  448. pipes = await pipesdb.find_one({"pipe": "pipe"})
  449. if not pipes:
  450. return []
  451. return pipes["pipes"]
  452. """ SUDOERS FUNCTIONS """
  453. async def get_sudoers() -> list:
  454. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  455. if not sudoers:
  456. return []
  457. return sudoers["sudoers"]
  458. async def add_sudo(user_id: int) -> bool:
  459. sudoers = await get_sudoers()
  460. sudoers.append(user_id)
  461. await sudoersdb.update_one(
  462. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  463. )
  464. return True
  465. async def remove_sudo(user_id: int) -> bool:
  466. sudoers = await get_sudoers()
  467. sudoers.remove(user_id)
  468. await sudoersdb.update_one(
  469. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  470. )
  471. return True
  472. """ BLACKLIST CHATS FUNCTIONS """
  473. async def blacklisted_chats() -> list:
  474. chats = await blacklist_chatdb.find_one({"chat": "chat"})
  475. if not chats:
  476. return []
  477. return chats["chats"]
  478. async def blacklist_chat(chat_id: int) -> bool:
  479. chats = await blacklisted_chats()
  480. chats.append(chats)
  481. await blacklist_chatdb.update_one(
  482. {"chat": "chat"}, {"$set": {"chats": chats}}, upsert=True
  483. )
  484. return True
  485. async def whitelist_chat(chat_id: int) -> bool:
  486. chats = await blacklisted_chats()
  487. chats.remove(chats)
  488. await blacklist_chatdb.update_one(
  489. {"chat": "chat"}, {"$set": {"chats": chats}}, upsert=True
  490. )
  491. return True