dbfunctions.py 13 KB

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