dbfunctions.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. if karma:
  254. if int(karma['karma']) > 0:
  255. total_karma += int(karma['karma'])
  256. return total_karma
  257. async def get_karmas(chat_id: int) -> Dict[str, int]:
  258. karma = await karmadb.find_one({"chat_id": chat_id})
  259. if karma:
  260. karma = karma['karma']
  261. else:
  262. karma = {}
  263. return karma
  264. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  265. name = name.lower().strip()
  266. karmas = await get_karmas(chat_id)
  267. if name in karmas:
  268. return karmas[name]
  269. async def update_karma(chat_id: int, name: str, karma: dict):
  270. name = name.lower().strip()
  271. karmas = await get_karmas(chat_id)
  272. karmas[name] = karma
  273. await karmadb.update_one(
  274. {"chat_id": chat_id},
  275. {
  276. "$set": {
  277. "karma": karmas
  278. }
  279. },
  280. upsert=True
  281. )
  282. async def is_karma_on(chat_id: int) -> bool:
  283. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  284. if not chat:
  285. return True
  286. return False
  287. async def karma_on(chat_id: int):
  288. is_karma = await is_karma_on(chat_id)
  289. if is_karma:
  290. return
  291. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  292. async def karma_off(chat_id: int):
  293. is_karma = await is_karma_on(chat_id)
  294. if not is_karma:
  295. return
  296. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  297. """ Chats log functions """
  298. async def is_served_chat(chat_id: int) -> bool:
  299. chat = await chatsdb.find_one({"chat_id": chat_id})
  300. if not chat:
  301. return False
  302. return True
  303. async def get_served_chats() -> list:
  304. chats = chatsdb.find({"chat_id": {'$lt': 0}})
  305. if not chats:
  306. return []
  307. chats_list = []
  308. for chat in await chats.to_list(length=1000000000):
  309. chats_list.append(chat)
  310. return chats_list
  311. async def add_served_chat(chat_id: int):
  312. is_served = await is_served_chat(chat_id)
  313. if is_served:
  314. return
  315. return await chatsdb.insert_one({"chat_id": chat_id})
  316. async def remove_served_chat(chat_id: int):
  317. is_served = await is_served_chat(chat_id)
  318. if not is_served:
  319. return
  320. return await chatsdb.delete_one({"chat_id": chat_id})
  321. """ Gban functions """
  322. async def get_gbans_count() -> int:
  323. users = gbansdb.find({"user_id": {"$gt": 0}})
  324. users = await users.to_list(length=100000)
  325. return len(users)
  326. async def is_gbanned_user(user_id: int) -> bool:
  327. user = await gbansdb.find_one({"user_id": user_id})
  328. if not user:
  329. return False
  330. return True
  331. async def add_gban_user(user_id: int):
  332. is_gbanned = await is_gbanned_user(user_id)
  333. if is_gbanned:
  334. return
  335. return await gbansdb.insert_one({"user_id": user_id})
  336. async def remove_gban_user(user_id: int):
  337. is_gbanned = await is_gbanned_user(user_id)
  338. if not is_gbanned:
  339. return
  340. return await gbansdb.delete_one({"user_id": user_id})
  341. # Couple Chooser
  342. async def _get_lovers(chat_id: int):
  343. lovers = await coupledb.find_one({"chat_id": chat_id})
  344. if lovers:
  345. lovers = lovers["couple"]
  346. else:
  347. lovers = {}
  348. return lovers
  349. async def get_couple(chat_id: int, date: str):
  350. lovers = await _get_lovers(chat_id)
  351. if date in lovers:
  352. return lovers[date]
  353. else:
  354. return False
  355. async def save_couple(chat_id: int, date: str, couple: dict):
  356. lovers = await _get_lovers(chat_id)
  357. lovers[date] = couple
  358. await coupledb.update_one(
  359. {"chat_id": chat_id},
  360. {
  361. "$set": {
  362. "couple": lovers
  363. }
  364. },
  365. upsert=True
  366. )
  367. # Captcha
  368. async def is_captcha_on(chat_id: int) -> bool:
  369. chat = await captchadb.find_one({"chat_id": chat_id})
  370. if not chat:
  371. return True
  372. return False
  373. async def captcha_on(chat_id: int):
  374. is_captcha = await is_captcha_on(chat_id)
  375. if is_captcha:
  376. return
  377. return await captchadb.delete_one({"chat_id": chat_id})
  378. async def captcha_off(chat_id: int):
  379. is_captcha = await is_captcha_on(chat_id)
  380. if not is_captcha:
  381. return
  382. return await captchadb.insert_one({"chat_id": chat_id})
  383. """Anti Service System"""
  384. async def is_antiservice_on(chat_id: int) -> bool:
  385. chat = await antiservicedb.find_one({"chat_id": chat_id})
  386. if not chat:
  387. return True
  388. return False
  389. async def antiservice_on(chat_id: int):
  390. is_antiservice = await is_antiservice_on(chat_id)
  391. if is_antiservice:
  392. return
  393. return await antiservicedb.delete_one({"chat_id": chat_id})
  394. async def antiservice_off(chat_id: int):
  395. is_antiservice = await is_antiservice_on(chat_id)
  396. if not is_antiservice:
  397. return
  398. return await antiservicedb.insert_one({"chat_id": chat_id})
  399. """ PM PERMIT """
  400. async def is_pmpermit_approved(user_id: int) -> bool:
  401. user = await pmpermitdb.find_one({"user_id": user_id})
  402. if not user:
  403. return False
  404. return True
  405. async def approve_pmpermit(user_id: int):
  406. is_pmpermit = await is_pmpermit_approved(user_id)
  407. if is_pmpermit:
  408. return
  409. return await pmpermitdb.insert_one({"user_id": user_id})
  410. async def disapprove_pmpermit(user_id: int):
  411. is_pmpermit = await is_pmpermit_approved(user_id)
  412. if not is_pmpermit:
  413. return
  414. return await pmpermitdb.delete_one({"user_id": user_id})
  415. """ WELCOME FUNCTIONS """
  416. async def get_welcome(chat_id: int) -> str:
  417. text = await welcomedb.find_one({"chat_id": chat_id})
  418. return text['text']
  419. async def set_welcome(chat_id: int, text: str):
  420. return await welcomedb.update_one(
  421. {"chat_id": chat_id},
  422. {
  423. "$set": {
  424. "text": text
  425. }
  426. },
  427. upsert=True
  428. )
  429. async def del_welcome(chat_id: int):
  430. return await welcomedb.delete_one({"chat_id": chat_id})
  431. """NSFW System"""
  432. async def is_nsfw_on(chat_id: int) -> bool:
  433. chat = await nsfwdb.find_one({"chat_id": chat_id})
  434. if not chat:
  435. return True
  436. return False
  437. async def nsfw_on(chat_id: int):
  438. is_nsfw = await is_nsfw_on(chat_id)
  439. if is_nsfw:
  440. return
  441. return await nsfwdb.delete_one({"chat_id": chat_id})
  442. async def nsfw_off(chat_id: int):
  443. is_nsfw = await is_nsfw_on(chat_id)
  444. if not is_nsfw:
  445. return
  446. return await nsfwdb.insert_one({"chat_id": chat_id})