dbfunctions.py 14 KB

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